amazon-web-services - 如何获取 Amazon S3 对象的最新版本 ID?
问题描述
给定一个有效的 Amazon S3 路径,该路径指定哪个存储桶、哪个文件获取版本 ID,我如何使用 AWS SDK JAVA API 获取最新版本 ID?
我尝试在https://sdk.amazonaws.com/java/api/latest/中搜索 API ,但没有找到返回 VersionID 的 API。
函数签名是String getLatestVersionID(String S3Path);
谢谢
解决方案
您可以通过调用GetObjectRequest 对象的 versionId方法来设置版本 ID 值。这是此的代码:
package com.example.s3;
// snippet-start:[s3.java2.getobjectdata.import]
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;
// snippet-end:[s3.java2.getobjectdata.import]
/**
* 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>\n\n" +
"Where:\n" +
" bucketName - the Amazon S3 bucket name. \n\n"+
" keyName - the key name. \n\n"+
" path - the path where the file is written to. \n\n";
if (args.length != 3) {
System.out.println(USAGE);
System.exit(1);
}
String bucketName = args[0];
String keyName = args[1];
String path = args[2];
Region region = Region.US_EAST_1;
S3Client s3 = S3Client.builder()
.region(region)
.build();
getObjectBytes(s3,bucketName,keyName, path);
s3.close();
}
// snippet-start:[s3.java2.getobjectdata.main]
public static void getObjectBytes (S3Client s3, String bucketName, String keyName, String path ) {
try {
GetObjectRequest objectRequest = GetObjectRequest
.builder()
.key(keyName)
.versionId("<Set Version ID>")
.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);
}
// snippet-end:[s3.java2.getobjectdata.main]
}
}
推荐阅读
- java - Java Spring boot Mongodb 花费太多时间来检索结果
- jquery - 添加元素而不影响其他元素
- javascript - 我可以使用 javascript 使用 forEach 实现异步等待吗?
- visual-studio - 为什么 Visual Studio 生成的 WSDL 未指定 soap:operation 元素(缺少 wsdl:binding 元素)
- laravel - Laravel 控制器:覆盖父请求验证
- django - 如何自动更改 BooleanField 的值
- android - Android 上 NFC 标签的 API 文档和限制
- python - 从networkx中的有向图中删除一个节点,同时保留子节点并重新映射它们的边缘
- ios - Facebook 登录的自定义 URL 方案 (fb(appID)://) 说“Safari 无法打开页面,因为地址无效”即使在编辑 info.plist 之后也是如此
- r - 替换字符串