AWS lambda:调用HeadObject操作时发生错误(404):找不到

问题描述 投票:0回答:1

[当我通过s3brower将文件上传到s3时,我的python lambda脚本将处理这些文件。如果一次上传数千个文件,可能会出现故障例如,我上传1651张图片,lambda失败16次,一张图片名为test.jpg

在我的lambda脚本中,首先检查文件是否存在,client.head_object(Bucket=bucket_tmp,Key='test.jpg')cloudwatch日志显示错误An error occurred (404) when calling the HeadObject operation: Not Found然后我在计算机上执行client.head_object(Bucket=bucket_tmp,Key='test.jpg'),就可以了,然后可以在我的s3存储桶中看到它。

我在中国,这会是网络问题吗?lambda处理图像时,图像没有上传吗?

python amazon-web-services amazon-s3 aws-lambda
1个回答
1
投票

我们遇到了与lambda类似的问题,我们获得了AWS支持,并发现这是由于S3中文件的最终一致性造成的。 S3事件是在S3中的实际文件完全可用之前触发的,通常发生在我们一次上传大量文件时。

我们通过引入具有指数补偿(2、4、8、16 ..秒)的重试来解决此问题。

示例S3下载代码(可以同时使用client.head_object调用):

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

阅读更多:https://docs.aws.amazon.com/AmazonS3/latest/dev/Introduction.html

© www.soinside.com 2019 - 2024. All rights reserved.