使用 Java2 aws sdk 时如何配置区域?
How to configure Region when using Java2 aws sdk?
我编写了以下控制器来生成 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.AwsProfileRegionProvider@60015ef5: No region provided in profile: default, 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 代码示例都显示了这种设置区域的方式:
我编写了以下控制器来生成 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.AwsProfileRegionProvider@60015ef5: No region provided in profile: default, 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 代码示例都显示了这种设置区域的方式: