【问题标题】:Is there any cloud API to save internet image to cloud using Python?是否有任何云 API 可以使用 Python 将互联网图像保存到云端?
【发布时间】:2016-06-29 08:36:01
【问题描述】:
出于某些原因,我手头有一个项目来备份网站。我使用 Python 中的请求来抓取内容和图像(url)。问题是,如何在云服务(Google Drive、Dropbox 等)中使用该图像的 url 将图像保存在云中。
我知道有一种方法可以先将图片保存在本地,然后再将本地图片上传到云端。但是我想知道是否有API支持通过url上传图片,而不是本地文件。
【问题讨论】:
标签:
python
image
google-app-engine
cloud
dropbox
【解决方案2】:
如果您不介意为存储付费,可以将其保存到您自己的云存储中。我有时不得不做类似的动作,并这样处理它:
def on_upload_image(self):
url = self.request.get('url')
result = urlfetch.fetch(url)
binary = result.content
blob_key = functions.get_blob_key_by_data(binary)
self.url = images.get_serving_url(blob_key, secure_url=True)
self.json()
from google.appengine.api import app_identity
def get_blob_key_by_data(data):
bucket = app_identity.get_default_gcs_bucket_name()
filename = hashlib.sha256(data).hexdigest()
mime_type = get_mime_type(data)
if not mime_type:
return None
gcs_filename = '/%s/image_%s' % (bucket, filename)
with gcs.open(gcs_filename, 'w', content_type=mime_type) as f:
f.write(data)
blob_key = blobstore.create_gs_key("/gs" + gcs_filename)
return blob_key