如何在 aws s3 开发工具包 java 2.0 中启用强制全局存储桶访问



这是指向Java 3 sdk 版本 1 的文档的链接。2.0 版是否有类似的东西,或者他们删除了这样的选项?

是的!在 AWS 开发工具包 v2 中,可以在客户端中配置的区域以外的区域上执行 S3 操作。

为此,请在客户端上将useArnRegionEnabled设置为 true。

使用 Scala 的一个例子是:

  val s3Configuration = S3Configuration.builder.useArnRegionEnabled(true).build
  val client = S3Client
    .builder
    .credentialsProvider({$foo})
    .region(Region.EU_WEST_1)
    .overrideConfiguration({$foo})
    .serviceConfiguration(s3Configuration)
    .build

这是文档: https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/S3Configuration.Builder.html#useArnRegionEnabled-java.lang.Boolean-

此处不支持

在版本 1.x 中,Amazon S3、Amazon SNS 和 Amazon SQS 等服务允许跨区域边界访问资源。在 2.x 版中,使用同一客户端不再允许这样做。如果需要访问其他区域中的资源,则必须在该区域中创建客户端,并使用相应的客户端检索资源。

这在使用 java AWS 开发工具包 2.16.98 时对我有用,只需要存储桶的名称而不是完整的 arn。

private S3Client defaultClient;
private S3Client bucketSpecificClient;
private String bucketName = "my-bucket-in-some-region";
// this client seems to be able to look up the location of buckets from any region
defaultClient = S3Client.builder().endpointOverride(URI.create("https://s3.us-east-1.amazonaws.com")).region(Region.US_EAST_1).build();
public S3Client getClient() {
    if (bucketSpecificClient == null) {
        String bucketLocation = defaultClient.getBucketLocation(builder -> builder.bucket(this.bucketName)).locationConstraintAsString();
        Region region = bucketLocation.trim().equals("") ? Region.US_EAST_1 : Region.of(bucketLocation);
        bucketSpecificClient = S3Client.builder().region(region).build();
    }
    return bucketSpecificClient;
}

现在,您可以使用bucketSpecificClient对存储桶中的对象执行操作my-bucket-in-some-region

最新更新