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

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

问题描述

s3cmd 我都试过了:

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

还有 AWS CLI:

And the 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天全站免登陆