不推荐使用 AmazonS3Client (凭据)

我试图阅读亚马逊 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 文件中提供的示例代码,它们是相同的。

63692 次浏览

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

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

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


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

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

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

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

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

AmazonS3ClientBuilder.defaultClient();

方法传递区域信息

com.amazonaws.regions.Region object.


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

在构造函数中只使用创建函数不推荐使用这种方法:

 val awsConfiguration = AWSConfiguration(context)
val awsCreds = CognitoCachingCredentialsProvider(context, awsConfiguration)
val s3Client = AmazonS3Client(awsCreds, Region.getRegion(Regions.EU_CENTRAL_1))
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)

使用 AWS SDK for Java 2.x,还可以像下面这样构建自己的 credentialProvider:

//证书提供者

package com.myproxylib.aws;


import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;


public class CustomCredentialsProvider implements AwsCredentialsProvider {


private final String accessKeyId;
private final String secretAccessKey;


public CustomCredentialsProvider(String accessKeyId, String secretAccessKey) {
this.secretAccessKey = secretAccessKey;
this.accessKeyId = accessKeyId;
}


@Override
public AwsCredentials resolveCredentials() {
return new CustomAwsCredentialsResolver(accessKeyId, secretAccessKey);
}


}


//信号解析器


package com.myproxylib.aws;


import software.amazon.awssdk.auth.credentials.AwsCredentials;


public class CustomAwsCredentialsResolver implements AwsCredentials {


private final String accessKeyId;
private final String secretAccessKey;


CustomAwsCredentialsResolver(String accessKeyId, String secretAccessKey) {
this.secretAccessKey = secretAccessKey;
this.accessKeyId = accessKeyId;
}


@Override
public String accessKeyId() {
return accessKeyId;
}


@Override
public String secretAccessKey() {
return secretAccessKey;
}
}


//提供者的用法


package com.myproxylib.aws.s3;


public class S3Storage implements IS3StorageCapable {


private final S3Client s3Client;


public S3Storage(String accessKeyId, String secretAccessKey, String region) {


this.s3Client = S3Client.builder().credentialsProvider(new CustomCredentialsProvider(accessKeyId, secretAccessKey)).region(of(region)).build();
}

注意:

  1. 当然,库用户可以从任何地方获得凭据,在调用 S3构造函数之前将其解析为 java 属性。

  2. 如果可能的话,请支持其他答案和 doc 中提到的其他方法。 我的用例对此是必要的。