Uploading a File to a File Server

This section describes how to use the Sentinel EMS API with ESD as a Service (ESDaaS) to upload a file to a file server and access it.

The GET /ems/api/v5/stsToken endpoint generates an STS token that is used for securely uploading a file to a file server. The STS token is a set of temporary security credentials that you can use to authenticate the user who is uploading the file. An STS token is valid for 30 minutes.

A sample response of this endpoint is shown below:

{
  "SecretAccessKey": "Ts8HO/nndTkKezUuLGdqFvYv2U9uIT+QAF0uuzYa",
  "apiRegion": "eu-west-1",
  "s3Region": "eu-west-1",
  "bucketName": "sentinel-esd-qa-e2",
  "domainAlias": "qa-e2.test.sentinelcloud.com",
  "AccessKeyId": "ASIAXF67OJWZF347H7XU",
  "SessionToken": "FwoGZXIvYXdzEMH//////////wEaDGP5uaXPGi9SWuP85iKBAUbJv6wDtBFrJWsgdPGNo+6V6SlWcSEQWBvo7r1Qsh/oW4VT5AzQd3w=="
}

Sample Java Program

The following Java program securely uploads a file to an S3 Bucket using the STS token generated by the GET /ems/api/v5/stsToken endpoint. You can customize the program according to your requirements.

You must update the sections highlighted in bold, as explained in the section Uploading a File.

import java.io.File;
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
public class UploadFileToS3Example  {
	private static String bucketName = "sentinel-esd-qa-e2";
	private static String keyName = "download-identifier/test-file.txt";
	private static String uploadFileName = "c:\\test-file.txt";
	private static String accessKeyId = "ASIAXF67OJWZF347H7XU";
	private static String secretAccessKey = "Ts8HO/nndTkKezUuLGdqFvYv2U9uIT+QAF0uuzYa";
	private static String sessionToken = "FwoGZXIvYXdzEMH//////////wEaDGP5uaXPGi9SWuP85iKBAU+6V6SlWcSEQWBvo7r1Qsh/oW4VT5AzQd3w==";
	private static String domainAlias = "qa-e2.test.sentinelcloud.com";
	private static String s3Region = "eu-west-1";	
	public static void main(String[] args) {
		// Creating AWS Session Credentials to get access to the s3 bucket
		AwsSessionCredentials awsCreds = AwsSessionCredentials.create(accessKeyId, secretAccessKey, sessionToken);		
		// Fetching the s3 bucket region object 
		Region region = null;
		for (Region regionObj : Region.regions()) {
			if (regionObj.toString().equalsIgnoreCase(s3Region.toLowerCase())) {
				region = regionObj;
			}
		}		
		// Creating the s3Client object
		S3Client s3 = S3Client.builder().region(region).credentialsProvider(StaticCredentialsProvider.create(awsCreds))
				.build();		
		// Creating a PutObjectRequest 
		PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucketName).key(keyName).build();		try {		
		// Reading the file to be uploaded
		File file = new File(uploadFileName);	
		// Passing the file you want to upload
			PutObjectResponse putObjectResponse = s3.putObject(objectRequest, RequestBody.fromFile(file));
		// File Download Base URL
			String downloadFileLink = "http://" + domainAlias + "/" + keyName;
			System.out.println(downloadFileLink);
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

Libraries

The libraries used in the sample Java program are:

>aws-java-sdk-2.15.19

>commons-codec-1.9

>commons-io-1.4

>commons-logging-1.1

>httpclient-4.5.2

>httpcore-4.4.4

>jackson-annotations-2.6.0

>jackson-core-2.6.6

>jackson-databind-2.6.6

>jackson-dataformat-cbor-2.6.6

>joda-time-2.8.1

Build Dependencies

The following is an example of the build.gradle file containing calls to dependent libraries:

group 'aws.test'
version '1.0'apply plugin: 'java'sourceCompatibility = 1.8repositories {
  mavenCentral()
}
dependencies {
  implementation platform('software.amazon.awssdk:bom:2.15.19')
  implementation 'software.amazon.awssdk:s3'
  testImplementation group: 'junit', name: 'junit', version: '4.11'
  testCompile group: 'software.amazon.awssdk', name: 's3', version: '2.15.19'  
}

Uploading a File

Perform the following steps to use the Java sample above to upload a file to an S3 bucket:

1.Call the GET /ems/api/v5/stsToken endpoint to get an STS token in the response. You will need to copy and paste the values from the response to the Java program. A sample response of this endpoint is shown below:

{
  "SecretAccessKey": "Ts8HO/nndTkKezUuLGdqFvYv2U9uIT+QAF0uuzYa",
  "apiRegion": "eu-west-1",
  "s3Region": "eu-west-1",
  "bucketName": "sentinel-esd-qa-e2",
  "domainAlias": "qa-e2.test.sentinelcloud.com",
  "AccessKeyId": "ASIAXF67OJWZF347H7XU",
  "SessionToken": "FwoGZXIvYXdzEMH//////////wEaDGP5uaXPGi9SWuP85iKBAU+6V6SlWcSEQWBvo7r1Qsh/oW4VT5AzQd3w=="
}

2.Replace the variable values in the sample Java program with the values obtained in the STS token:

private static String bucketName = "sentinel-esd-qa-e2";
private static String accessKeyId = "ASIAXF67OJWZF347H7XU";
private static String secretAccessKey = "Ts8HO/nndTkKezUuLGdqFvYv2U9uIT+QAF0uuzYa";
private static String sessionToken = "FwoGZXIvYXdzEMH//////////wEaDGP5uaXPGi9SWuP85iKBA+6V6SlWcSEQWBvo7r1Qsh/oW4VT5AzQd3w==;
private static String domainAlias = "qa-e2.test.sentinelcloud.com";
private static String s3Region = "eu-west-1";	

3.Specify the name and path of the file you want to upload. You can generate a new download identifier and specify it with the file name in the keyName variable (for example, download-identifier/test-file). This creates a folder named download-identifier on the file server and stores the file named test-file in it. The download identifier ensures that a file with the same name can be uploaded for separate downloads.

For example:

private static String keyName = "download_identifier/test-file.txt";
private static String uploadFileName = "c:\\test-file.txt";

4.Compile and run the Java program. The program uploads the file to the file server and generates a base URL of the location where the file is uploaded. The following is an example of the base URL:

<url>http://qa-e2.test.sentinelcloud.com/download_identifier/test-file.txt</url>

You can use the base URL to create a download definition in EMS.

Creating a Download Definition

After uploading a file to a file server, you can use its base URL to create a download definition by using the EMS REST API.

Use the base URL in the request body of the POST /ems/api/v5/downloads to create a download definition. For example:

{

  "download": {
    "identifier": "TestIdentifier",
    "nameVersion": {
      "name": "TestDownload",
      "version": "1.0"
    },
    "releaseDate": "2021-04-19",
    "state": "ENABLE",
    "isAttachable": false,
    "description": "Download description",
    "file": {
      "name": "MyFile.txt",
      "size": "",
      "logo": "",
      "url": "http://qa-e2.test.sentinelcloud.com//download_identifier/test-file.txt"
    }
  }
}

Accessing a Download

You can use the Get /ems/api/v5/downloads/{downloadId} endpoint to access a download. You can also view the download from the Add Downloads page of Sentinel EMS GUI. Refer to Downloads for more information.