I have a backend application syncing files in Google Cloud Storage and want to list all the files in storage in javascript without requesting them from the backend. I have set up CORS and the acl of all files are public-read. The API has a paragraph about authentication:
Most of the operations you perform with the Google Cloud Storage API must be authenticated. The only exceptions are operations on objects that allow anonymous access. Objects are anonymously accessible if the AllUsers group has READ permission. The AllUsers group includes anyone on the Internet.
How can you set the permissions for a AllUsers? Is that just setting the acl to public-read?
Does the bucket list API call fall under those operations required to have authentication or is it possible to do this?
Here is what I am trying to achieve:
$.ajax({
url: "http://storage.googleapis.com/" + BUCKET_NAME,
method: "GET",
headers: {
"x-goog-project-id": PROJECT_ID
}
});
Any help would be appreciated.
You can use the JSON API to fetch the contents of a bucket from JavaScript.
The storage-metabucket-javascript project on GitHub is an example of how to do this. The bucket listing code is here. See the live demo for the working solution.
As an alternative, you could use the google-api-javascript-client library. The storage-getting-started-javascript repository on GitHub shows an example of how to use it.
You need to have the public-read acl on the bucket (instead of just on all the objects) in order to list the bucket without authorization. However, you should use public-read bucket with caution, as you might get a surprising bill at the end of the month :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With