java - 上传时如何向 S3AsyncClient 添加凭据

标签 java amazon-web-services amazon-s3

这是异步上传到 S3 的 AWS Java 代码示例:

S3AsyncClient client = S3AsyncClient.create();
CompletableFuture<PutObjectResponse> future = client.putObject(
PutObjectRequest.builder()
.bucket(BUCKET)
.key(KEY)
.build(),
AsyncRequestBody.fromFile(Paths.get("myfile.in"))
);
future.whenComplete((resp, err) -> {
try {
if (resp != null) {
System.out.println("my response: " + resp);
} else {
// Handle error
err.printStackTrace();
}
} finally {
// Lets the application shut down. Only close the client when you are completely done with it.
client.close();
}
});

我让它工作了一段时间,但只是好奇为什么你可以在没有凭据的情况下上传到一个存储桶(已阻止所有公共(public)访问打开)?

然后由于某种原因我的访问 key 被删除,我不得不重新生成一个新的,但现在我得到了

software.amazon.awssdk.services.s3.model.S3Exception: The AWS Access Key Id you provided does not exist in our records. (Service: S3, Status Code: 403, Request ID: 2FC0CEEB338D50CB)

。谢谢。

最佳答案

尝试一下,看看它是否适合您:

        AwsCredentialsProvider creds = StaticCredentialsProvider.create(AwsBasicCredentials.create("my_access_key", "my_secret_key"));
        S3AsyncClient s3Client;
        try {
            s3Client = S3AsyncClient.builder().credentialsProvider(creds)
                    .region(Region.US_WEST_1)
                    .endpointOverride(new URI("https://abc.xyz.com:9021"))
                    .build();
            CompletableFuture<PutObjectResponse> futureGet = s3Client.putObject(
                    PutObjectRequest.builder()
                            .bucket("my_bucket_name")
                            .key("/somepath/anotherpath/myData.pdf")
                            .build(),
                            AsyncRequestBody.fromFile(Paths.get("myfile.in")));
            futureGet.get();
        } catch (URISyntaxException e1) {
            e1.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

关于java - 上传时如何向 S3AsyncClient 添加凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59735104/

相关文章:

php - Ubuntu 服务器上的 Laravel Lumen Mail 和 Amazon SES 500 内部服务器错误(生产)

amazon-web-services - 有没有办法在 cloudformation 模板中添加标签以应用于资源和嵌套堆栈?

amazon-web-services - S3 Glacier 对象未还原

php - 无法使用 return response()->download($file) 从 s3 下载文件;

python - 我应该如何将我的 s3 凭证传递给 AWS 上的 Python lambda 函数?

java - 使用itextpdf转换为PDF时如何使西里尔字符正确显示?

java - 为什么插入数据时小数点会被去掉? SQLite(Android 开发)

java - jSpinner 不显示月份

java - 类文件 javax/validation/ConstraintViolationException 中非 native 或抽象方法中缺少代码属性

amazon-web-services - 发布消息到SNS而不是直接推送到SQS的优点