Use GetObject with an AWS SDK or CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use GetObject with an AWS SDK or CLI

The following code examples show how to use GetObject.

CLI
AWS CLI

To download an object

The following get-object example download an object to the specified endpoint.

aws mediastore-data get-object \ --endpoint https://aaabbbcccdddee.data.mediastore.us-west-2.amazonaws.com \ --path=/folder_name/README.md README.md

Output:

{ "ContentLength": "2307346", "ContentType": "image/jpeg", "LastModified": "Fri, 19 Jul 2019 21:32:20 GMT", "ETag": "2aa333bbcc8d8d22d777e999c88d4aa9eeeeee4dd89ff7f555555555555da6d3", "StatusCode": 200 }

To download part of an object

The following get-object example downloads a portion an object to the specified endpoint.

aws mediastore-data get-object \ --endpoint https://aaabbbcccdddee.data.mediastore.us-west-2.amazonaws.com \ --path /folder_name/README.md \ --range="bytes=0-100" README2.md

Output:

{ "StatusCode": 206, "ContentRange": "bytes 0-100/2307346", "ContentLength": "101", "LastModified": "Fri, 19 Jul 2019 21:32:20 GMT", "ContentType": "image/jpeg", "ETag": "2aa333bbcc8d8d22d777e999c88d4aa9eeeeee4dd89ff7f555555555555da6d3" }

For more information, see Downloading an Object in the AWS Elemental MediaStore User Guide.

  • For API details, see GetObject in AWS CLI Command Reference.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import software.amazon.awssdk.core.ResponseInputStream; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.mediastore.MediaStoreClient; import software.amazon.awssdk.services.mediastore.model.DescribeContainerRequest; import software.amazon.awssdk.services.mediastore.model.DescribeContainerResponse; import software.amazon.awssdk.services.mediastoredata.MediaStoreDataClient; import software.amazon.awssdk.services.mediastoredata.model.GetObjectRequest; import software.amazon.awssdk.services.mediastoredata.model.GetObjectResponse; import software.amazon.awssdk.services.mediastoredata.model.MediaStoreDataException; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class GetObject { public static void main(String[] args) throws URISyntaxException { final String usage = """ Usage: <completePath> <containerName> <savePath> Where: completePath - The path of the object in the container (for example, Videos5/sampleVideo.mp4). containerName - The name of the container. savePath - The path on the local drive where the file is saved, including the file name (for example, C:/AWS/myvid.mp4). """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String completePath = args[0]; String containerName = args[1]; String savePath = args[2]; Region region = Region.US_EAST_1; URI uri = new URI(getEndpoint(containerName)); MediaStoreDataClient mediaStoreData = MediaStoreDataClient.builder() .endpointOverride(uri) .region(region) .build(); getMediaObject(mediaStoreData, completePath, savePath); mediaStoreData.close(); } public static void getMediaObject(MediaStoreDataClient mediaStoreData, String completePath, String savePath) { try { GetObjectRequest objectRequest = GetObjectRequest.builder() .path(completePath) .build(); // Write out the data to a file. ResponseInputStream<GetObjectResponse> data = mediaStoreData.getObject(objectRequest); byte[] buffer = new byte[data.available()]; data.read(buffer); File targetFile = new File(savePath); OutputStream outStream = new FileOutputStream(targetFile); outStream.write(buffer); System.out.println("The data was written to " + savePath); } catch (MediaStoreDataException | IOException e) { System.err.println(e.getMessage()); System.exit(1); } } private static String getEndpoint(String containerName) { Region region = Region.US_EAST_1; MediaStoreClient mediaStoreClient = MediaStoreClient.builder() .region(region) .build(); DescribeContainerRequest containerRequest = DescribeContainerRequest.builder() .containerName(containerName) .build(); DescribeContainerResponse response = mediaStoreClient.describeContainer(containerRequest); return response.container().endpoint(); } }
  • For API details, see GetObject in AWS SDK for Java 2.x API Reference.