以分页的方式列出文件

列出存储桶中没有自动分页功能的对象,展示如何移到下一组对象。

代码示例

Node.js

如需了解详情,请参阅 Cloud Storage Node.js API 参考文档

如需向 Cloud Storage 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为客户端库设置身份验证

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

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

async function listFilesPaginated() {
  const bucket = storage.bucket(bucketName);
  const [files, queryForPage2] = await bucket.getFiles({autoPaginate: false});

  console.log('Files:');
  files.forEach(file => {
    console.log(file.name);
  });

  // Page through the next set of results using "queryForPage2"
  if (queryForPage2 !== null) {
    const [files, queryForPage3] = await bucket.getFiles(queryForPage2);

    console.log('Files:');
    files.forEach(file => {
      console.log(file.name);
    });

    // If necessary, continue cursoring using "queryForPage3"
  }
}

listFilesPaginated().catch(console.error);

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器