Use GetObject com um AWS SDK ou CLI - AWS SDKExemplos de código

Há mais AWS SDK exemplos disponíveis no GitHub repositório AWS Doc SDK Examples.

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Use GetObject com um AWS SDK ou CLI

Os exemplos de código a seguir mostram como usar o GetObject.

CLI
AWS CLI

Para baixar um objeto

O get-object exemplo a seguir baixa um objeto para o endpoint especificado.

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

Saída:

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

Para baixar parte de um objeto

O get-object exemplo a seguir baixa uma parte de um objeto para o endpoint especificado.

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

Saída:

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

Para obter mais informações, consulte Baixar um objeto no Guia do MediaStore usuário do AWS Elemental.

  • Para API obter detalhes, consulte GetObjectna Referência de AWS CLI Comandos.

Java
SDKpara Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

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(); } }
  • Para API obter detalhes, consulte GetObjectem AWS SDK for Java 2.x APIReferência.