如何使用 python 按特定大小过滤和列出 s3 文件夹中的所有对象
How to filter and list all objects in s3 folder by a certain size using python
我正在尝试获取 s3 存储桶的文件夹中指定大小的所有文件。如何遍历存储桶并按指定大小过滤文件?我也想 return 那些大小正确的文件名。
s3 = boto3.client('s3')
s3.list_objects_v2(Bucket = 'my-images')
样本输出是
u'Key': u'detail/01018535.jpg',
u'LastModified': datetime.datetime(2019, 1, 23, 0, 48, 41, tzinfo=tzlocal()),
u'Size': 13535,
u'StorageClass': 'STANDARD'},
{u'ETag': '"cd65991a1c6f118e8b036208a30028a7"',
u'Key': u'detail/0119AF2.jpg',
u'LastModified': datetime.datetime(2019, 1, 10, 17, 17, tzinfo=tzlocal()),
u'Size': 12984,
u'StorageClass': 'STANDARD'}
例如,假设我想要搜索大小为 12984 的内容。
然后它将 return 'Key'
您可以使用 --query
表达式:
aws s3api list-objects-v2 --bucket my-images --query 'Contents[?Size==`12984`].[Key]' --output text
我将 [Key]
放在方括号中,以强制每个出现在单独的行上。
此语法适用于 Mac 命令行。 Windows 可能需要不同的引号。
有关使用此类表达式的提示,请参阅:JMESPath Tutorial
如果您想使用 boto3,我会使用此函数来查找零字节对象。您可以通过过滤特定尺寸
来根据您的需要进行调整
import boto3
def get_empty_objects(bucket_name, prefixes):
"""
get list of objects from a given s3 prefix recursively
"""
results = []
for prefix in prefixes:
s3client = boto3.client('s3')
paginator = s3client.get_paginator("list_objects_v2")
paginator_result = paginator.paginate(
Bucket=bucket_name, Prefix=prefix)
try:
for object in paginator_result.search('Contents'):
if object['Size'] == 0:
results.append("s3://" + bucket_name + "/" + object['Key'])
except Exception as err:
print(">>> Error processing objects of [s3://" + bucket_name +
"/" + prefix + "] - " + str(err))
print(">>> Returning " + str(len(results)) + " objects for [s3://" + bucket_name + "/" + prefix + "]")
return results
用法:
get_empty_objects("mybucket", ["prefix1/", "prefix2/"])
我正在尝试获取 s3 存储桶的文件夹中指定大小的所有文件。如何遍历存储桶并按指定大小过滤文件?我也想 return 那些大小正确的文件名。
s3 = boto3.client('s3')
s3.list_objects_v2(Bucket = 'my-images')
样本输出是
u'Key': u'detail/01018535.jpg',
u'LastModified': datetime.datetime(2019, 1, 23, 0, 48, 41, tzinfo=tzlocal()),
u'Size': 13535,
u'StorageClass': 'STANDARD'},
{u'ETag': '"cd65991a1c6f118e8b036208a30028a7"',
u'Key': u'detail/0119AF2.jpg',
u'LastModified': datetime.datetime(2019, 1, 10, 17, 17, tzinfo=tzlocal()),
u'Size': 12984,
u'StorageClass': 'STANDARD'}
例如,假设我想要搜索大小为 12984 的内容。 然后它将 return 'Key'
您可以使用 --query
表达式:
aws s3api list-objects-v2 --bucket my-images --query 'Contents[?Size==`12984`].[Key]' --output text
我将 [Key]
放在方括号中,以强制每个出现在单独的行上。
此语法适用于 Mac 命令行。 Windows 可能需要不同的引号。
有关使用此类表达式的提示,请参阅:JMESPath Tutorial
如果您想使用 boto3,我会使用此函数来查找零字节对象。您可以通过过滤特定尺寸
来根据您的需要进行调整import boto3
def get_empty_objects(bucket_name, prefixes):
"""
get list of objects from a given s3 prefix recursively
"""
results = []
for prefix in prefixes:
s3client = boto3.client('s3')
paginator = s3client.get_paginator("list_objects_v2")
paginator_result = paginator.paginate(
Bucket=bucket_name, Prefix=prefix)
try:
for object in paginator_result.search('Contents'):
if object['Size'] == 0:
results.append("s3://" + bucket_name + "/" + object['Key'])
except Exception as err:
print(">>> Error processing objects of [s3://" + bucket_name +
"/" + prefix + "] - " + str(err))
print(">>> Returning " + str(len(results)) + " objects for [s3://" + bucket_name + "/" + prefix + "]")
return results
用法:
get_empty_objects("mybucket", ["prefix1/", "prefix2/"])