何もしないとawscliのdefaultのものが利用される。
Region region = Region.AP_NORTHEAST_1; S3Client s3 = S3Client.builder().region(region).build();
この記載だとdefaultが使われる。s3が使いたい場合はAWS_PROFILE=s3に設定して実行
[default] aws_access_key_id = hogehoge aws_secret_access_key = fugafuga [s3] aws_access_key_id = hogehoge aws_secret_access_key = fugafuga
直接設定する場合は以下のように記載
AwsCredentialsProvider creds = StaticCredentialsProvider.create(AwsBasicCredentials.create("AWS KEY", "AWS PASSWORD")); S3Client s3 = S3Client.builder().credentialsProvider(creds).region(region).build();
タイムアウトの設定も別で
ClientOverrideConfiguration clientOverrideConfiguration = ClientOverrideConfiguration .builder() .apiCallTimeout(props.getApiCallTimeout()) .apiCallAttemptTimeout(props.getApiCallAttemptTimeout()) .build();
S3Client s3 = S3Client .builder() .overrideConfiguration(clientOverrideConfiguration) .region(Region.AP_NORTHEAST_1).build();
Putもめんどくさい
PutObjectRequest request = PutObjectRequest.builder().bucket("").key("").build(); RequestBody requestBody = RequestBody.fromFile(new File(""));
s3Client.putObject(request,requestBody);
Exception in thread "main" software.amazon.awssdk.services.s3.model.S3Exception: Access Denied (Service: S3, Status Code: 403, Request ID: 4E7A2E3296DB04C2)