使用 Java2 aws sdk 时如何配置 Region?

问题描述

我编写了以下控制器来生成 PreSigned S3 上传链接

case class S3Controller(private val s3Config: S3Config,private val awsConfig: AwsConfig) {
  val URL_TIMEOUT_IN_MILLIS: Long = 60 * 5 * 1000

  def getPreSignedURLForUpload(keyName: String): String = {
    val preSigner: S3Presigner = DefaultS3Presigner.builder()
      .serviceConfiguration(s3Config.s3Configuration)
      .credentialsProvider(awsConfig.awsCredentialsProvider).build()

    val objectRequest: PutObjectRequest = PutObjectRequest.builder()
      .bucket(s3Config.bucketName)
      .key(keyName)
      .contentType("text/plain")
      .build()

    val preSignRequest: PutObjectPresignRequest = PutObjectPresignRequest.builder()
      .signatureDuration(Duration.ofMinutes(10))
      .putObjectRequest(objectRequest)
      .build()

    val preSignedRequest: PresignedPutObjectRequest = preSigner.presignPutObject(preSignRequest)


    val myURL: String = preSignedRequest.url().toString

    myURL
  }
}

我使用的配置对象

case class S3Config (
                      bucketName: String,s3Configuration: S3Configuration
                    )
case class AwsConfig (
                       awsCredentialsProvider: AwsCredentialsProvider
                     )

我尝试使用以下代码对其进行测试

  test("S3 Controller"){
    val s3Configuration: S3Configuration = S3Configuration.builder()
      .pathStyleAccessEnabled(true).build()

    val s3Config: S3Config = S3Config(
      bucketName = "ccc",s3Configuration = s3Configuration
    )

    val awsCredentials: AwsCredentials = AwsBasicCredentials.create("aaa","bbb")
    val awsCredentialsProvider: AwsCredentialsProvider = AwsCredentialsProviderChain.of(StaticCredentialsProvider.create(awsCredentials))
    
    val awsConfig: AwsConfig = AwsConfig(awsCredentialsProvider = awsCredentialsProvider)

    val s3Controller: S3Controller = S3Controller(s3Config,awsConfig)

    s3Controller.getPreSignedURLForUpload("ab")
  }

此测试失败并显示消息

Unable to load region from any of the providers in the chain software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain@4e5ed836: [software.amazon.awssdk.regions.providers.SystemSettingsRegionProvider@5f8edcc5: Unable to load region from system settings. Region must be specified either via environment variable (AWS_REGION) or  system property (aws.region).,software.amazon.awssdk.regions.providers.AwsProfileRegionProvider@60015ef5: No region provided in profile: default,software.amazon.awssdk.regions.providers.InstanceProfileRegionProvider@2ab4bc72: Unable to contact EC2 Metadata service.]
software.amazon.awssdk.core.exception.SdkClientException: Unable to load region from any of the providers in the chain software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain@4e5ed836: [software.amazon.awssdk.regions.providers.SystemSettingsRegionProvider@5f8edcc5: Unable to load region from system settings. Region must be specified either via environment variable (AWS_REGION) or  system property (aws.region).,software.amazon.awssdk.regions.providers.InstanceProfileRegionProvider@2ab4bc72: Unable to contact EC2 Metadata service.]

我知道这是因为我没有在任何地方配置区域。
配置Region的所有方式都是通过环境变量或配置文件

有没有办法以编程方式配置区域?

解决方法

您在使用 builder() 声明服务对象时设置区域:

Region region = Region.US_EAST_1;
S3Presigner presigner = S3Presigner.builder()
                .region(region)
                .build();

所有 Java V2 代码示例都展示了这种设置区域的方式:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/example_code/s3/src/main/java/com/example/s3