如何使用 Python 访问存储桶 GCS 的子文件夹中的文件? [英] How to access files within subfolders of a bucket GCS using Python?

查看:20
本文介绍了如何使用 Python 访问存储桶 GCS 的子文件夹中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from google.cloud import storage
import os
bucket = client.get_bucket('path to bucket')

上面的代码将我连接到我的存储桶,但我很难连接到存储桶中的特定文件夹.

The above code connects me to my bucket but I am struggling to connect with a specific folder within the bucket.

我正在尝试此代码的变体,但没有运气:

I am trying variants of this code, but no luck:

blob = bucket.get_blob("training/bad")
blob = bucket.get_blob("/training/bad")
blob = bucket.get_blob("path to bucket/training/bad")

我希望能够访问坏子文件夹中的图像列表,但我似乎做不到.尽管阅读了文档,但我什至不完全理解 blob 是什么,并根据教程对其进行了修改.

I am hoping to get access to a list of images within the bad subfolder, but I can't seem to do so. I don't even fully understand what a blob is despite reading the docs, and sort of winging it based on tutorials.

谢谢.

推荐答案

如果您想查找存在于特定 prefix(子目录)下的 blob(文件)您可以为 list_blobs() 函数

If you would like to find blobs (files) that exist under a specific prefix (subdirectory) you can specify prefix and delimiter arguments to the list_blobs() function

请参阅以下示例,取自 Google 列出对象示例(还有 GitHub 片段)

See the following example taken from the Google Listing Objects example (also GitHub snippet)

def list_blobs_with_prefix(bucket_name, prefix, delimiter=None):
    """Lists all the blobs in the bucket that begin with the prefix.

    This can be used to list all blobs in a "folder", e.g. "public/".

    The delimiter argument can be used to restrict the results to only the
    "files" in the given "folder". Without the delimiter, the entire tree under
    the prefix is returned. For example, given these blobs:

        /a/1.txt
        /a/b/2.txt

    If you just specify prefix = '/a', you'll get back:

        /a/1.txt
        /a/b/2.txt

    However, if you specify prefix='/a' and delimiter='/', you'll get back:

        /a/1.txt

    """
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)

    blobs = bucket.list_blobs(prefix=prefix, delimiter=delimiter)

    print('Blobs:')
    for blob in blobs:
        print(blob.name)

    if delimiter:
        print('Prefixes:')
        for prefix in blobs.prefixes:
            print(prefix)

这篇关于如何使用 Python 访问存储桶 GCS 的子文件夹中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆