当用户将图像上传到 Google Cloud Storage 时,我需要获取文件名(云中的散列名称,而不是用户的文件名)才能在之后删除文件。
我读过这个问题:
BlobService.getUploads()
BlobStore.blobKeys
即使使用 GS 也会
返回。https://code.google.com/p/googleappengine/issues/detail?id=8337
在 GAE SDK 1.7.5 发行说明中:
Blobstore 服务现在在使用 Cloud Storage 时返回创建的文件名而不是 blobKey。
但是,我不知道如何通过 Blobstore 服务获取创建的文件名。
我的代码:
icon = self.get_uploads('icon')
if icon:
blob_info = icon[0]
img_key = str(blob_info.key())
img_filename = blob_info.filename
img_size = blob_info.size
img_type = blob_info.content_type
img_creation = blob_info.creation
img_url = images.get_serving_url(blob_info.key())
谢谢你的帮助。
更新:
最后我谷歌并得到问题:
self.get_uploads('file')
[0]BlobStore.blobKeys
即使在使用 GS 时也会返回。
https://code.google.com/p/googleappengine/issues/detail?id=9051
魔法代码:
fileInfo = blobstore.parse_file_info(self.request.POST['file'])
logging.info(fileInfo.gs_object_name)
有用!
所以我可以使用 gs_object_name 来删除文件:
files.delete(fileInfo.gs_object_name)