簡體   English   中英

使用Python列出Google Cloud Storage存儲桶

[英]List Google Cloud Storage buckets using Python

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

我已經下載了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

任何幫助是極大的贊賞

這對我有用:

import threading
from gslib.third_party.oauth2_plugin import oauth2_client
oauth2_client.token_exchange_lock = threading.Lock()
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

是的有效!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM