Ohne Authentifizierung hochladen durch Verwendung einer signierten URL zum manuellen Starten fortsetzbarer Uploads

Beispiel in Node.js: https://github.com/googleapis/nodejs-storage/pull/1711

Codebeispiel

Node.js

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage Node.js API.

Richten Sie die Standardanmeldedaten für Anwendungen ein, um sich bei Cloud Storage zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

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

Nächste Schritte

Informationen zum Suchen und Filtern von Codebeispielen für andere Google Cloud-Produkte finden Sie im Google Cloud-Beispielbrowser.