简体   繁体   中英

IS there a way to use the Android AWS SDK to read file context from Amazon S3 bucket without having to download the file?

从文档中可以看到您可以下载文件,但是我想知道是否有一种方法可以从Amazon S3存储桶中获取文本文件的内容,而不必在本地存储文件?

Yes, you can use getObject() on an instance of AmazonS3 to download a file:

String bucketName = "BUCKET_NAME";
String key = "KEY_OF_OBJECT";

AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(Regions.DEFAULT_REGION)
                    .withCredentials(new ProfileCredentialsProvider())
                    .build();

System.out.println("Downloading an object");
S3Object fullObject = s3Client.getObject(new GetObjectRequest(bucketName, key));
System.out.println("Content-Type: " + fullObject.getObjectMetadata().getContentType());
System.out.println("Content: ");

// fullObject.getObjectContent() is an InputStream 

BufferedReader reader = new BufferedReader(new InputStreamReader(fullObject.getObjectContent()));
String line = null;
while ((line = reader.readLine()) != null) {
    System.out.println(line);
}

If you only need part of the data, you can use the withRange() modifier on GetObjectRequest() to create a range request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM