我正在嘗試將文件上傳到 ap-southeast-1 區(qū)域中的 AWS Bucket,憑證已經(jīng)過驗(yàn)證并且是正確的。S3AsyncClientBuilder 為每個(gè)區(qū)域端點(diǎn)拋出空指針異常。(https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region)github 上的一個(gè)相關(guān)線程在簽署區(qū)域時(shí)遇到問題,但這不是問題,一旦指定了適當(dāng)?shù)膮^(qū)域。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); } }); } }}我不確定是什么原因?qū)е逻@種情況,自 AWS-SDK 2.0 最近發(fā)布以來,文檔中提供了一些參考。
添加回答
舉報(bào)
0/150
提交
取消