2
votes

A simple Python code to list obj in certain folder in Google Cloud Storage:

from apiclient import discovery
import apiclient
import json

client = discovery.build('storage', 'v1beta2')

request = client.objects().list(
    bucket = 'mybucket',
    prefix = 'myfolder1/myfolder',
    key =   'A0rsER3odwksawsesse3Dw_d3Ks') # my API key

try:
    response = request.execute()
    print json.dumps(response, indent = 2)

except apiclient.errors.HttpError, e:
    print e

Then I got below failure message:

https://www.googleapis.com/storage/v1beta2/b/mybucket/o?prefix=myfolder1%2Fmyfolder&alt=json&key=A0rsER3odwksawsesse3Dw_d3Ks returned "*Access Not Configured. Please use Google Developers Console to activate the API for your project.*">

I have enabled the API for my project, so it should not be the problem as the return message said, maybe it jump to incorrect project? As I know the flow should be -> project in GCS -> bucket -> root-folder -> folder-1.

So my question is how do this code know which project I'm using? Do I miss any code here?

Thanks for all the kind help!

1

1 Answers

4
votes

API key alone is only good for accessing data in public buckets. GCS is able to map API usage back to your project by looking at the API key, which is unique. For anything else than public data access you need proper authentication, via OAuth2, for example. More info here: https://developers.google.com/storage/docs/json_api/v1/how-tos/authorizing

Your code looks OK and it works with public buckets, which I've verified. Ensure you have both Google Cloud Storage and Google Cloud Storage JSON API enabled in Developers Console.