gpt4 book ai didi

python - 在 Google Cloud Storage 中存储同名的多个文件?

转载 作者:太空宇宙 更新时间:2023-11-04 01:20:30 25 4
gpt4 key购买 nike

因此,我正在尝试将使用 Flask 编写的 Python 网络应用程序移植到 Google App Engine。该应用程序可托管用户上传的最大 200 MB 的文件,对于非图像文件,需要保留文件的原始名称。为了防止文件名冲突,例如两个人上传 stuff.zip,每个人都包含完全不同且不相关的内容,该应用程序在文件系统上创建一个 UUID 文件夹并将文件存储在其中,并将它们提供给用户。 Google App Engine 的云存储,我计划通过创建一个存储桶来存储用户文件——根据他们的文档,“没有文件夹的概念”。使用他们的系统获得相同功能的最佳方法是什么?

当前方法,仅供演示:

 # generates a new folder with a shortened UUID name to save files
# other than images to avoid filename conflicts
else:
# if there is a better way of doing this i'm not clever enough
# to figure it out
new_folder_name = shortuuid.uuid()[:9]
os.mkdir(
os.path.join(app.config['FILE_FOLDER'], new_folder_name))
file.save(
os.path.join(os.path.join(app.config['FILE_FOLDER'], new_folder_name), filename))
new_folder_path = os.path.join(
app.config['FILE_FOLDER'], new_folder_name)
return url_for('uploaded_file', new_folder_name=new_folder_name)

最佳答案

来自Google Cloud Storage Client Library Overview文档:

GCS and "subdirectories"

Google Cloud Storage documentation refers to "subdirectories" and the GCS client library allows you to supply subdirectory delimiters when you create an object. However, GCS does not actually store the objects into any real subdirectory. Instead, the subdirectories are simply part of the object filename. For example, if I have a bucket my_bucket and store the file somewhere/over/the/rainbow.mp3, the file rainbow.mp3 is not really stored in the subdirectory somewhere/over/the/. It is actually a file named somewhere/over/the/rainbow.mp3. Understanding this is important for using listbucket filtering.

虽然 Cloud Storage 本身不支持子目录,但它允许您使用子目录分隔符在文件名中。这基本上意味着您的文件的路径仍然看起来就像它在子目录中一样,即使它不是。显然,只有当您遍历存储桶的全部内容时,您才应该关心这一点。

来自Request URIs文档:

URIs for Standard Requests

For most operations you can use either of the following URLs to access objects:

storage.googleapis.com/<bucket>/<object>

<bucket>.storage.googleapis.com/<object>

这意味着他们示例的公共(public) URL 将是 http://storage.googleapis.com/my_bucket/somewhere/over/the/rainbow.mp3 .他们的服务会将其解释为 bucket=my_bucketobject=somewhere/over/the/rainbow.mp3 (即没有子目录的概念,只是一个带有嵌入斜杠的对象名称);然而,浏览器只会看到路径 /my_bucket/somewhere/over/the/rainbow.mp3并将其解释为文件名是 rainbow.mp3 .

关于python - 在 Google Cloud Storage 中存储同名的多个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21584838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com