Guy Guy - 2 months ago 48
Node.js Question

Google storage bucket is public and still no access to files

I am creating a public bucket with nodeJS (apologise for the ES5/ES6 mix, copied Google's example were ES5):

var gcloud = require('google-cloud');

const gcs = gcloud.storage({
projectId: 'h-212f6',
keyFilename: './h-9a814129651f4.json'
});

const createBucket = (bucketName) => {
// Create a new bucket.

return new Promise((resolve, reject) => {

gcs.createBucket(bucketName, function (err, bucket) {
if (err) {
reject(err);
}

bucket.acl.add({
entity: 'allUsers',
role: gcloud.storage.acl.READER_ROLE
}, function(err, aclObject) {
console.log('err -> ', err);
});

resolve(bucket);
});

});
};

const upload = (bucket, filepath, options) => {

return new Promise((resolve, reject) => {

bucket.upload(filepath, options, function (err, file) {

if (err) {
reject(err);
}

resolve(file)
});
});
};


Yet having trouble accessing the file from Chrome:

chrome error

UPDATE:

When I change the file's ACL all seems to work:

const upload = (bucket, filepath, options) => {

return new Promise((resolve, reject) => {

bucket.upload(filepath, options, function (err, file) {

if (err) {
reject(err);
}

file.acl.add({
entity: 'allUsers',
role: gcs.acl.READER_ROLE
}, function (err, aclObject) {

console.log('err -> ', err);

});

resolve(file)
});
});
};


However, I was under the assumption that changing the bucket ACL should be enough, isn't it?

Answer

When you set the ACL in your code above, you're setting it for existing objects. In order to set it as the default ACL for new objects, too, you need to do the following:

bucket.acl.default.add({
    entity: 'allUsers',
    role: gcloud.storage.acl.READER_ROLE
}, function(err, aclObject) {
    console.log('err -> ', err);
});

See here for more examples.