简体   繁体   English

使用Python列出Google Cloud Storage存储桶

[英]List Google Cloud Storage buckets using Python

I'm following this tutorial: https://developers.google.com/storage/docs/gspythonlibrary and getting couple of errors while trying to list my buckets. 我正在关注本教程: https//developers.google.com/storage/docs/gspythonlibrary,并在尝试列出我的存储桶时遇到了一些错误。

I've downloaded gsutil and added it to my PYTHONPATH which looks like this: 我已经下载了gsutil并将其添加到我的PYTHONPATH中,如下所示:

/home/nicolasalvo/tools/gsutil/third_party/boto:/home/nicolasalvo/tools/gsutil /家庭/ nicolasalvo /工具/ gsutil会/ THIRD_PARTY /博托:/家庭/ nicolasalvo /工具/ gsutil会

I've also executed: 我还执行了:

pip install -U oauth2client

The code I'm trying to run is: 我正在尝试运行的代码是:

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

The first error I've got is: 我得到的第一个错误是:

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

Which I've fixed manually changing: 我已经修改了手动更改:

import socks

to be 成为

import httplib2.socks

Now the error I'm facing is: 现在我面临的错误是:

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

I've tried to declare the global object before using it, but it hasn't helped. 我试图在使用之前声明全局对象,但它没有帮助。

Also I would expect that I should be able to use Google provided libraries for Cloud Storage without requiring manual fixes. 此外,我希望我能够使用Google提供的库存储,而无需手动修复。

I'm running Python 2.7.3 我正在运行Python 2.7.3

Any help is greatly appreciated 任何帮助是极大的赞赏

This worked for me: 这对我有用:

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

Yes it works!! 是的有效!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中列出 Google Storage 中的存储桶? - How to list buckets from Google Storage in Python? 使用Python中的Storage JSON API列出Google存储中某个项目下的所有存储桶 - List all buckets under a project from the google storage using Storage JSON API in Python 在 Python/Django 中从 Google Cloud Storage/ Buckets 上传和检索文件 - Upload and retrieve files from Google Cloud Storage/ Buckets in Python/Django Python创建Google存储桶 - Python to create Google Storage buckets 使用Python的Google云存储 - Google Cloud Storage using Python 如何使用带有 Python 的 Google Cloud Functions 将列表写入 Google Cloud Storage 中的文件 - How to write a list to a file in Google Cloud Storage using Google Cloud Functions with Python 如何为Google Cloud Storage存储桶选择存储类别和位置 - How to choose storage class and location for Google Cloud Storage buckets 如何在谷歌云存储 python 库中正确使用 create_anonymous_client() 函数来访问公共存储桶? - How to properly use create_anonymous_client() function in google cloud storage python library for access on public buckets? Google云存储:图像无法复制到其他存储桶 - Google Cloud Storage: Images Wont Copy To Other Buckets python和谷歌云存储 - python and google cloud storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM