Google云端存储使用Python [英] Google Cloud Storage using Python

查看:171
本文介绍了Google云端存储使用Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据手册设置了Google云端存储所需的环境。
我已经安装了gsutil并设置了所有路径。
然而,当我尝试运行下面的代码时,我的gsutil工作得很好,
$ b

 #!/ usr / bin / python 

导入StringIO
导入os
导入shutil
导入tempfile
导入时间$ b $ from oauth2_plugin导入oauth2_plugin

导入boto

#Google云存储的URI方案。
GOOGLE_STORAGE ='gs'
#访问本地文件的URI方案。
LOCAL_FILE ='文件'

uri = boto.storage_uri('sangin2',GOOGLE_STORAGE)

尝试:
uri.create_bucket()
打印完成!
除了boto.exception.StorageCreateError,e:
打印失败

它会给出403访问被拒绝错误。

pre $ code Traceback最近一次调用最后一次
文件/ Volumes / WingIDE-101-4.0.0 / WingIDE.app / Contents / MacOS / src / debug / tserver / _sandbox.py,第23行,位于< module>
文件/Users/lsangin/gsutil/boto/boto/storage_uri.py,第349行,在create_bucket
返回conn.create_bucket(self.bucket_name,标题,位置,策略)
文件/Users/lsangin/gsutil/boto/boto/gs/connection.py,第91行,在create_bucket
response.status,response.reason,body)
boto.exception.GSResponseError:GSResponseError :403 Forbidden
<?xml version ='1.0'encoding ='UTF-8'?>< Error>< Code> AccessDenied< / Code>< Message> Access denied。< / Message> ;< /错误>

由于我是新手,因此我很难找出原因。
有人可以帮我吗?
谢谢。

解决方案

boto库应该会自动查找并使用您的$ HOME / .boto文件。需要检查的一件事:确保您使用的项目被设置为传统访问的默认项目(在API控制台上,单击存储访问并验证它是否显示这是您的默认项目,用于传统访问)) 。当我设置不正确,我按照你引用的创建存储区的例子,我也得到了一个403错误,然而,它没有任何意义,这将在gsutil中适用于你,但不能直接使用boto。



尝试在实例化storage_uri对象时添加debug = 2,如下所示:

  uri = boto.storage_uri(name,GOOGLE_STORAGE,debug = 2)

有关stdout的额外调试信息,然后您可以将其与类似的工作gsutil示例(通过gsutil -D mb)的调试输出进行比较。


I set up a required environment for Google Cloud Storage according to the manual. I have installed "gsutil" and set up all paths. My gsutil works perfectly, however, when I try to run the code below,

#!/usr/bin/python

import StringIO
import os
import shutil
import tempfile
import time
from 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('sangin2', GOOGLE_STORAGE)

try:
    uri.create_bucket()
    print "done!"
except boto.exception.StorageCreateError, e:
    print "failed"

It gives "403 Access denied" error.

  Traceback (most recent call last):
      File "/Volumes/WingIDE-101-4.0.0/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 23, in <module>
      File "/Users/lsangin/gsutil/boto/boto/storage_uri.py", line 349, in create_bucket
        return conn.create_bucket(self.bucket_name, headers, location, policy)
      File "/Users/lsangin/gsutil/boto/boto/gs/connection.py", line 91, in create_bucket
        response.status, response.reason, body)
    boto.exception.GSResponseError: GSResponseError: 403 Forbidden
      <?xml version='1.0' encoding='UTF-8'?><Error><Code>AccessDenied</Code><Message>Access denied.</Message></Error>

Since I am new to this, it is kinda hard for me to figure out why. Can someone help me? Thank you.

解决方案

The boto library should automatically find and use your $HOME/.boto file. One thing to check: make sure the project you're using is set as your default project for legacy access (at the API console, click on "Storage Access" and verify that it says "This is your default project for legacy access"). When I have that set incorrectly and I follow the create bucket example you referenced, I also get a 403 error, however, it doesn't make sense that this would work for you in gsutil but not with direct use of boto.

Try adding "debug=2" when you instantiate the storage_uri object, like this:

uri = boto.storage_uri(name, GOOGLE_STORAGE, debug=2)

That will generate some additional debugging information on stdout, which you can then compare with the debug output from an analogous, working gsutil example (via gsutil -D mb ).

这篇关于Google云端存储使用Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆