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

标签 python google-app-engine flask

所以我试图将用 Flask 编写的 Python Web 应用程序移植到 Google App Engine。该应用程序托管用户上传的文件大小最大为 200mb,对于非图像文件,需要保留文件的原始名称。为了防止文件名冲突,例如两个人上传 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/

相关文章:

python - Django orm 为每个组获取最新信息

python - 比较 Pandas Dataframe 中共享列值的行组

python - 提高函数的可读性和功能性

google-app-engine - 在 Google App Engine 中,如何检查 urlsafe 创建的 key 的输入有效性?

java - GAE 上的 Spring 4.2.1 和 java.util.ResourceBundle$Control 是受限类

google-app-engine - 演示项目中的appengine云存储编译错误

python - 在 flask/jinja2 中编码二进制数据

android - 处理 android 和 django 时使用什么日期格式?

python - Slack 机器人 [Python/Flask] : Wait for Direct Messages from specified users and save the responses

python - 像 MVC 3 这样的 flask 部分 View