2016-04-28 59 views
1

我按照此鏈接中的說明操作,以便通過Python創建Google Cloud存儲桶。 https://cloud.google.com/storage/docs/xml-api/gspythonlibraryPython無法識別我編輯的BOTO文件

我遵循了所有的說明,並創建了一個包含我所有憑據的啓動文件。我打開這兩個文件,我可以看到我的gs_access_key_id和gs_secret_access_key在那裏,文件被保存。

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) 

但是,在嘗試創建create_bucket時,出現錯誤。我調試它,它回來說,gs_access_key_id從來沒有找到,但它清楚地在我的.boto文件。

這是我在嘗試在本地主機上運行此程序時得到的錯誤。

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 

所有我想知道的是爲什麼博託沒有認出我的boto文件中的憑證。

+0

作爲第一步,您可以確保您希望使用的憑證文件實際上是由boto在您的路徑上識別的嗎?我會建議這樣的代碼:print boto.pyami.config.BotoConfigLocations然後,假設工作,確保加載的配置符合您的期望:print boto.pyami.config.Config()。dump() –

+0

太棒了。這是一個很好的答案。它告訴我它正在['/etc/boto.cfg','〜/ .boto']中查找配置文件。但是,當我要求它傾倒。它說沒有。另外,當我輸入boto.pyami.config時,它會顯示「NameError:'''未定義,來自import:pyami的未定義變量。我該如何處理這個錯誤?轉儲時,我得到None。 –

+0

如果它說'兩個'沒有定義,聽起來就像一個錯字?你的意思是'博託'嗎? –

回答

0

好的,boto模塊會通過您的boto配置文件收集您的憑據,以便在Google Cloud中創建和編輯數據。如果boto模塊找不到配置文件,那麼您將得到上述錯誤。自從我試圖弄明白之後的3天,我簡直就把我的憑據放在代碼中。

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) 

就是這麼簡單。現在,您可以通過並在沒有boto配置文件的情況下執行任何您想要的操作。