Boto3: upload file from base64 to S3

只谈情不闲聊 提交于 2020-02-04 04:09:05

问题


How can I directly upload a base64 encoded file to S3 with boto3?

object = s3.Object(BUCKET_NAME,email+"/"+save_name)
object.put(Body=base64.b64decode(file))

I tried to upload the base64 encoded file like this, but then the file is broken. Directly uploading the string without the base64 decoding also doesn't work.

Is there anything similar to set_contents_from_string() from boto2?


回答1:


I just fixed the problem and found out that the way of uploading was correct, but the base64 string was incorrect because it still contained the prefix data:image/jpeg;base64, - removing that prefix solved the problem.




回答2:


If you read the documentation thoughtfully on Object.put, you will see this

response = object.put(
    ACL='private'......,
    Body=b'bytes'|file,
    .....,

Body only accept file object or bytes, any other type will failed. base64.b64decode doesn't read file object automatically, you must read the data into the decode module.

# FIX 
object.put(Body=base64.b64decode(file.read()))  

As reminder, always post the stacktrace.



来源:https://stackoverflow.com/questions/47420886/boto3-upload-file-from-base64-to-s3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!