【发布时间】:2015-04-19 19:59:00
【问题描述】:
我有一个用例,我需要动态生成一个 csv/txt 文件,然后将文件的密钥保存到数据存储区,以便以后可以下载该文件。我似乎在生成与 Ferris 的下载 uri 结合使用的有效密钥时遇到问题。 比如:
import cloudstorage
from google.appengine.ext import blobstore
@route
def make_file(self):
# Hardcoded filename, this will overwrite prior file if it exists
filename = '/mydomain.appspot.com/some_folder/myawesometextfile2.txt'
# Create file
gcs_file = cloudstorage.open(filename,'w',content_type='text/plain')
# Generate the file's contents (pretend this is being done dynamically)
gcs_file.write('Doe,John\n')
gcs_file.write('Smith,Jane\n')
# Close the file
gcs_file.close()
# This is supposed to create a blobkey that represents the cloud object
psuedo_blobkey = blobstore.create_gs_key('/gs'+filename)
# This is supposed to also create a blobkey...I think?
another_key = blobstore.BlobKey(psuedo_blobkey)
# Here I attempt to store this in the datastore.
new_file = self.meta.Model(
file_key = another_key,
file_name_actual = filename,
)
new_file.put()
如果我尝试将“psuedo_blobkey”保存到我的 NDB 模型中,我会收到类似“Expected BlobKey, got AMIfv-yadda-yadda-yadda”的错误。
如果我尝试将“another_key”保存到我的模型中,它会毫无问题地存储密钥,但是当我尝试通过 appengine 仪表板中的数据存储查看器访问实体时,它会告诉我它不是有效的密钥。因此,当我尝试像这样在我的 jinja 模板中使用密钥时:
<a href="{{uri("download", blob=file_key)}}" target="_blank">Export</a>
Ferris 呈现错误“找不到资源”。这是有道理的,因为它显然不是有效的密钥。
所以我想我的问题是,我到底如何才能为我在谷歌云存储中动态生成的文件获取有效密钥?
顺便说一句:通过上传操作获取密钥很容易,但由于某种原因,动态生成的 GCS 对象不会产生相同的结果。
提前致谢
【问题讨论】:
-
你能保存文件名而不是密钥吗?这就是您获取文件所需的全部内容。
-
当然,我可以保存文件名,但我尝试使用 http://storage.googleapis.com/ / 但除非 ACL 设置为公开的,我不想这样做。即使我做了文档描述设置 acl 如下: options ={' x-goog-acl': 'public-read'} 但这甚至不起作用,谷歌会产生错误。如果你有例子,我很乐意试一试。
标签: google-app-engine python-2.7 google-cloud-storage