Google app engine 不使用文件API从urlfetch保存图像

Google app engine 不使用文件API从urlfetch保存图像,google-app-engine,blobstore,Google App Engine,Blobstore,允许将文件写入blobstore的API实际上已经过时,将来将被删除。但是我使用这个API来保存来自外部服务的用户图片。我无法使用上载处理程序来执行此操作 当删除文件API时,是否有其他方法可以直接将文件写入blobstore 以下是一个解决我问题的示例。现在,我们可以通过编程上传文件,避免使用不推荐的文件API class BlobstoreUpload(blobstore_handlers.BlobstoreUploadHandler): def post(self): uplo

允许将文件写入blobstore的API实际上已经过时,将来将被删除。但是我使用这个API来保存来自外部服务的用户图片。我无法使用上载处理程序来执行此操作

当删除文件API时,是否有其他方法可以直接将文件写入blobstore


以下是一个解决我问题的示例。现在,我们可以通过编程上传文件,避免使用不推荐的文件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

红盒子里的那张纸条怎么会让你困惑?不推荐写入blobstore,您应该改为写入云存储。我不想使用其他库。(GCS)您没有太多选择。下面是示例代码。有可能将blob分块发布吗?@voscausa为什么需要这个?但我认为可以将文件“拆分”为块,并将每个块放在不同的blob中。或者你想把chunk1和chunk2放在一个blob中?我需要读取、缓冲和发布/上传大blob。现在,我使用文件API和任务编写blob块。