[使用前缀选项时,Google Cloud Storage API不返回文件

编程入门 行业动态 更新时间:2024-10-14 00:25:03

[使用<a href=https://www.elefans.com/category/jswz/34/1768815.html style=前缀选项时,Google Cloud Storage API不返回文件"/>

[使用前缀选项时,Google Cloud Storage API不返回文件

我有以下代码:

async function gcs_test() {
  // Create a client
  const storage = new gcs.Storage()

  const [buckets] = await storage.getBuckets()
  const bucket = buckets[0]
  console.log(`Using bucket ${bucket.name}`)
  const [files] = await bucket.getFiles({prefix: 'Users/', delimiter: '/', autoPaginate: false})
  console.log('FILES:', JSON.stringify(files))
}

并且每当我使用delimiter选项时,我什么也得不到。如果我不通过delimiter,则会得到所有以“ Users /”开头的文件,例如“ Users / foo / project / 1”,“ Users / bar / project / 2”,依此类推。

我猜测设置delimiter时不返回任何内容的原因是/Users中没有任何files,只有“目录”(所有文件都更深)。所以我的问题确实是:如何在存储桶中列出给定前缀(父目录)的“目录”?

回答如下:

Google Cloud REST API返回与prefix匹配的对象的数据。这意味着目录。

如果查看REST API,您将看到返回的数据(响应)包含prefixes。这是前缀匹配的列表。处理此列表项。

Google的定义:

匹配但未列出的对象的前缀列表,最多可达和包括请求的分隔符。

示例代码显示如何处理此问题:

bucket.getFiles({prefix: 'Users/', delimiter: '/', autoPaginate: false}, function(err, files, nextQuery, apiResponse) {
    // process files[] here
    // process apiResponse.prefixes[] here (subdirectory names)
});

Google Cloud Storage Object Response Object

Google Documentation on listing objects

更多推荐

[使用前缀选项时,Google Cloud Storage API不返回文件

本文发布于:2024-05-06 22:42:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1753910.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:前缀   选项   文件   API   Cloud

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!