【问题标题】:Is it possible to loop through Amazon S3 bucket and count the number of lines in its file/key using Python?是否可以遍历 Amazon S3 存储桶并使用 Python 计算其文件/密钥中的行数?
【发布时间】:2016-05-30 06:34:52
【问题描述】:

是否可以遍历 Amazon S3 存储桶中的文件/密钥,读取内容并使用 Python 计算行数?

例如:

  1. My bucket: "my-bucket-name"
  2. File/Key : "test.txt" 

我需要遍历文件“test.txt”并计算原始文件中的行数。

示例代码:

for bucket in conn.get_all_buckets():
    if bucket.name == "my-bucket-name":
        for file in bucket.list():
            #need to count the number lines in each file and print to a log.

【问题讨论】:

    标签: python amazon-web-services amazon-s3 boto


    【解决方案1】:

    使用boto3,您可以执行以下操作:

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

    如果你想对一个bucket中的所有对象都这样做,可以使用如下代码sn-p:

    bucket = s3.Bucket('bucket_name')
    for obj in bucket.objects.all():
        file_contents = obj.get()["Body"].read()
        print file_contents.count('\n')
    

    更多功能请参考 boto3 文档:http://boto3.readthedocs.io/en/latest/reference/services/s3.html#object

    更新:(使用 boto 2)

    import boto
    s3 = boto.connect_s3()  # establish connection
    bucket = s3.get_bucket('bucket_name')  # get bucket
    
    for key in bucket.list(prefix='key'):  # list objects at a given prefix
        file_contents = key.get_contents_as_string()  # get file contents
        print file_contents.count('\n')  # print the occurrences of the new line character to get the number of lines
    

    【讨论】:

    • 问题是,我没有使用 Boto 3.0。我的 Boto 版本是 2.38.0。因此不能尝试 s3.Object 方法。另一个问题是我的文件都是 .gz 格式,当我尝试使用 Key.open_read 作为 gzip.GzipFile 的 fd 时,它变得更糟。它错误为 AttributeError: 'str' object has no attribute 'tell' 或 'seek' 我想知道是否有任何解决方法。
    • @Renukadevi,我更新了我的帖子,为 boto2 添加了一个示例。要解压 gzip 数据,您可能可以使用 zlib 库,请参见此处的示例:stackoverflow.com/a/2695575/4072706 希望对您有所帮助。
    • 非常感谢。,这太简单了。我是 AWS 新手,您的解决方案帮助很大。
    【解决方案2】:

    有时将大文件读入内存远非理想。相反,您可能会发现以下更多用途:

    s3 = boto3.client('s3')
    obj = s3.get_object(Bucket='bucketname', Key=fileKey)
    
    
    nlines = 0
    for _ in obj['Body'].iter_lines(): nlines+=1
    
    print (nlines)
    

    【讨论】:

      【解决方案3】:

      Amazon S3 只是一种存储服务。您必须获取文件才能对其执行操作(例如读取文件数)。

      【讨论】:

        【解决方案4】:

        您可以使用boto3 list_objects_v2 循环访问存储桶。因为 list_objects_v2 最多只列出 1000 个键(即使您指定 MaxKeys),您必须在响应字典中是否存在 NextContinuationToken,然后指定 ContinuationToken 以读取下一页。

        我在一些答案中编写了示例代码,但我不记得了。

        然后你使用 get_object() 读取文件,并使用simple line count code

        (更新) 如果您需要特定前缀名称的键,则添加 PREFIX 过滤器。

        【讨论】:

        • 您好,谢谢,我可以正确地表达我的问题。我想遍历 S3 中的特定文件并计算其中的行数。
        • @Renukadevi:请澄清“具体”的含义。你的意思是带前缀的文件吗?
        猜你喜欢
        • 1970-01-01
        • 2012-03-15
        • 2015-01-14
        • 1970-01-01
        • 1970-01-01
        • 2020-07-07
        • 2016-02-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多