繁体   English   中英

有没有一种方法可以使用Android AWS SDK从Amazon S3存储桶读取文件上下文,而不必下载文件?

[英]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存储桶中获取文本文件的内容,而不必在本地存储文件?

是的,您可以在AmazonS3的实例上使用getObject()下载文件:

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);
}

如果只需要部分数据,则可以在GetObjectRequest()上使用withRange()修饰符来创建范围请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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