代码之家  ›  专栏  ›  技术社区  ›  RustyShackleford

如何使用boto3统计s3存储桶中具有特定命名结构的csv文件?

  •  1
  • RustyShackleford  · 技术社区  · 7 年前

    我想数一数我的桶中有多少个带有“成员”字样的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的“成员”文件。

    1 回复  |  直到 7 年前
        1
  •  2
  •   John Rotenstein    7 年前

    如果要计算键中包含特定单词的对象数,可以使用以下方法:

    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))