gpt4 book ai didi

google-app-engine - 在没有 Files API 的情况下从 urlfetch 保存图像

转载 作者:太空宇宙 更新时间:2023-11-03 15:35:28 26 4
gpt4 key购买 nike

允许实用地将文件写入 blobstore 的 API 现在已弃用,将来会被删除。但是我正在使用这个 API 来保存来自外部服务的用户图片。我不能使用上传处理程序来执行此操作。

当 Files API 将被删除时,是否有任何替代方法可以将文件直接写入 blobstore?

https://developers.google.com/appengine/docs/python/blobstore/#Writing_Files_to_the_Blobstore

最佳答案

这是一个解决我的问题的例子。现在我们可以通过编程方式上传文件,避免弃用的 Files API。

class BlobstoreUpload(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
blob_info = upload_files[0]
return self.response.write(blob_info.key())

@classmethod
def encode_multipart_formdata(cls, fields, files, mimetype='image/png'):
"""
Args:
fields: A sequence of (name, value) elements for regular form fields.
files: A sequence of (name, filename, value) elements for data to be
uploaded as files.

Returns:
A sequence of (content_type, body) ready for urlfetch.
"""
boundary = 'paLp12Buasdasd40tcxAp97curasdaSt40bqweastfarcUNIQUE_STRING'
crlf = '\r\n'
line = []
for (key, value) in fields:
line.append('--' + boundary)
line.append('Content-Disposition: form-data; name="%s"' % key)
line.append('')
line.append(value)
for (key, filename, value) in files:
line.append('--' + boundary)
line.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
line.append('Content-Type: %s' % mimetype)
line.append('')
line.append(value)
line.append('--%s--' % boundary)
line.append('')
body = crlf.join(line)
content_type = 'multipart/form-data; boundary=%s' % boundary
return content_type, body


class UserProfile(webapp2.RequestHandler):
def post(self):
picture = self.request.POST.get('picture')

# Write new picture to blob
content_type, body = BlobstoreUpload.encode_multipart_formdata(
[], [('file', name, image)])
response = urlfetch.fetch(
url=blobstore.create_upload_url(self.uri_for('blobstore-upload')),
payload=body,
method=urlfetch.POST,
headers={'Content-Type': content_type},
deadline=30
)
blob_key = response.content

关于google-app-engine - 在没有 Files API 的情况下从 urlfetch 保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17211966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com