Amazon web services 如何从S3 bucket中的文件夹中删除带有后缀的图像
我在s3上存储了多个大小的图像。 e、 g.图像100_100、图像200_200、图像300_150Amazon web services 如何从S3 bucket中的文件夹中删除带有后缀的图像,amazon-web-services,amazon-s3,boto3,Amazon Web Services,Amazon S3,Boto3,我在s3上存储了多个大小的图像。 e、 g.图像100_100、图像200_200、图像300_150 我想从文件夹中删除特定大小的图像,如后缀为200_200的图像。此文件夹中有很多图像,因此如何删除这些图像?最简单的方法是编写Python脚本,类似于: import boto3 BUCKET = 'my-bucket' PREFIX = '' # eg 'images/' s3_client = boto3.client('s3', region_name='ap-southeast-2
我想从文件夹中删除特定大小的图像,如后缀为200_200的图像。此文件夹中有很多图像,因此如何删除这些图像?最简单的方法是编写Python脚本,类似于:
import boto3
BUCKET = 'my-bucket'
PREFIX = '' # eg 'images/'
s3_client = boto3.client('s3', region_name='ap-southeast-2')
# Get a list of objects
list_response = s3_client.list_objects_v2(Bucket = BUCKET, Prefix = PREFIX)
while True:
# Find desired objects to delete
objects = [{'Key':object['Key']} for object in list_response['Contents'] if object['Key'].endswith('200_200')]
print ('Deleting:', objects)
# Delete objects
if len(objects) > 0:
delete_response = s3_client.delete_objects(
Bucket=BUCKET,
Delete={'Objects': objects}
)
# Next page
if list_response['IsTruncated']:
list_response = s3_client.list_objects_v2(
Bucket = BUCKET,
Prefix = PREFIX,
ContinuationToken=list_reponse['NextContinuationToken'])
else:
break
使用AWS命令行界面(AWS CLI):
我们首先排除所有内容,然后包括需要删除的内容。这是一个模拟Linux中
rm-r“*200_200”
命令行为的变通方法。好的,通过什么?lambda函数?如果是,用什么语言(Python、Java、Go等)?谢谢,只需添加扩展名“200x200.jpeg”即可
aws s3 rm s3://Path/To/Dir/ --recursive --exclude "*" --include "*200_200"