如何使用AWS SDK for Java V2从Amazon S3桶中读取文件



我能够使用AmazonS3Client读取文件,但现在我试图使用软件包软件。amazon.awssdk.services.s3. s3client读取文件。与com. amazon.com .services不同,我没有找到将文件名传递给GetObjectRequest构造的选项。s3包。下面是我的示例代码。

import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
import software.amazon.awssdk.services.s3.model.S3Object;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
main(String[] args){
String bucketName = "some-name-s3";
Region region = Region.US_EAST_1;
S3Client s3client = S3Client
.builder()
.region(region)
.build();
ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucketName).build();
ListObjectsV2Response listObjectsV2 = s3client.listObjectsV2(listObjectsV2Request);
List<S3Object> s3Objects = listObjectsV2.contents();
}

现在如何读取每个文件并处理来自s3Objects的流。

使用AWS SDK for Java V2从Amazon S3 bucket读取对象时使用了错误的逻辑。你把列表称为bucket。你可以通过调用listObjectsV2来获取每个对象的元数据。例如,您可以调用S3Object的键()方法获取密钥名称。

现在要从Amazon S3桶中读取对象,需要桶名和密钥名,然后调用getObjectAsBytes,如以下Java逻辑所示,该逻辑显示了如何读取PDF文档并将其写入本地路径:

package com.example.s3;
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
* To run this AWS code example, ensure that you have setup your development environment, including your AWS credentials.
*
* For information, see this documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class GetObjectData {
public static void main(String[] args) {
final String USAGE = "n" +
"Usage:n" +
"    GetObjectData <bucketName> <keyName> <path>nn" +
"Where:n" +
"    bucketName - the Amazon S3 bucket name. nn"+
"    keyName - the key name. nn"+
"    path - the path where the file is written to. nn";
if (args.length != 3) {
System.out.println(USAGE);
System.exit(1);
}
String bucketName = "myBucket";
String keyName = "book.pdf";
String path = "C:/AWS/book.pdf";
Region region = Region.US_EAST_1;
S3Client s3 = S3Client.builder()
.region(region)
.build();
getObjectBytes(s3,bucketName,keyName, path);
s3.close();
}

public static void getObjectBytes (S3Client s3, String bucketName, String keyName, String path ) {
try {
GetObjectRequest objectRequest = GetObjectRequest
.builder()
.key(keyName)
.bucket(bucketName)
.build();
ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
byte[] data = objectBytes.asByteArray();
// Write the data to a local file
File myFile = new File(path );
OutputStream os = new FileOutputStream(myFile);
os.write(data);
System.out.println("Successfully obtained bytes from an S3 object");
os.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
} 

在Github中找到这个示例和许多其他Amazon S3 Java V2代码示例:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/example_code/s3

相关内容

  • 没有找到相关文章

最新更新