是否可以循环遍历 Amazon S3 存储桶并使用 Python 计算其 file/key 中的行数?

Is it possible to loop through Amazon S3 bucket and count the number of lines in its file/key using Python?

是否可以循环访问 Amazon S3 存储桶中的 file/key,读取内容并使用 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.

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

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

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

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

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

使用 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 = 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

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

s3 = boto3.client('s3')
obj = s3.get_object(Bucket='bucketname', Key=fileKey)


nlines = 0
for _ in obj['Body'].iter_lines(): nlines+=1

print (nlines)