node.js - 使用 Node.js 列出 Google Cloud Storage 存储桶中的文件夹

标签 node.js google-cloud-platform google-cloud-storage

我一直在寻找一种方法来仅检索存储桶内的目录,而不检索其中的内容。

根据 Google Cloud Storage Docs可以按前缀过滤:

const Storage = require('@google-cloud/storage');  

const storage = new Storage();

const options = {
  prefix: prefix,
  delimiter: '/'
};
    
storage
  .bucket(bucketName)
  .getFiles(options)
  .then(results => {
    const files = results[0];

    console.log('Files:');
    files.forEach(file => {
      console.log(file.name);
    });
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

我已经尝试使用前缀:""、或前缀:"*"或前缀:"\"等进行多种组合,但我无法使其仅返回文件夹。

有人能做到吗?

最佳答案

事实上,getFiles 方法并没有按照您使用它的方式读取文件夹。我已经调整了this post中的代码打印我的水桶:

const storage = require('@google-cloud/storage');

const projectId = 'PROJECT_ID';
const gcs = storage({
    projectId: projectId
});

let bucketName = 'BUCKET_NAME';
let bucket = gcs.bucket(bucketName);
// bucket.getFiles({}, (err, files,apires) => {console.log(err,files,apires)});


let cb=(err, files,next,apires) => {
    /*console.log('Err:');
    console.log('Files:');
    console.log(files);*/
    console.log('Apires:');
    console.log(typeof apires);
    console.log(apires);
    console.log(apires.prefixes);
    if(!!next)
    {   
        bucket.getFiles(next,cb);
    }   
}

bucket.getFiles({delimiter:'/', autoPaginate:false}, cb);

/* 
// List folders inside folders
let cb=(err, files,next,apires) => {
    console.log(err,files,apires);
    if(!!next)
    {
        bucket.getFiles(next,cb);
    }
}
bucket.getFiles({prefix:'foo/', delimiter:'/', autoPaginate:false}, cb);
*/ 

而且,即使这在这种情况下没有用,我建议您使用 regular expression确定名称的特定格式。

关于node.js - 使用 Node.js 列出 Google Cloud Storage 存储桶中的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50659628/

相关文章:

ssl - 可以在静态 GCP 网站上提供 HTTPS 服务吗?

javascript - 在服务器上实现 HTML DOM 重写逻辑

node.js - npm audit 只针对生产依赖?

node.js - 如何使用 mocha 和 chai 正确测试 Promise?

google-bigquery - 使用 Google Data Studio 分析 Google Cloud Datastore 中的数据

node.js - GCF Node10 部署失败 : "Function failed on loading user code. Error message: Provided code is not a loadable module."

eclipse - 使用 Eclipse 调试 Node.js 应用程序

python - 在 Google Cloud VM 上加速和优化 scikit-learn

java - 谷歌CP : Unable to try Cloud Billing Account API from Java

python - 未在提供的 Blob 的 Flask App Engine 响应上设置内容长度 header