【问题标题】:List Google Cloud Storage buckets using Python使用 Python 列出 Google Cloud Storage 存储分区
【发布时间】:2014-01-05 17:58:22
【问题描述】:

我正在关注本教程:https://developers.google.com/storage/docs/gspythonlibrary 并在尝试列出我的存储桶时遇到了几个错误。

我已下载 gsutil 并将其添加到我的 PYTHONPATH 中,如下所示:

/home/nicolasalvo/tools/gsutil/third_party/boto:/home/nicolasalvo/tools/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

非常感谢任何帮助

【问题讨论】:

  • 有什么理由不想使用google-api-python-client
  • JSON API 被标记为实验性的,所以我认为 XML API 更稳定。我已经尝试使用 google-api-client 并且它可以工作,但我仍然有兴趣使用其他 API 解决问题..

标签: python google-cloud-storage boto gsutil


【解决方案1】:

这对我有用:

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

【讨论】:

  • 在 Ubuntu 13.10 上为我工作
【解决方案2】:
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

是的,它有效!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-23
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 2017-07-07
    • 2019-03-31
    相关资源
    最近更新 更多