Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon s3 如何一次删除多个S3存储桶?_Amazon S3 - Fatal编程技术网

Amazon s3 如何一次删除多个S3存储桶?

Amazon s3 如何一次删除多个S3存储桶?,amazon-s3,Amazon S3,我想在AWS S3中删除十几个bucket,它们都有一个类似的名称,包含要删除的bucket,其中包含一些对象 使用UI的速度非常慢,是否有一种解决方案可以使用CLI快速删除所有这些bucket?使用bot3您无法删除包含对象的bucket,因此您首先需要在删除bucket之前删除对象。最简单的解决方案是一个简单的Python脚本,例如: import boto3 import botocore import json s3_client = boto3.client( "s3",

我想在AWS S3中删除十几个bucket,它们都有一个类似的名称,包含要删除的
bucket
,其中包含一些对象


使用UI的速度非常慢,是否有一种解决方案可以使用CLI快速删除所有这些bucket?

使用bot3您无法删除包含对象的bucket,因此您首先需要在删除bucket之前删除对象。最简单的解决方案是一个简单的Python脚本,例如:

import boto3
import botocore
import json

s3_client = boto3.client(
    "s3",
    aws_access_key_id="<your key id>",
    aws_secret_access_key="<your secret access key>"
)

response = s3_client.list_buckets()
for bucket in response["Buckets"]:
    # Only removes the buckets with the name you want.
    if "bucket-to-remove" in bucket["Name"]:
        s3_objects = s3_client.list_objects_v2(Bucket=bucket["Name"])
        # Deletes the objects in the bucket before deleting the bucket.
        if "Contents" in s3_objects:
            for s3_obj in s3_objects["Contents"]:
                rm_obj = s3_client.delete_object(
                    Bucket=bucket["Name"], Key=s3_obj["Key"])
                print(rm_obj)
        rm_bucket = s3_client.delete_bucket(Bucket=bucket["Name"])
        print(rm_bucket)
导入boto3
导入botocore
导入json
s3_client=boto3.client(
“s3”,
aws\u访问密钥\u id=“”,
aws_secret_access_key=“”
)
response=s3\u client.list\u bucket()
对于响应中的桶[“桶”]:
#仅删除具有所需名称的存储桶。
如果bucket[“Name”]中的“bucket to remove”:
s3_objects=s3_client.list_objects_v2(Bucket=Bucket[“Name”])
#删除存储桶之前,先删除存储桶中的对象。
如果s3_对象中的“内容”:
对于s3_对象[“内容”]中的s3_对象:
rm_obj=s3_client.delete_对象(
Bucket=Bucket[“Name”],Key=s3_obj[“Key”])
打印(rm_obj)
rm_bucket=s3_客户端。删除_bucket(bucket=bucket[“Name”])
打印(rm_桶)
根据,仅当存储桶未启用版本控制时,才可以使用CLI命令
aws s3 rb
删除存储桶。如果是这种情况,您可以编写一个简单的bash脚本来获取bucket名称并逐个删除它们,如:

#/bin/bash
#get bucket list=>返回时间戳+由行分隔的bucket名称
S3LS=“$(aws s3 ls | grep‘桶名模式’)”
#将线拆分为一个数组@看见https://stackoverflow.com/a/13196466/6569593
oldIFS=“$IFS”
如果
'
IFS=${IFS:0:1}
行=($S3LS)
IFS=“$oldIFS”
对于“${lines[@]}”中的行
做
BUCKET_NAME=${line:20:${{line}}}删除时间戳
aws s3 rb“s3://${BUCKET_NAME}”--强制
完成

小心不要移走重要的桶!我建议在实际删除它们之前输出每个bucket名称。还要注意的是,
awss3rb
命令需要一段时间才能运行,因为它会递归删除bucket中的所有对象

您可以尝试使用此采样行一次删除所有内容。记住这是极具破坏性的,所以我希望你知道你在做什么:

for bucket in $(aws s3 ls | awk '{print $3}' | grep my-bucket-pattern); do  aws s3 rb "s3://${bucket}" --force ; done

你受够了。可能需要一些时间,具体取决于bucket的数量及其内容。

批量删除S3 bucket最简单的方法是根本不编写任何代码。我过去常常浏览我的S3帐户,很容易地删除存储桶及其内容。

我这样做了

aws s3api list-buckets \
   --query 'Buckets[?starts_with(Name, `bucket-pattern `) == `true`].[Name]' \
   --output text | xargs -I {} aws s3 rb s3://{} --force
然后根据需要更新铲斗模式


不过要小心,这是一个相当危险的操作

这是一个windows解决方案

在删除之前,首先测试过滤器

aws s3 ls ^| findstr "<search here>"
aws s3 ls^|findstr“”
然后执行

for /f "tokens=3" %a in ('<copy the correct command between the quotes>') do aws s3 rb s3://%a --force
for/f“tokens=3”%a in(“”)do aws s3 rb s3://%a--force

@ArmandGrillet您应该将list objects Responses中的对象聚合为多个集合,并使用multi-delete API使其更快。另外,不要把这个脚本放在小孩可以找到的地方。这可能会把事情搞得一团糟提醒读者,您可能需要在
my bucket pattern
周围添加引号。这实际上是一个很好的解决方案,因为它不涉及代码,而且比AWS控制台UI更好