Monday 10 April 2017

Nodejs Google cloud storage returns 403 when accessing public url

I'm uploading a profile image to google cloud store and trying to make all of the images publicly available, however I keep getting a 403 error shown at the bottom of the post.

The code below shows how i set google cloud store up:

const gcs = require('@google-cloud/storage')({
  projectId: ‘my_project_id’,
  keyFilename: './routes/to/my/keyfile.json'
});

const bucket = gcs.bucket(‘my_bucket_name’);

const options = {
  entity: 'allUsers',
  role: gcs.acl.READER_ROLE
};

bucket.acl.add(options, (err, aclObject) => {
  console.log(err); //null
  console.log(aclObject); //{ entity: 'allUsers', role: 'READER' }
});

function getPublicUrl(filename) {
  return `https://storage.googleapis.com/${bucketName}/${filename}`;
}

... code for uploading the image, (works fine)

I store the return value of getPublicUrl inside of the database and send it back to the client, the url looks as follows:

https://storage.googleapis.com/my_bucket_name/12370691_1205162746164520_2152367844899316112_o.jpg

However when visiting the url or using it inside of img src it gives a 403 error:

Anonymous users does not have storage.objects.get access to object my_bucket_name/15991995_384418798568801_734448169_o.jpg.

note: my_bucket_name is not the real name, it's just a placeholder for this post



via linas mnew

No comments:

Post a Comment