[英]How to list and read each of the files in specific folder of an S3 bucket using Python Boto3
[英]How to count csv files with specific naming structure in s3 bucket using boto3?
我在 s3 存储桶中有许多不同名称的文件。
我想计算我的存储桶中有多少个带有“成员”一词的 csv?
但是,成员文件附加了一个 UUID,如下所示:
member_asldf2323209.csv
到目前为止我已经尝试过这个:
import boto3
# create the s3 resource
s3 = boto3.resource('s3')
# get the file object
obj = s3.Object('bucket_name', 'key')
# read the file contents in memory
file_contents = obj.get()["Body"].read()
# print the occurrences of the new line character to get the number of lines
print file_contents.count('\n')
这只会让我得到一个没有附加 UUID 的“成员”文件。
如果您想计算 Key 中包含特定单词的对象数量,您可以使用类似的方法:
import boto3
s3_client = boto3.client('s3', region_name = 'ap-southeast-2')
listing = s3_client.list_objects_v2(Bucket='my-bucket')
members = [object['Key'] for object in listing['Contents'] if 'member' in object['Key']]
print (members)
print (len(members))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.