재개 가능한 업로드를 수동으로 시작할 수 있도록 서명된 URL을 사용하여 인증 없이 업로드

Node.js의 예시: https://github.com/googleapis/nodejs-storage/pull/1711

코드 샘플

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';

// 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');

const fetch = require('node-fetch');

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

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

  // Use signed URLs to manually start resumable uploads.
  // Authenticating is required to get the signed URL, but isn't
  // required to start the resumable upload
  const options = {
    version: 'v4',
    action: 'resumable',
    expires: Date.now() + 30 * 60 * 1000, // 30 mins
  };
  //auth required
  const [signedUrl] = await file.getSignedUrl(options);

  // no auth required
  const resumableSession = await fetch(signedUrl, {
    method: 'POST',
    headers: {
      'x-goog-resumable': 'start',
    },
  });

  // Endpoint to which we should upload the file
  const location = resumableSession.headers.location;

  // 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}`);
}

uploadWithoutAuthenticationSignedUrlStrategy().catch(console.error);

다음 단계

다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저를 참조하세요.