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

查看:59
本文介绍了如何使用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.

谢谢.

推荐答案

如果您要查找在特定前缀(子目录)下存在的 blob (文件)您可以为prefix 和 delimiter 参数.html?highlight = list_blobs#google.cloud.storage.bucket.Bucket.list_blobs"rel =" noreferrer> 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 列出对象示例(也

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天全站免登陆