我的lambda函数失败只是因为我仍在编写它,而我还没有放入任何代码来捕获错误...
但是我发现在重试中,它的运行与第一次调用不同,它似乎失去了对某些数据的访问权限。
我正在使用response = s3_client.head_object(Bucket=bucket, Key=key)
来访问Accessing Meta Data from AWS S3 with AWS Lambda建议的元数据。
在第一次调用时,s3_client.head_object(...)
恰好返回我需要的内容。但是,当函数失败并且第二次调用运行时,我收到一个新错误:
[ERROR] ClientError: An error occurred (404) when calling the HeadObject operation: Not Found
Traceback (most recent call last):
File "/var/task/CreateThumbnail.py", line 25, in handler
response = s3_client.head_object(Bucket=bucket, Key=key)
File "/var/task/botocore/client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/var/task/botocore/client.py", line 661, in _make_api_call
raise error_class(parsed_response, operation_name)
为什么连续调用将失去对此的访问权限?我已经验证了对event
以及bucket
和key
等的响应仍然是相同的。
奖金:还有另一种获取元数据的方法吗?
编辑1:通过S3 POST触发器调用此lambda函数...
编辑2:实际上,这似乎似乎只在50%的时间内出错,放入S3的文件越多,出错的机会就越多。几乎有一个缓存错误?
import boto3
import os
import sys
import uuid
from PIL import Image
import PIL.Image
s3_client = boto3.client('s3')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def resize_image(image_path, resized_path):
with Image.open(image_path) as image:
image.thumbnail(tuple(x / 2 for x in image.size))
image.save(resized_path)
def handler(event, context):
logger.info(event)
for record in event['Records']:
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
response = s3_client.head_object(Bucket=bucket, Key=key)
logger.info('Response: {}'.format(response))
download_path = '/tmp/{}{}'.format(uuid.uuid4(), key)
upload_path = '/tmp/resized-{}'.format(key)
s3_client.download_file(bucket, key, download_path)
resize_image(download_path, upload_path)
s3_client.upload_file(upload_path, '{}resized'.format(bucket), key)
答案 0 :(得分:0)
原来我正在上传一些文件,其名称带有括号[
...
根据https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html“避免使用的字符”下的建议,不建议这样做。
感谢What are valid S3 key names that can be accessed via the S3 rest API?将我指向该资源。