amazon-web-services - 如何获取 Amazon S3 对象的最新版本 ID?

标签 amazon-web-services amazon-s3 aws-sdk

给定有效的 Amazon S3 路径(指定要获取版本 ID 的存储桶、文件),我如何使用 AWS 开发工具包 JAVA API 获取最新版本 ID?

我尝试在 https://sdk.amazonaws.com/java/api/latest/ 中搜索 API ,但没有找到返回VersionID的API。

函数签名为String getLatestVersionID(String S3Path);

谢谢

最佳答案

您可以通过调用GetObjectRequest objects 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]
    }
}

关于amazon-web-services - 如何获取 Amazon S3 对象的最新版本 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67926937/

相关文章:

amazon-web-services - AWS .Net API - 提供的 token 已过期

java - Dynamodb : UpdateItemSpec : Multiple Update Expression - Not Working

http - CloudFront 签名 URL 抛出 403 禁止错误

amazon-web-services - 写入 S3Object : The file indicated by the FilePath property does not exist

javascript - ionic 图片到 s3

amazon-s3 - Amazon CloudFront 不遵守我的 S3 网站存储桶的 index.html 规则

php - Laravel : unable to get local issuer certificate 中的 AWL SSL 错误

amazon-web-services - AWS 上的 Websocket 连接总是导致 TOO MANY REQUESTS 即使是一个请求

amazon-web-services - AWS CodeDeploy 在通过 AutoScaling 部署时覆盖现有文件

javascript - 字符串数组架构的 DynamoDB 数组