我正在尝试将文件上传到 ap-southeast-1 区域中的 AWS Bucket,凭证已经过验证并且是正确的。
S3AsyncClientBuilder 为每个区域端点抛出空指针异常。(https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region)
github 上的一个相关线程在签署区域时遇到问题,但这不是问题,一旦指定了适当的区域。
https://github.com/aws/aws-sdk-java-v2/issues/448
这是访问 AWS Bucket 的类,为了清楚起见,该行已被注释。
public class ObjectUploaderInterfaceImpl implements ObjectUploaderInterface {
private String AWS_ACCESS_KEY;
private String AWS_SECRET_KEY;
private S3AsyncClient s3Client; // Asynchronous Client for Non Blocking IO
private AwsCredentialsProvider credentials;
public ObjectUploaderInterfaceImpl(String awsAccessKey, String awsSecretKey) {
this.AWS_ACCESS_KEY = awsAccessKey;
this.AWS_SECRET_KEY = awsSecretKey;
this.credentials = new StaticCredentialsProvider(
new AwsCredentials(this.AWS_ACCESS_KEY, this.AWS_SECRET_KEY));
try {
this.s3Client = S3AsyncClient.builder()
.credentialsProvider(this.credentials)
.region(Region.AP_SOUTHEAST_1)
.endpointOverride(new URI("s3-ap-southeast-1.amazonaws.com"))
.build(); // Throws Null pointer Exception
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public CompletableFuture<PutObjectResponse> uploadObject(String filePath, String s3Bucket,
String s3BucketPath) {
CompletableFuture<PutObjectResponse> future;
try {
future = s3Client.putObject(
PutObjectRequest.builder()
.bucket(s3Bucket)
.key(s3BucketPath)
.build(),
AsyncRequestProvider.fromFile(Paths.get(filePath))
);
future.whenComplete((resp, err) -> {
if (resp != null) {
System.out.println("Response from Server : " + resp);
} else {
err.printStackTrace();
}
try {
s3Client.close();
} catch (Exception e) {
System.out.println(e);
}
});
}
}
}
我不确定是什么原因导致这种情况,自 AWS-SDK 2.0 最近发布以来,文档中提供了一些参考。
相关分类