通过创建可续传上传并将该位置传递到 file.save,在不进行身份验证的情况下上传

Node.js 中的示例:https://github.com/googleapis/nodejs-storage/pull/1711

代码示例

Node.js

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

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

// The contents that you want to upload
// const contents = 'these are my contents';

// The new ID for your GCS file
// const destFileName = 'your-new-file-name';

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

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

async function uploadWithoutAuthenticationCreateResumableUploadStrategy() {
  const file = storage.bucket(bucketName).file(destFileName);

  // Returns an authenticated endpoint to which
  // you can make requests without credentials.
  const [location] = await file.createResumableUpload(); //auth required

  // Passes the location to file.save so you don't need to
  // authenticate this call
  await file.save(contents, {
    uri: location,
    resumable: true,
    validation: false,
  });

  console.log(`${destFileName} uploaded to ${bucketName}`);
}

uploadWithoutAuthenticationCreateResumableUploadStrategy().catch(
  console.error
);

后续步骤

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