如何使用CLI在AWS S3中删除版本存储桶? [英] How do I delete a versioned bucket in AWS S3 using the CLI?

查看:606
本文介绍了如何使用CLI在AWS S3中删除版本存储桶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我都尝试过s3cmd:

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

AWS CLI:

$ aws s3 rm s3://my-versioned-bucket/ --recursive

但这两个命令都只是将DELETE标记添加到S3.用于删除存储桶的命令也不起作用(从AWS CLI):

But both of these commands simply add DELETE markers to S3. The command for removing a bucket also doesn't work (from the AWS CLI):

$ aws s3 rb s3://my-versioned-bucket/ --force
Cleaning up. Please wait...
Completed 1 part(s) with ... file(s) remaining
remove_bucket failed: s3://my-versioned-bucket/ A client error (BucketNotEmpty) occurred when calling the DeleteBucket operation: The bucket you tried to delete is not empty. You must delete all versions in the bucket.

好吧...怎么样?为此,其文档中没有任何信息. S3Cmd表示这是一个功能齐全"的S3命令行工具,但是除了其自身版本外,它未引用任何版本.是否有任何方法可以不使用Web界面来完成此操作,这将需要很长时间,并且需要我保持笔记本电脑处于打开状态?

Ok... how? There's no information in their documentation for this. S3Cmd says it's a 'fully-featured' S3 command-line tool, but it makes no reference to versions other than its own. Is there any way to do this without using the web interface, which will take forever and requires me to keep my laptop on?

推荐答案

一种实现方法是遍历各个版本并将其删除.在CLI上有些棘手,但是就像您提到的Java一样,这会更简单:

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.

这篇关于如何使用CLI在AWS S3中删除版本存储桶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆