Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List files in a bucket without authorisation in javascript from Google Cloud Storage

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.

like image 697
georgephillips Avatar asked Sep 05 '25 03:09

georgephillips


2 Answers

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.

like image 67
jterrace Avatar answered Sep 07 '25 19:09

jterrace


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 :)

like image 44
yzhihong Avatar answered Sep 07 '25 19:09

yzhihong