AmazonS3Client(credentials) 已弃用

新手上路,请多包涵

我正在尝试阅读 Amazon S3 上可用的文件,因为问题解释了问题。我找不到对已弃用的构造函数的替代调用。

这是代码:

 private String AccessKeyID = "xxxxxxxxxxxxxxxxxxxx";
private String SecretAccessKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private static String bucketName     = "documentcontainer";
private static String keyName     = "test";
//private static String uploadFileName    = "/PATH TO FILE WHICH WANT TO UPLOAD/abc.txt";

AWSCredentials credentials = new BasicAWSCredentials(AccessKeyID, SecretAccessKey);

void downloadfile() throws IOException
{

    // Problem lies here - AmazonS3Client is deprecated
    AmazonS3 s3client = new AmazonS3Client(credentials);
        try {
        System.out.println("Downloading an object...");
        S3Object s3object = s3client.getObject(new GetObjectRequest(
                bucketName, keyName));
        System.out.println("Content-Type: "  +
                s3object.getObjectMetadata().getContentType());
        InputStream input = s3object.getObjectContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        while (true) {
            String line = reader.readLine();
            if (line == null) break;

            System.out.println("    " + line);
        }
        System.out.println();
    } catch (AmazonServiceException ase) {
          //do something
    } catch (AmazonClientException ace) {
        // do something
    }
 }

有什么帮助吗?如果需要更多解释,请提及。我查看了SDK的.zip文件中提供的示例代码,是一样的。

原文由 Asif Ali 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

您可以使用 AmazonS3ClientBuilderAwsClientBuilder 作为备选方案。

对于 S3,最简单的是使用 AmazonS3ClientBuilder

 BasicAWSCredentials creds = new BasicAWSCredentials("access_key", "secret_key");

AmazonS3 s3Client = AmazonS3ClientBuilder
    .standard()
    .withCredentials(new AWSStaticCredentialsProvider(creds))
    .build();

原文由 franklinsijo 发布,翻译遵循 CC BY-SA 4.0 许可协议

使用下面列出的代码创建没有凭据的 S3 客户端:

 AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();

一个用法示例是调用 S3 的 lambda 函数。

原文由 Sid 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题