使用blobstore存储MIME类型

1 投票
1 回答
856 浏览
提问于 2025-04-16 16:51

我该如何将传入的文件的MIME类型也存储到blobstore中呢?现在我既没有存储文件名,也没有存储MIME类型,而使用处理程序 blobstore_handlers.BlobstoreUploadHandler 是可以做到的。下面是我不使用 blobstore_handlers.BlobstoreUploadHandler 的代码:

    def create_image(number, self, file, ad):
        logging.debug('creating image')            
        try:
          file_name = files.blobstore.create()
          with files.open(file_name, 'a') as f:
              f.write(file)
          files.finalize(file_name)
          blob_key = files.blobstore.get_blob_key(file_name)
          logging.debug('creating image')
          img = Image(reference=ad) 
          logging.debug('creating image')
          img.primary_image = blob_key
          logging.debug('creating image')
          img.put()
          ad.put()
        except Exception:                
           self.response.write(Exception)

1 个回答

1

在创建的时候,可以把名称和 MIME 类型作为参数传进去:

def create(mime_type='application/octet-stream',
           _blobinfo_uploaded_filename=None):
  """Create a writable blobstore file.

  Args:
    mime_type: Resulting blob content MIME type as string.
    _blobinfo_uploaded_filename: Resulting blob's BlobInfo file name as string.

  Returns:
    A file name for blobstore file. This file can be opened for write
    by File API open function. To read the file or obtain its blob key, finalize
    it and call get_blob_key function.
  """

撰写回答