Unable to get the Versioning Status of a S3 bucket created programmatically for mock S3 bucket using Java

Viewed 16

I have created a S3 mock service in my code base.

// Create a S3 Client

    S3Client client = S3Client.builder()
                    .serviceConfiguration(configuration)
                    .credentialsProvider(AnonymousCredentialsProvider.create())
                    .region(Region.of("us-west-2"))
                    .endpointOverride(new URI("http://localhost:8001"))
                    .build();

// Create a S3 bucket with a bucket name i.e.

    CreateBucketResponse createBucketResponse = client.createBucket(CreateBucketRequest.builder().
                                                bucket(<BucketName>).build());

// Verify if the bucket has been created or not. // If the bucket is not created then the following lines will throw no such bucket exception.

    HeadBucketRequest headBucketRequest = HeadBucketRequest.builder()
                                              .bucket(**<BucketName>**)
                                              .build();
    
    HeadBucketResponse headBucketResponse = client.headBucket(headBucketRequest);

// Update the Versioning status of the created bucket to Enabled.

   PutBucketVersioningRequest versioningRequest = PutBucketVersioningRequest.builder().bucket(**<BucketName>**)
                                                      .versioningConfiguration(
VersioningConfiguration.builder().status(BucketVersioningStatus.ENABLED).build()
    ).build();

   PutBucketVersioningResponse result = client.putBucketVersioning(versioningRequest);

// Check if the bucket versioning status is enabled or not.

   BucketVersioningStatus bucketVersioningStatus = client.getBucketVersioning(GetBucketVersioningRequest.builder().bucket(**<BucketName>**).build()).status();

Note : The bucketVersioningStatus is null/empty for the above call. In the real time, when I am creating the bucket in the cloud and setting the versioning status gives me proper results but in the mock s3 client I am not getting the same expected outcome.

I am not sure if I have used the PutBucketVersioningRequest appropriately.

NOTE: I am using the S3 Client V2 and not the AmazonS3Client

Kindly help me in identifying the root cause of the issue.

0 Answers
Related