不推荐使用AmazonS3Client(凭据)

时间:2017-01-31 07:31:37

标签: java amazon-web-services amazon-s3 aws-sdk deprecated

我正在尝试阅读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文件中提供的示例代码,它是一样的。

6 个答案:

答案 0 :(得分:103)

您可以使用AmazonS3ClientBuilderAwsClientBuilder作为替代品。

对于S3,最简单的将是AmazonS3ClientBuilder,

BasicAWSCredentials creds = new BasicAWSCredentials("access_key", "secret_key"); 
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).build();

答案 1 :(得分:10)

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

$(function() {
  var url = "https://example.com?abc=xyz";
  $.ajax({
    type : "GET",
    contentType : "text/plain",
    url : url + "&callback=?",
    dataType : 'jsonp',
    xhrFields : {
        withCredentials : false
    },
    headers : {},
    success : function() {
        console.log("success");
    },
    error : function() {
        console.log("error");
    },
    complete : function() {
        console.log("complete");
    }
  });
});

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

答案 2 :(得分:2)

在构造函数中仅弃用了凭证,您可以使用类似以下的方法:

 val awsConfiguration = AWSConfiguration(context)
 val awsCreds = CognitoCachingCredentialsProvider(context, awsConfiguration)
 val s3Client = AmazonS3Client(awsCreds, Region.getRegion(Regions.EU_CENTRAL_1))

答案 3 :(得分:1)

您可以按如下方式创建S3默认客户端(使用aws-java-sdk-s3-1.11.232):

<p>
<strong>Telephone:</strong>
 1(111)656-3485
<br/>

答案 4 :(得分:0)

您需要通过

传递区域信息
com.amazonaws.regions.Region object.

Use AmazonS3Client(credentials, Region.getRegion(Regions.REPLACE_WITH_YOUR_REGION))

答案 5 :(得分:0)

implementation 'com.amazonaws:aws-android-sdk-s3:2.16.12'

val key = "XXX"
val secret = "XXX"
val credentials = BasicAWSCredentials(key, secret)
val s3 = AmazonS3Client(
    credentials, com.amazonaws.regions.Region.getRegion(
        Regions.US_EAST_2
    )
)
val expires = Date(Date().time + 1000 * 60 * 60)

val keyFile = "13/thumbnail_800x600_13_photo.jpeg"
val generatePresignedUrlRequest = GeneratePresignedUrlRequest(
    "bucket_name",
    keyFile
)
generatePresignedUrlRequest.expiration = expires
val url: URL = s3.generatePresignedUrl(generatePresignedUrlRequest)

GlideApp.with(this)
    .load(url.toString())
    .apply(RequestOptions.centerCropTransform())
    .into(image)