【问题标题】:Listing files in Google Cloud bucket with thousands of files.列出包含数千个文件的 Google Cloud 存储桶中的文件。
【发布时间】:2019-04-16 17:27:26
【问题描述】:

我正在运行一个 Node.js 脚本来获取 Google Cloud Storage 上存储桶中的文件数。

在一个包含大约 30K 个文件的存储桶中,我在几秒钟内就得到了结果。在包含大约 300K 文件的存储桶中,我收到以下错误:

<--- Last few GCs --->

[10508:0000014DB738ADB0]  2053931 ms: Mark-sweep 1400.6 (1467.7) -> 1400.6 (1437.2) MB, 1292.2 / 0.0 ms  (+ 0.0 ms in 0 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 1292 ms) last resort GC in old space requested
[10508:0000014DB738ADB0]  2055233 ms: Mark-sweep 1400.6 (1437.2) -> 1400.6 (1437.2) MB, 1301.9 / 0.0 ms  last resort GC in old space requested


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 000001A6B8025EE1 <JSObject>
    1: /* anonymous */(aka /* anonymous */) [D:\Libraries\Documents\project-name\node_modules\@google-cloud\storage\src\acl.js:~717] [pc=0000005E62D95DCF](this=0000016DB7602311 <undefined>,accessMethod=0000016DB7602AC1 <String[3]: add>)
    2: arguments adaptor frame: 3->1
    3: forEach(this=00000335A20E8891 <JSArray[2]>)
    4: /* anonymous */(a...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

下面是我正在使用的代码。有没有更好的办法?

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

function listFiles(bucketName) {
  // [START storage_list_files]
  // Imports the Google Cloud client library


  // Creates a client
  const storage = new Storage();

  /**
   * TODO(developer): Uncomment the following line before running the sample.
   */
  // const bucketName = 'Name of a bucket, e.g. my-bucket';

  // Lists files in the bucket
  return storage
    .bucket(bucketName)
    .getFiles(); ///const files = results[0];
  // [END storage_list_files]
}

listFiles('bucket-name')
    .then(x => {
        console.log('Number of files: ', x[0].length)
    });

【问题讨论】:

  • 您的问题是内存不足,无法存储返回的结果。您将需要使用 API 并对输出进行分页。这将限制每个响应返回的项目数。
  • 如何使用 API 获取文件列表?你能链接到文档吗?

标签: javascript node.js google-app-engine google-cloud-platform google-cloud-storage


【解决方案1】:

大多数返回列表的方法都提供了该方法的流式传输版本。在这种情况下,您需要使用 bucket.getFilesStream()

bucket.getFilesStream()
  .on('error', console.error)
  .on('data', function(file) {
    // file is a File object.
  })
  .on('end', function() {
    // All files retrieved.
  });

或者,您可以通过结果禁用自动分页和手册页

const callback = function(err, files, nextQuery, apiResponse) {
  if (nextQuery) {
    // More results exist.
    bucket.getFiles(nextQuery, callback);
  }
};

bucket.getFiles({
  autoPaginate: false
}, callback);

【讨论】:

    【解决方案2】:

    正如 cmets 中指出的,您应该使用 Objects: list API 来列出大型存储桶。

    另外,如果我正确阅读了 library documentation,您可以将 autoPaginate 选项设置为 false 并手动迭代结果,而无需直接与 JSON api 对话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      • 2020-09-06
      • 2013-02-16
      • 1970-01-01
      • 2020-01-24
      • 2018-07-03
      • 1970-01-01
      相关资源
      最近更新 更多