AWS S3映像保存丢失元数据 [英] AWS S3 image saving loses metadata
问题描述
我正在使用以python 2.7x编写的AWS Lambda函数,该函数下载并保存到/tmp,然后将图像文件上传回存储桶.
I am working with an AWS Lambda function written in python 2.7x which downloads, saves to /tmp , then uploads the image file back to bucket.
我的图像元数据以带有诸如Content-Type = image/jpeg之类的http标头的原始存储桶开始.
My image meta data starts out in original bucket with http headers like Content-Type= image/jpeg, and others.
使用PIL保存图像后,所有标头都消失了,剩下Content-Type = binary/octet-stream
After saving my image with PIL, all headers are gone and I am left with Content-Type = binary/octet-stream
据我所知,由于PIL的工作方式,image.save失去了标题.如何保留元数据或至少将其应用于新保存的图像?
From what I can tell, image.save is loosing the headers due to the way PIL works. How do I either preserve metadata or at least apply it to the new saved image?
我看到帖子暗示此元数据在exif中,但是我试图从原始文件中获取exif信息并运用于已保存的文件,但没有运气.我仍然不清楚它是否在exif数据中.
I have seen post suggesting that this metadata is in exif but I tried to get exif info from original file and apply to saved file with no luck. I am not clear of it's in exif data anyway.
部分代码可让我对正在做的事情有所了解:
Partial code to give idea of what I am doing:
def resize_image(image_path):
with Image.open(image_path) as image:
image.save(upload_path, optimize=True)
def handler(event, context):
global upload_path
for record in event['Records']:
bucket = record['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode("utf8"))
download_path = '/tmp/{}{}'.format(uuid.uuid4(), file_name)
upload_path = '/tmp/resized-{}'.format(file_name)
s3_client.download_file(bucket, key, download_path)
resize_image(download_path)
s3_client.upload_file(upload_path, '{}resized'.format(bucket), key)
感谢谢尔盖(Sergey),我改为使用get_object,但是响应缺少元数据:
Thanks to Sergey, I changed to using get_object but response is missing Metadata:
response = s3_client.get_object(Bucket=bucket,Key=key)
response = {u'Body':,u'AcceptRanges':'bytes',u'ContentType':'image/jpeg','ResponseMetadata':{'HTTPStatusCode':200,'RetryAttempts':0,' HostId'':'au30hBMN37/ti0WCfDqlb3t9ehainumc9onVYWgu + CsrHtvG0u/zmgcOIvCCBKZgQrGoooZoW9o =','RequestId':'1A94D7F01914A787','HTTPHeaders':{'' ti0WCfDqlb3t9ehainumc9onVYWgu + CsrHtvG0u/zmgcOIvCCBKZgQrGoooZoW9o =','accept-ranges':'bytes','expires':'Sun,2034 Jan Jan 00:00:00 GMT','server':'lastS3 '星期五,2016年12月23日15:21:56','x-amz-request-id':'1A94D7F01914A787','etag':'"9ba59e5457da0dc40357f2b53715619d"','cache-control':'max-age = 2592000 ,public','date':'Fri,23 Dec 2016 15:21:58 GMT','content-type':'image/jpeg'}},u'LastModified':datetime.datetime(2016,12,23 ,15、21、56,tzinfo = tzutc()),u'ContentLength':84053,u'Expires':datetime.datetime(2034、1、1、0、0,tzinfo = tzutc()),u'ETag ':"9ba59e5457da0dc40357f2b53715619d",u'CacheControl:最大年龄= 2592000,pu blic',u'Metadata':{}}
response= {u'Body': , u'AcceptRanges': 'bytes', u'ContentType': 'image/jpeg', 'ResponseMetadata': {'HTTPStatusCode': 200, 'RetryAttempts': 0, 'HostId': 'au30hBMN37/ti0WCfDqlb3t9ehainumc9onVYWgu+CsrHtvG0u/zmgcOIvCCBKZgQrGoooZoW9o=', 'RequestId': '1A94D7F01914A787', 'HTTPHeaders': {'content-length': '84053', 'x-amz-id-2': 'au30hBMN37/ti0WCfDqlb3t9ehainumc9onVYWgu+CsrHtvG0u/zmgcOIvCCBKZgQrGoooZoW9o=', 'accept-ranges': 'bytes', 'expires': 'Sun, 01 Jan 2034 00:00:00 GMT', 'server': 'AmazonS3', 'last-modified': 'Fri, 23 Dec 2016 15:21:56 GMT', 'x-amz-request-id': '1A94D7F01914A787', 'etag': '"9ba59e5457da0dc40357f2b53715619d"', 'cache-control': 'max-age=2592000,public', 'date': 'Fri, 23 Dec 2016 15:21:58 GMT', 'content-type': 'image/jpeg'}}, u'LastModified': datetime.datetime(2016, 12, 23, 15, 21, 56, tzinfo=tzutc()), u'ContentLength': 84053, u'Expires': datetime.datetime(2034, 1, 1, 0, 0, tzinfo=tzutc()), u'ETag': '"9ba59e5457da0dc40357f2b53715619d"', u'CacheControl': 'max-age=2592000,public', u'Metadata': {}}
如果我使用: 元数据= response ['ResponseMetadata'] ['HTTPHeaders']
If I use: metadata = response['ResponseMetadata']['HTTPHeaders']
metadata = {'content-length':'84053','x-amz-id-2':'f5UAhWzx7lulo3cMVF8hdVRbHnhdnjHWRDl + LDFkYm9pubjL0A01L5yWjgDjWRE4TjRnjqDeA0U-',' 2034年1月1日00:00:00 GMT','服务器':'AmazonS3','最后修改':'星期五,2016年12月23日15:47:09 GMT','x-amz-request-id':' 4C69DF8A58EF3380','etag':'"9ba59e5457da0dc40357f2b53715619d"','cache-control':'max-age = 2592000,public','date':'Fri,23 Dec 2016 15:47:10 GMT','content-类型":"image/jpeg"}
metadata = {'content-length': '84053', 'x-amz-id-2': 'f5UAhWzx7lulo3cMVF8hdVRbHnhdnjHWRDl+LDFkYm9pubjL0A01L5yWjgDjWRE4TjRnjqDeA0U=', 'accept-ranges': 'bytes', 'expires': 'Sun, 01 Jan 2034 00:00:00 GMT', 'server': 'AmazonS3', 'last-modified': 'Fri, 23 Dec 2016 15:47:09 GMT', 'x-amz-request-id': '4C69DF8A58EF3380', 'etag': '"9ba59e5457da0dc40357f2b53715619d"', 'cache-control': 'max-age=2592000,public', 'date': 'Fri, 23 Dec 2016 15:47:10 GMT', 'content-type': 'image/jpeg'}
保存put_object
Saving with put_object
s3_client.put_object(Bucket=bucket+'resized',Key=key, Metadata=metadata, Body=downloadfile)
在s3中创建了很多额外的元数据,包括以下事实:它没有将内容类型保存为image/jpeg而是将其保存为二进制/八位字节流,并且确实创建了元数据x-amz-meta-content-type =图片/jpeg
creates a whole lot of extra metadata in s3 including the fact that it does not save content-type as image/jpeg but rather as binary/octet-stream and it does create metadata x-amz-meta-content-type = image/jpeg
推荐答案
您会混淆由AWS S3存储的S3元数据与对象以及存储在文件本身内部的EXIF元数据.
You are confusing S3 metadata, stored by AWS S3 along with an object, and EXIF metadata, stored inside the file itself.
download_file()
不能从S3获取对象属性.您应该改用get_object()
: https: //boto3.readthedocs.io/zh-CN/latest/reference/services/s3.html#S3.Client.get_object
download_file()
doesn't get object attributes from S3. You should use get_object()
instead: https://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.get_object
然后,您可以使用具有相同属性的put_objects()
来上载新文件:
Then you can use put_objects()
with the same attributes to upload new file: https://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.put_object
这篇关于AWS S3映像保存丢失元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!