AmazonS3Client(凭证)已弃用

我正在尝试读取 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样。


答案 1

您可以使用 AmazonS3ClientBuilderAwsClientBuilder 作为替代方法。

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

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

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

答案 2

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

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

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


推荐