1
votes

I am following the instructions that are in this link in order to create a Google Cloud storage bucket through Python. https://cloud.google.com/storage/docs/xml-api/gspythonlibrary

I have followed all of the instructions and created a boot file with all of my credentials. I opened the both file and I can see that my gs_access_key_id and gs_secret_access_key are there and the file is saved.

import webapp2
import sys
 sys.path.append("/Library/Frameworks/Python.framework/Versions/2.7/lib/py thon2.7/site-packages")
import boto 
import gcs_oauth2_boto_plugin

import os
import shutil
import StringIO
import tempfile
import time

GOOGLE_STORAGE = 'gs'
LOCAL_FILE = 'file'

CLIENT_ID = ''
CLIENT_SECRET = ''
gcs_oauth2_boto_plugin.SetFallbackClientIdAndSecret(CLIENT_ID,     CLIENT_SECRET)

class MainHandler(webapp2.RequestHandler):
def get(self):
    self.response.write('Hello world! This should work ! I have been working no this shit all day!')
    now = time.time()
    CATS_BUCKETS = 'cats-%d' % now
    DOGS_BUCKETS = 'cats-%d' % now
    project_id = 'name-of-my-bucket'

    for name in (CATS_BUCKETS, DOGS_BUCKETS):
        uri = boto.storage_uri(name, GOOGLE_STORAGE)
        try:
            header_values={'x-google-project-id': project_id}
            uri.create_bucket()
            print 'Successfully created bucket "%s"' %name
        except boto.exception.StorageCreateError, e:
            print 'Failed to create bucket:', e

app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)

However, at the line that it tries to create_bucket, I get an error. I debugged it and it comes back saying that the gs_access_key_id was never found, however it is clearly in my .boto file.

This is the error that I get when I try to run this program in localhost.

  File "/Users/LejendVega/Desktop/create-buckets-adrian/main.py", line 48, in get
uri.create_bucket()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/boto/storage_uri.py", line 558, in create_bucket
conn = self.connect()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/boto/storage_uri.py", line 140, in connect
**connection_args)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/boto/gs/connection.py", line 47, in __init__
suppress_consec_slashes=suppress_consec_slashes)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/boto/s3/connection.py", line 191, in __init__
validate_certs=validate_certs, profile_name=profile_name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/boto/connection.py", line 569, in __init__
host, config, self.provider, self._required_auth_capability())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/boto/auth.py", line 989, in get_auth_handler
'Check your credentials' % (len(names), str(names)))
NoAuthHandlerFound: No handler was ready to authenticate. 3 handlers were checked. ['OAuth2Auth', 'OAuth2ServiceAccountAuth', 'HmacAuthV1Handler'] Check your credentials

All I want to know is why the boto is not recognizing my credentials that are in my boto file.

1
As a first step, can you make sure that your credentials file you hope to use is actually being recognized on your path by boto? I'd suggest code like this: print boto.pyami.config.BotoConfigLocations Then, assuming that works, ensure the loaded config matches your expectations: print boto.pyami.config.Config().dump() - Travis Hobrla
Awesome. That was a good answer. It shows me that it is looking for the configs files in ['/etc/boto.cfg', '~/.boto']. However when I ask it to dump. it says none. Also, when I type in boto.pyami.config, it says "NameError: 'both' is not defined, undefined variable from import: pyami". What do I do about this error? I get None for the dump. - Lejend Vega
If it says 'both' is not defined, sounds just like a typo? Did you mean 'boto'? - Travis Hobrla

1 Answers

0
votes

Okay so the boto module goes through your boto configuration file to gather your credentials in order to create and edit data in the Google Cloud. If the boto module can not find the configuration file then you will get the errors above. What I did was since, after 3 days straight of trying to figure it out, I literally just put my credentials in the code.

from boto import connect_gs

name = 'the-name-of-your-bucket'
gs_conn = connect_gs(gs_access_key_id='YOUR_ACCESS_KEY_ID',
                          gs_secret_access_key='YOUR_ACCESS_SECRET_KEY')
    """This is the line that creates the actual bucket"""
gs_conn.create_bucket(name)

It is that simple. Now you can go through and do anything that you want to without the boto configuration file.