前缀选项时,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不返回文件
发布评论