當用戶將圖像上傳到Google Cloud Storage時,我需要獲取文件名(Cloud中的散列名稱,而不是用戶的文件名)才能刪除文件。如何在用戶將文件上傳到Google Cloud Storage時獲取創建的文件名?
我讀過這個問題:
BlobService.getUploads()返回BLOBSTORE blobKeys即使使用GS時。 https://code.google.com/p/googleappengine/issues/detail?id=8337
在GAE SDK 1.7.5版本說明:
的Blob存儲服務使用雲存儲時,現在返回所創建的文件名,而不是的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( '文件')[0]返回即使使用GS時BLOBSTORE blobKeys。 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)