Google Container Engine: Accessing Cloud Storage

2020-04-20 22:37发布

问题:

I can't get the Application Default Credentials working in Google Container Engine. The docs say that they're intended for App Engine and Compute Engine, but I've been told that they should transparently pass through to a container running on Container Engine.

Here's the code that's failing:

credentials = GoogleCredentials.get_application_default()
service = discovery.build('storage', 'v1', credentials=credentials)

The error it's failing with: AssertionError: No api proxy found for service "memcache"

Is it correct to expect Application Default Credentials to work with Container Engine? If not, can anyone recommend the proper way to connect to cloud storage from a container running on Container Engine?

Thanks.

EDIT: After running gcloud beta auth application-default activate-service-account --key-file <my_key>.json the credentials object in the above Python example is populated with data. However I'm still getting the same error.

回答1:

The easiest way to access cloud storage on Container Engine appears to be the gcloud library.

It won't work with zero configuration if the App Engine SDK is installed on the container (gcloud finds it and assumes it's running on App Engine). So I stopped using the official Google container.

On a python container just add gcloud to your requirements.txt (or run pip install gcloud). As long as the container lives on a Google Compute Engine instance you can access any bucket in the same project.

Example:

from gcloud import storage
client = storage.Client()
bucket = client.get_bucket('<bucket_name>')
blob = bucket.blob('test_file.txt')
blob.upload_from_string('test content')

The gcloud library also exists for Java, Node, and Ruby.