早期我使用Files API使用数据将数据保存到blobstore,然后使用以下代码读取它:
resource = blob_key
blob_info = blobstore.BlobInfo.get(resource)
if blob_info:
self.send_blob(blob_info)
else:
self.response.write('Blob entry not found')
文档说在很多情况下我可以继续使用blobstore API来使用Google Cloud Storage。但上面的代码会返回blob_info
等于None
。
根据the documentation,我应该直接从blobstore读取数据,但以下代码也不适用于GCS(Blob entry not found
),但适用于blobstore文件:
class BlobstoreServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, resource):
resource = str(urllib.unquote(resource))
blob_info = blobstore.BlobInfo.get(resource)
if blob_info: # blobstore files
self.send_blob(blob_info)
elif blobstore.get(resource): # Google Cloud Storage files
self.send_blob(resource)
else:
self.response.write('Blob entry not found')
答案 0 :(得分:0)
您如何撰写Google云端存储?使用时不会创建BlobInfo appengine gcs client。对于此类文件,您仍然可以使用blobstore.create_gs_key创建一个blob密钥,但这些密钥没有关联的BlobInfo。
答案 1 :(得分:0)
使用Blobstore下载blobfiles和GCS文件的示例代码
您已在db.Model YourFiles实体中保存了文件名和blobkey:
your_file = YourFiles.get_by_key_name(key_name)
# blob_ref is a blobstore.BlobReferenceProperty()
download_url = '/blob_serve/%s?save_as=%s' % (your_file.blob_ref.key(), your_file.file_name)
self.redirect(download_url) # download using the handler below
class BlobServe(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, resource):
""" use blob_key to download GCS and blobstore blobs """
blob_key = str(urllib.unquote(resource))
save_as = self.request.get('save_as')
self.send_blob(blob_key, save_as=save_as)