问题描述
我正在尝试使用AWS lambdas上传图像,但是图像损坏了,我使用txt文件进行了测试,效果很好。函数checkImage是处理程序
def parse_into_field_storage(fp,ctype,clength):
fs = FieldStorage(
fp=fp,environ={'REQUEST_METHOD': 'POST'},headers={
'content-type': ctype,'content-length': clength
},keep_blank_values=True
)
form = {}
files = {}
for f in fs.list:
if f.filename:
files.setdefault(f.name,[]).append(f)
else:
form.setdefault(f.name,[]).append(f.value)
return form,files
def checkImage(event,context):
body_file = BytesIO(bytes(event["body"],"utf-8"))
form,files = parse_into_field_storage(
body_file,event['headers']['Content-Type'],body_file.getbuffer().nbytes
)
fileitem = files['file'][0]
print(fileitem.filename)
# Test if the file was uploaded
if fileitem.filename:
fn = os.path.basename(fileitem.filename)
open('/tmp/' + fn,'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
return {
"statusCode": 200,"body": message
}
当我从python打印文件时,我得到了
我使用了diff,二进制文件不同。我认为图像文件被解析成两倍或类似的东西 我猜这里我做错了什么
body_file = BytesIO(bytes(event["body"],"utf-8"))
我尝试了不同的形式,例如
fp = io.BytesIO(event['body'].encode('utf-8'))
但基本相同。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)