谷歌云存储目录大小

Sha*_*hor 8 node.js google-cloud-storage

我试图找出如何计算 google-cloud-storage 存储桶中目录的大小。

到目前为止,我找到了两种方法:

  • 使用gsutils(我真的不能,因为它在我需要这样做的机器上不可用)
  • 使用库中的getMetadata方法nodejs

问题是该getMetatada方法仅适用于文件,而不适用于目录(或称为前缀)。

我可以列出所有文件并询问它们的大小,但getMetadata不允许查询批处理,我可能会让人们对@google 感到生气。

你们知道有什么方法可以以尊重的方式正确地做到这一点吗?

真挚地

Man*_*ngu 9

If you were able to use gsutil, you could use du, like this:

gsutil du -sh YOUR_BUCKET/YOUR_DIRECTORY

The -s flag will give you only the size of the directory, if you remove it you will also see the size of the files inside. The -h flag returns the size in a human-readable format (KiB,MiB, etc). If not present, it will display the size in bytes.

But, since you mention you can't do it like that, you will have to use, as mentioned, getMetadata.

This simple script will show the size in the console, in bytes, of all of your files in the folder you wish. You can later modify it to add up those sizes.

var http = require("http");
http.createServer(function (request, response) {
   const Storage = require('@google-cloud/storage');
   const projectId = "PROJECT-ID";
   const bucketName = "BUCKET-NAME";
   const storage = Storage({
       projectId: projectId,
   });
  const options = {
    prefix: "FOLDER-IN-BUCKET/"
  }
  storage
    .bucket(bucketName)
    .getFiles(options)
    .then(results => {
        const files = results[0];
        files.forEach(file => {
          storage.
            bucket(bucketName)
            .file(file.name)
            .getMetadata()
            .then(metadata_results => {
                const metadata = metadata_results[0];
                console.log(metadata.size);
                console.log(metadata.name);
          }).catch(metadata_err => {
            console.error(metadata_err);
          });
        });
     }).catch(err => {
        console.error(err);
    });
   response.writeHead(200, {'Content-Type': 'text/plain'});
   response.end('Hello World\n');
}).listen(8080);
console.log("Hello World");
Run Code Online (Sandbox Code Playgroud)

Don't forget to add the dependency for Cloud Storage in your package.json.

你可以按照这个建议,使用每日存储日志,并从节点读取所述日志