2
votes

I would like to upload directly to a Google Cloud Storage bucket from my client-side JavaScript code, although the bucket should not be publicly writeable. The server side code is Node.js. In the past I have been able to upload directly to Amazon S3, by generating temporary authorization for the client on the server. What is the procedure for uploading a file to a write-protected Google Cloud Storage bucket, i.e. which requires authorization?

1

1 Answers

2
votes

I've found that I can use getSignedUrl, which is supposed to allow certain temporary access (e.g. for writing) to a file:

bucket = gcs.bucket('aBucket')
bucket.file('aFile').getSignedUrl({
  action: 'write',
  expires: moment.utc().add(1, 'days').format(),
}, (error, signedUrl) => {
  if (error == null) {
    console.log(`Signed URL is ${signedUrl}`)
  }
})

In order to upload a file, issue a PUT request to the obtained signed URL.