2013-12-17 87 views
2

我正在關注本教程:https://developers.google.com/storage/docs/gspythonlibrary並嘗試列出我的存儲桶時遇到了一些錯誤。使用Python列出Google Cloud Storage存儲桶

我已經下載的gsutil並把它添加到我的PYTHONPATH,看起來像這樣:

/家庭/ nicolasalvo /工具/ gsutil會/ THIRD_PARTY /博託:/家庭/ nicolasalvo /工具/ gsutil會

我「以前也執行:

pip install -U oauth2client 

我試圖運行的代碼是:

import StringIO 
import os 
import shutil 
import tempfile 
import time 
from gslib.third_party.oauth2_plugin import oauth2_plugin 

import boto 

# URI scheme for Google Cloud Storage. 
GOOGLE_STORAGE = 'gs' 
# URI scheme for accessing local files. 
LOCAL_FILE = 'file' 

uri = boto.storage_uri('', GOOGLE_STORAGE) 
for bucket in uri.get_all_buckets(): 
    print bucket.name 

的我已經得到了第一個錯誤是:

File "<stdin>", line 1, in <module> 
File "/home/nicolasalvo/tools/gsutil/gslib/third_party/oauth2_plugin/oauth2_plugin.py", line 3, in <module> 
import oauth2_client 
File "/home/nicolasalvo/tools/gsutil/gslib/third_party/oauth2_plugin/oauth2_client.py", line 33, in <module> 
import socks 

我已經固定的手動改變:

import socks 

import httplib2.socks 

現在我面臨的錯誤是:

File "/home/nicolasalvo/tools/gsutil/third_party/boto/boto/connection.py", line 876, in _mexe 
request.authorize(connection=self) 
File "/home/nicolasalvo/tools/gsutil/third_party/boto/boto/connection.py", line 377, in authorize 
connection._auth_handler.add_auth(self, **kwargs) 
File "/home/nicolasalvo/tools/gsutil/gslib/third_party/oauth2_plugin/oauth2_plugin.py", line 22, in add_auth 
self.oauth2_client.GetAuthorizationHeader() 
File "/home/nicolasalvo/tools/gsutil/gslib/third_party/oauth2_plugin/oauth2_client.py", line 325, in GetAuthorizationHeader 
return 'Bearer %s' % self.GetAccessToken().token 
File "/home/nicolasalvo/tools/gsutil/gslib/third_party/oauth2_plugin/oauth2_client.py", line 288, in GetAccessToken 
token_exchange_lock.acquire() 
NameError: global name 'token_exchange_lock' is not defined 

我試過宣佈全球對象等使用它之前,但它沒有幫助。

此外,我希望我應該能夠使用Google提供的雲存儲庫,而無需手動修復。

我運行的Python 2.7.3

任何幫助是極大的讚賞

+0

你不想使用[google-api-python-client](https://code.google.com/p/google-api-python-client/)的任何理由? – jterrace

+0

JSON API已標記爲實驗性,所以我通過XML API更穩定。我已經嘗試過使用google-api-client並且它可以工作,但是我仍然會對解決其他API的問題感興趣。 – ROOTto

回答

6

這爲我工作:

import threading 
from gslib.third_party.oauth2_plugin import oauth2_client 
oauth2_client.token_exchange_lock = threading.Lock() 
+0

適用於Ubuntu 13.10 –

4
import StringIO 
import os 
import shutil 
import tempfile 
import time 
import threading 
import boto 

from gslib.third_party.oauth2_plugin import oauth2_plugin 
from gslib.third_party.oauth2_plugin import oauth2_client 

oauth2_client.token_exchange_lock = threading.Lock() 

# URI scheme for Google Cloud Storage. 
GOOGLE_STORAGE = 'gs' 

# URI scheme for accessing local files. 
LOCAL_FILE = 'file' 

project_id = 'abc.com:abc' 

uri = boto.storage_uri('', GOOGLE_STORAGE) 

header_values = {"x-goog-project-id": project_id} 

for bucket in uri.get_all_buckets(headers=header_values): 
    print bucket.name 

是的,它的作品!

相關問題