我是 AWS 新手,我需要为 s3 存储桶中现有的对象设置存储类别。如何使用 java SDK 更改 S3 中对象的存储类别
在任何情况下都不需要使用已弃用的方法。 就我而言,需要更新现有对象:
AmazonS3Client s3Client = (AmazonS3Client)AmazonS3ClientBuilder.standard()
.withRegion(clientRegion)
.withCredentials(new ProfileCredentialsProvider())
.build();
CopyObjectRequest copyRequest = new CopyObjectRequest(sourceBucketName, sourceKey, destinationBucketName, destinationKey)
.withStorageClass(StorageClass.ReducedRedundancy);
s3Client.copyObject(copyRequest);
使用 changeObjectStorageClass看起来非常简单:
AmazonS3Client s3Client = (AmazonS3Client)AmazonS3ClientBuilder.standard()
.withRegion(clientRegion)
.withCredentials(new ProfileCredentialsProvider())
.build();
PutObjectRequest request = new PutObjectRequest(bucketName,
fileObjKeyName, new File(fileName));
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("binary/octet-stream");
request.setMetadata(metadata);
s3Client.putObject(request);
s3Client.changeObjectStorageClass(bucketName, fileObjKeyName,
StorageClass.ReducedRedundancy );
唯一奇怪的部分是您需要在
changeObjectStorageClass
的实例上使用 AmazonS3Client
- 接口 AmazonS3
上的版本已弃用。
以下是更新后的 AWS for Java V2 响应。此代码将对象放置在 Glacier Deep Archive 存储类中。
public static String copyBucketObject (S3Client s3, String fromBucket, String objectKey, String toBucket) {
CopyObjectRequest copyReq = CopyObjectRequest.builder()
.sourceBucket(fromBucket)
.sourceKey(objectKey)
.storageClass("DEEP_ARCHIVE")
.destinationBucket(toBucket)
.destinationKey(objectKey)
.build();
try {
CopyObjectResponse copyRes = s3.copyObject(copyReq);
return copyRes.copyObjectResult().toString();
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return "";
}