Not setting the S3 bucket owner condition might introduce a risk of accidentally using a wrong bucket. For example, a configuration error could lead to accidentally writing production data into test accounts.
1public void putObjectNoncompliant() {
2 S3Client s3Client = S3Client.create();
3 // Noncompliant: the account that owns the bucket is not specified in the request.
4 PutObjectRequest request = PutObjectRequest.builder()
5 .bucket("PUT-EXAMPLE-BUCKET")
6 .key("example-key")
7 .build();
8 Path path = Paths.get("put_file.txt");
9 s3Client.putObject(request, path);
10}
1public void putObjectCompliant() {
2 S3Client s3Client = S3Client.create();
3 // Compliant: the account that owns the bucket is specified in the request.
4 PutObjectRequest request = PutObjectRequest.builder()
5 .bucket("PUT-EXAMPLE-BUCKET")
6 .key("example-key")
7 .expectedBucketOwner("111122223333")
8 .build();
9 Path path = Paths.get("put_file.txt");
10 s3Client.putObject(request, path);
11}