0
votes

The script I have is working, but the bucket I am scanning over is massive and times out after a while. What can I do to make this more efficient or start from a specific location?

import boto3

s3 = boto3.resource('s3')
b = s3.Bucket('my_bucket')

for obj in b.objects.all():
   # Open the file, run some RegEx to find some data. If it's found, output to a log file

The first problem I have is the size of the bucket. It's about 1.5 million objects. I have my code opening up text files looking for some RegEx and if there's a match on the RegEx then it outputs the Object name and what was found.

After running the script for about an hour, it makes it about 40k objects in before throwing an error:

requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))

or

object at 0x109e82d50>: Failed to establish a new connection: [Errno 60] Operation timed out',))

The search items it's searching through are alphabetical, so we'll say it makes it through the "E" section and then times out. I want start with objects starting with "F".

2
Some performance considerations: docs.aws.amazon.com/AmazonS3/latest/dev/… - jarmod
Have you tried Amazon Athena? It is intended to quickly analyze large-scale datasets on S3: aws.amazon.com/athena - Khalid T.

2 Answers

3
votes

If you have a large number of objects in your Amazon S3 bucket, then objects.all() is not an efficient iteration method, since it tries to load them all into memory simultaneously.

Instead, use list_objects_v2() to page through the objects in groups of 1000. Then, call it again with the ContinuationToken that was returned.

You will effectively need a for loop calling list_objects_v2() and another for loop within that which loops through each object.

1
votes

1) Is your script running on a EC2 instance? If you are running it from your localhost or your own datacenter , moving it to a EC2 instance in the same Region as the bucket and running it from there could decrease latency and increase performance. You are probably running it from a EC2 instance, but I figured I would ask. You got alot of files to go through!

2) Have you checked out the Athena service from AWS? Athena is a interactive query service and queries data directly from S3. Amazon Athena uses Presto with full standard SQL support and works with a variety of standard data formats, including CSV, JSON, ORC, and Parquet. If the text data you are working with fits a Athena use case, it might be worth the time to check it out. Athena is a new service, I have never used it but it sounds like it was created to address the use case you have. Here is the FAQ:

https://aws.amazon.com/athena/faqs/