Hello,
The following Java code is meant to get the full name of the first found S3 bucket which name starts with "mys3" and, if none exists, then to create a new one and return its name.
...
AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard().build();
...
this.bucketName = amazonS3.listBuckets().stream().filter(b -> b.getName().startsWith("mys3")).findFirst().orElse(amazonS3.createBucket("mys3" + RANDOM)).getName();
...
it raises the following exception:
com.amazonaws.services.s3.model.AmazonS3Exception: Your previous request to create the named bucket succeeded and you already own it. (Service: Amazon S3; Status Code: 409; Error Code: BucketAlreadyOwnedByYou; Request ID: D8BV5DZVYFE5P9QA; S3 Extended Request ID: 27GbfEzSb0h3/taU5jB2/DM1vu4Te2mM5GdbgudbpkrJUlKmaDpffGXe1iaPTiGxtU4gjVzhSBU=; Proxy: null), S3 Extended Request ID: 27GbfEzSb0h3/taU5jB2DM1vu4Te2mM5GdbgudbpkrJUlKmaDpffGXe1iaPTiGxtU4gjVzhSBU=
Not sure why this exception is raised as nothing seems to be wrong with the code above and googling for a while, I didn't find any pertinent solution. Then, I replaced the code above by the equivalent one, as follows:
...
AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard().build();
Optional<Bucket> optionalBucket = amazonS3.listBuckets().stream().filter(b -> b.getName().startsWith("mys3")).findFirst();
if (optionalBucket.isPresent())
this.s3BucketName = optionalBucket.get().getName();
else
this.s3BucketName = amazonS3.createBucket("mys3" + RANDOM).getName();
...
This time everything works as expected and no exception is raised. Then I tried a 3rd version of the code, also equivalent to the first one:
...
AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard().build();
Optional<Bucket> optionalBucket = amazonS3.listBuckets().stream().filter(b -> b.getName().startsWith("mys3")).findFirst();
this.s3BucketName = optionalBucket.isPresent() ? optionalBucket.get().getName() : amazonS3.createBucket("mys3" + RANDOM).getName();
...
It raised the same exception as previously. So, am I right to say that the AWS Java SDK behave inconsistently as it works differently for different version of equivalent Java code ?