邓平洲
邓平洲

Reputation: 11

AWS lambda:An error occurred (404) when calling the HeadObject operation: Not Found

When I upload file to s3 by s3brower,my python lambda script will process these file. if I upload thousands of file at once,There may be some failures For example,I upload 1651 images ,lambda failed sixteen times,a image named test.jpg

In my lambda script,First check if the file exists,
client.head_object(Bucket=bucket_tmp,Key='test.jpg')
cloudwatch log show the error An error occurred (404) when calling the HeadObject operation: Not Found Then I execute the client.head_object(Bucket=bucket_tmp,Key='test.jpg') in my computer,It's ok ,then I can see it in my s3 bucket.

I'am in china,Will this be a network problem?When lambda process the image,but the image is not uploaded?

Upvotes: 0

Views: 11680

Answers (1)

Nimin Unnikrishnan
Nimin Unnikrishnan

Reputation: 383

We faced similiar issues with lambda, we followed up with AWS support and found out that this is caused due to eventual consistency of files in S3. The S3 event is triggered before the actual file is fully available in S3, usually happens when we upload large number of files at once.

We solved this issue by introducing retries with exponential backoff (2,4,8,16.. secs).

Sample S3 download code (you could use the client.head_object call similiarly):

#Method with retries
def download_file_s3(client,bucket,s3_path,local_path,retries = 3):
    i = 0
    sleep = 2
    while(i <= retries):
        try:
            client.download_file(bucket,s3_path,local_path)
            break
        except Exception as e:            
            print("404 file not found !!!")
            i = i+1
            if i>retries:
                raise Exception(traceback.format_exc())
            time.sleep(sleep)
            sleep = sleep*2
            print("retry: "+str(i))

#Sample call
client = boto3.client('s3')
download_file_s3(client,bucket,s3_path,local_path,retries)

Read more: https://docs.aws.amazon.com/AmazonS3/latest/dev/Introduction.html

Upvotes: 6

Related Questions