【问题标题】:Wildcard folder listing with gsutil使用 gsutil 列出通配符文件夹
【发布时间】:2018-02-19 18:44:07
【问题描述】:

我正在尝试列出以固定字符串后跟字母数字字符开头的 GCS 文件夹。我不想做一个递归列表。当我尝试关注时

假设我们有以下文件夹结构(我知道内部没有概念没有文件夹的概念。它只是路径前缀)

gs://somebucket/monitor/a
gs://somebucket/monitor/a/a1.log.gz
gs://somebucket/monitor/a/a2.log.gz
gs://somebucket/monitor/b
gs://somebucket/monitor/b/b1.log.gz
gs://somebucket/monitor/b/b2.log.gz
gs://somebucket/monitor/c
gs://somebucket/monitor1/x
gs://somebucket/monitor1/y
gs://somebucket/monitor1/z

在输出中我想要的是

gs://somebucket/monitor
gs://somebucket/monitor1

我已尝试关注

$ gsutil ls gs://somebucket/monitor*

$ gsutil ls gs://somebucket/monitor**

但都没有给出所需的输出

gsutil 中有没有办法实现所需的输出

【问题讨论】:

    标签: google-cloud-platform google-cloud-storage gsutil


    【解决方案1】:

    gsutil 只会在使用 ** 通配符时列出对象,这意味着除非在 somebucket 中的路径 monitor 处存在对象,否则它不会只打印 gs://somebucket/monitor。鉴于此,有两种方法可以直接使用 JSON API(提供所需的前缀和using "/" as the delimiter),或者使用不带 ** 通配符的 gsutil,通过 grep/Python/<your scripting tool of choice here> 对字符串进行一些额外处理。

    执行此操作的脚本的简单示例:

    # Say I want the objects starting with "201", but have others:
    $ gsutil ls gs://my-bucket/**
    gs://my-bucket/other-thing
    gs://my-bucket/2015/01/01/foo.jpg
    gs://my-bucket/2016/12/25/christmas.jpg
    
    $ export PATTERN="gs://my-bucket/201"
    $ gsutil ls "$(python -c "print \"${PATTERN}\"[0:\"${PATTERN}\".rfind('/')]")" | grep -o "$PATTERN[^/]*"
    gs://my-bucket/2015
    gs://my-bucket/2016
    

    【讨论】:

    • 还有一个我后来发现的简单方法:我们可以使用gsutil ls -d gs://sombucket/monitor* 来得到想要的结果
    【解决方案2】:

    您可能正在使用 zsh 作为您的 shell。在发送到 gsutil 之前,shell 会尝试在本地搜索它。尝试 gsutil ls 'gs://somebucket/monitor*' 应该可以工作(注意单引号)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-16
      • 2015-04-17
      • 1970-01-01
      • 2020-04-15
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      相关资源
      最近更新 更多