问题描述:

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

网友答案:

In order to list object in the bucket you must make your bucket anonymously readable. Go to developer console Storage->Browser. In Edit permissions choose/write User - allUsers - Read. Files inside bucket can have any ACL.

After you can list files as follow:

Storage.bucket = 'mybucket';

Storage.listObjectsItems = function(prefix, delimiter, callback) {
  var request = gapi.client.storage.objects.list({
    'bucket' : Storage.bucket,
    'prefix' : prefix,
    'delimiter' : delimiter
  });
  request.execute(function(resp) {
    console.log(resp);
    callback(resp.items);
  });
 };

Storage.listObjectsPrefixes = function(prefix, delimiter, callback) {
var request = gapi.client.storage.objects.list({
    'bucket' : Storage.bucket,
    'prefix' : prefix,
    'delimiter' : delimiter
});
request.execute(function(resp) {
    console.log(resp);
    callback(resp.prefixes);
});
 };

You can read more about prefixes and delimiters here: https://cloud.google.com/storage/docs/json_api/v1/objects/list

Specific example: Let say we have next bucket structure in console->storage->browser:

mybucket/folder1/folder11/..
                 folder12/..
                 folder13/..
                   ...
                 folder1n/..

call

Storage.listObjectsPrefixes('folder1/', '/',callback );

it will return (as argument of callback) all folders inside folder1.

I don't see(probably wrong?) finance danger to make bucket anonymously readable. It is only must be accessible from limited domains and why somebody will browse content of my bucket, even he discovered (how?) that such bucket exists.

相关阅读:
Top