【问题标题】:Google Cloud Storage NodeJS and JSON API not returning certain folders when trying to listGoogle Cloud Storage NodeJS 和 JSON API 在尝试列出时未返回某些文件夹
【发布时间】:2021-07-27 01:44:11
【问题描述】:

在尝试使用 NodeJS 客户端库(getFiles - https://googleapis.dev/nodejs/storage/latest/Bucket.html#getFiles) 和 JSON API (objects/list - https://cloud.google.com/storage/docs/json_api/v1/objects/list) 在这两种情况下,响应似乎都没有返回某些文件夹。

使用gsutil ls 时,它会按预期列出所有文件夹,使用 Google Cloud Console 浏览 GCS 存储桶时也会按预期列出所有文件。

Storage API 是否发生了可能导致这种情况发生的变化?

更新:

使用 NodeJS Client SDK for Storage 列出 GCS 文件夹路径的代码:

import * as functions from 'firebase-functions';
import { Storage } from '@google-cloud/storage';
import { globVars } from '../admin/admin';

const projectId = process.env.GCLOUD_PROJECT;
// shared global variables setup
const { keyFilename } = globVars;
// Storage set up
const storage = new Storage({
  projectId,
  keyFilename,
});

export const gcsListPath = functions
  .region('europe-west2')
  .runWith({ timeoutSeconds: 540, memory: '256MB' })
  .https.onCall(async (data, context) => {
    if (context.auth?.token.email_verified) {
      const { bucketName, prefix, pathList = false, fileList = false } = data;
      let list;
      const options = {
        autoPaginate: false,
        delimiter: '',
        prefix,
      };

      if (pathList) {
        options.delimiter = '/';
        const verboseResponse = await storage
          .bucket(bucketName)
          .getFiles(options);
        list = verboseResponse[2].prefixes;
      }
      if (fileList) {
        const [files] = await storage
          .bucket(bucketName)
          .getFiles(options);
        list = files.map((file) => file.name);
      }

      return { list };

    } else {
      return {
        error: { message: 'Bad Request', status: 'INVALID_ARGUMENT' },
      };
    }
  });

【问题讨论】:

  • 你能分享你写的代码吗?为了能够在我们这边重现这一点
  • @guillaumeblaquiere,感谢您的留言。我已经用我们用来列出 GCS 文件夹的代码更新了这个问题。奇怪的是,我们在使用 JSON_API 时遇到了同样的问题。
  • 还有一点需要注意的是,这个问题只是最近才出现的,过去 3 个月我们一直在使用 API 列出没有问题的文件夹路径。

标签: google-cloud-storage google-api-nodejs-client


【解决方案1】:

看起来分页功能限制了结果。实施nextQuery 返回列表的下一页。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 2018-07-13
    • 2023-03-11
    • 2016-08-23
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多