How do I delete a versioned bucket in AWS S3 using the CLI?

前端 未结 20 994
我在风中等你
我在风中等你 2020-12-07 16:34

I have tried both s3cmd:

$ s3cmd -r -f -v del s3://my-versioned-bucket/

And the AWS CLI:

$ aws s3 rm s3://my-v         


        
20条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 16:49

    One way to do it is iterate through the versions and delete them. A bit tricky on the CLI, but as you mentioned Java, that would be more straightforward:

    AmazonS3Client s3 = new AmazonS3Client();
    String bucketName = "deleteversions-"+UUID.randomUUID();
    
    //Creates Bucket
    s3.createBucket(bucketName);
    
    //Enable Versioning
    BucketVersioningConfiguration configuration = new BucketVersioningConfiguration(ENABLED);
    s3.setBucketVersioningConfiguration(new SetBucketVersioningConfigurationRequest(bucketName, configuration ));
    
    //Puts versions
    s3.putObject(bucketName, "some-key",new ByteArrayInputStream("some-bytes".getBytes()), null);
    s3.putObject(bucketName, "some-key",new ByteArrayInputStream("other-bytes".getBytes()), null);
    
    //Removes all versions
    for ( S3VersionSummary version : S3Versions.inBucket(s3, bucketName) ) {
        String key = version.getKey();
        String versionId = version.getVersionId();          
        s3.deleteVersion(bucketName, key, versionId);
    }
    
    //Removes the bucket
    s3.deleteBucket(bucketName);
    System.out.println("Done!");
    

    You can also batch delete calls for efficiency if needed.

提交回复
热议问题