我正在尝试将文件上传到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的类,为清晰起见,已对该行进行了注释。
>>> a = [1,2,3]
>>> b = [3,2,1]
>>> list(map(sum, (zip(*((x>y, y>x) for x,y in zip(a,b))))))
[1, 1]
异常堆栈如下
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);
}
});
} catch (Exception e) {
System.out.println(e.getMessage());
return null;
}
return future;
}
}
我不确定是什么原因造成的,自AWS-SDK 2.0的最新版本以来,文档中几乎没有引用。