如何将Croppie照片上传到Google云端存储

时间:2018-04-22 20:13:52

标签: python google-app-engine google-cloud-storage croppie

我正在尝试将 croppie 裁剪后的照片上传到GCS中。 gcs write(imageToUpload)方法失败,因为imageToUpload当前不是文件,而是BLOB。有没有人知道如何使这项工作?也许,在python中将BLOB转换为文件的方法?可以从Croppie返回的图像类型是:1)Canvas,2)HTML,3)BLOB和4)Base64 Here is the documentation (Ctrl-F "Result")。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

您可以在App Engine中使用images pkg。

from google.appengine.api import images
import urllib2

# if you fetch as image
image        = urllib2.urlopen(the_image_url)   
content_type =  image.headers['Content-Type'] 
image_bytes  = image.read()
image.close()

# if you fetch as bytes
image_bytes  = urllib2.urlopen(the_image_bytes_url) 
content_type = my_image_file.content_type  

filename = '/{}/user/{}_avatar'.format(DEFAULT_GCS_BUCKET_NAME), user_id) # for example

'''
    Create a GCS file with GCS client.
    Make sure you give the GAE app permission to write to your bucket:
    https://developers.google.com/appengine/docs/python/googlestorage/index#Prerequisites
'''

options={'x-goog-acl': 'public-read', 'Cache-Control': 'private, max-age=0, no-transform'}
with gcs.open(filename, 'w', content_type=content_type, options=options) as f:
    f.write(image_bytes)
    f.close()

答案 1 :(得分:0)

您真正需要做的就是将base64格式转换为本地文件,以便上传。

请查看this post了解详情。