Chris
Chris

Reputation: 4372

Make files publicly readable, but not whole bucket

I'm uploading files from my server to my bucket foobar. The path of the uploaded file is foobar/uploads/avatars/123456789.jpg.

I want users to be able to access this file via a public https link like https://storage.googleapis.com/foobar/uploads/avatars/123456789.jpg. The link has to be accessible without logging into any google account. I do not want users to be able to browse the whole bucket by a link like https://storage.googleapis.com/foobar/, https://storage.googleapis.com/foobar/uploads/avatars/ or somehow different.

I am using @google-cloud/storage to upload the files. How can I achieve these settings with ACL?

Upvotes: 0

Views: 56

Answers (1)

Brandon Yarbrough
Brandon Yarbrough

Reputation: 38369

For an existing object:

file.makePublic(function(err, apiResponse) {});

When uploading a new object, use the option public, which is the equivalent of specifying options.predefinedAcl = 'publicRead'. Depending on how you're doing the upload, you might change your createWriteStream call like so:

file.createWriteStream({public: true, metadata: {...}})

Upvotes: 3

Related Questions