gcloud-node - How to ensure files uploaded to Google Cloud Storage via signed URL are publicly readable?


aknuds1

I want to allow clients (JavaScript in browsers) to upload files to Google Cloud Storage, which I currently do by generating signed URLs . However, uploaded files are private by default. How can I ensure that files uploaded via signed URLs are publicly readable?

aknuds1

Control file ACLs are handled via the header x-goog-acl , which must also be included when computing the signed URL. For better public readability, use values .public-read

Client code example:

let request = new XMLHttpRequest()
request.onreadystatechange = () => {
  if (request.readyState === XMLHttpRequest.DONE) {
    if (request.status === 200) {
      console.log(`Google Cloud Storage upload was successful:`, request.responseText)
    } else {
      console.log(`Google Cloud Storage upload was not successful: ${request.status}`)
    }
  }
}

request.open('PUT', signedUrl, true)
// Don't care about content-type header for signature calculation
request.setRequestHeader('content-type', 'ignore')
request.setRequestHeader('x-goog-acl', 'public-read')
request.send(file)

Server-side code example (for getting the signed URL):

let gcloud = require('gcloud')
let moment = require('moment')

let gcs = gcloud.storage({
  projectId: process.env.GCLOUD_PROJECT_ID,
  credentials: {
    client_email: process.env.GCLOUD_CLIENT_EMAIL,
    private_key: process.env.GCLOUD_PRIVATE_KEY,
  },
})

let bucket = gcs.bucket('aBucket')
let cloudFile = bucket.file('aFile')
cloudFile.getSignedUrl({
  action: 'write',
  expires: moment.utc().add(1, 'days').format(),
  contentType: 'ignore',
  extensionHeaders: {'x-goog-acl': 'public-read',},
}, (error, signedUrl) => {
  if (error != null) {
    console.log(`Failed to obtain signed URL for file`)
  } else {
    console.log(`Got signed URL for file: ${signedUrl}`)
  }
})

Related


Put files into Google Cloud Storage (GCS) via signed URL

kctang: I'm trying to use a "signed URL" to access/upload files to Google Cloud Storage (GCS). Follow the instructions at https://developers.google.com/storage/docs/accesscontrol#Signing-Strings what did I do: "Web Application" is registered in Google Cloud Co

Put files into Google Cloud Storage (GCS) via signed URL

tea soup I'm trying to use a "signed URL" to access/upload files to Google Cloud Storage (GCS). Follow the instructions at https://developers.google.com/storage/docs/accesscontrol#Signing-Strings what did I do: "Web Application" is registered in Google Cloud C

Put files into Google Cloud Storage (GCS) via signed URL

kctang: I'm trying to use a "signed URL" to access/upload files to Google Cloud Storage (GCS). Follow the instructions at https://developers.google.com/storage/docs/accesscontrol#Signing-Strings what did I do: "Web Application" is registered in Google Cloud Co

Put files into Google Cloud Storage (GCS) via signed URL

kctang: I'm trying to use a "signed URL" to access/upload files to Google Cloud Storage (GCS). Follow the instructions at https://developers.google.com/storage/docs/accesscontrol#Signing-Strings what did I do: "Web Application" is registered in Google Cloud Co

Upload to Google Cloud Storage via signed URL - object not public

username I followed this tutorial to allow direct upload of files from the GWT frontend to Google Cloud Storage using signed URLs. I extended the Java example by specifying a content-type that works fine. Then, I found out that files uploaded this way are not

Upload to Google Cloud Storage via signed URL - object not public

username I followed this tutorial to allow direct upload of files from the GWT frontend to Google Cloud Storage using signed URLs. I extended the Java example by specifying a content-type that works fine. Then, I found out that files uploaded this way are not

Upload to Google Cloud Storage via signed URL - object not public

username I followed this tutorial to allow direct upload of files from the GWT frontend to Google Cloud Storage using signed URLs. I extended the Java example by specifying a content-type that works fine. Then I see that files uploaded this way are not publicl

Upload to Google Cloud Storage via signed URL - object not public

username I followed this tutorial to allow direct upload of files from the GWT frontend to Google Cloud Storage using signed URLs. I extended the Java example by specifying a content-type that works fine. Then I see that files uploaded this way are not publicl

How to get URL to Google Cloud Storage file using gcloud-node?

aknuds1 Using the gcloud Node library , how can I get the URL of a file in a Cloud Storage bucket? Consider the instantiation of the following file object: let bucket = gcs.bucket(`aBucket`) let cloudFile = bucket.file(`aFile`) I want to get the download URL

How to shorten google cloud storage signed download url?

Brandon I have an firebaseapp where I use to Firebase Storageupload pictures. URLWhen I use firebase web sdkupload , si returns reasonable : https://firebasestorage.googleapis.com/v0/b/projectId.appspot.com/o/image.jpg?alt=media&token=51183d4a-551a-41e2-b620-1