使用Python的Google云端存储
问题描述:
根据手册设置了Google云端存储所需的环境。 我已经安装了“gsutil”并设置了所有路径。 我的gsutil完美的作品,但是,当我尝试运行下面的代码,使用Python的Google云端存储
#!/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"
它给“403次拒绝访问”错误。
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>
由于我是新来这个,它是有点难受,找出原因。 有人可以帮我吗? 谢谢。
答
boto库应该会自动查找并使用您的$ HOME/.boto文件。需要检查的一件事:确保您使用的项目被设置为传统访问的默认项目(在API控制台上,单击“存储访问”并验证它是否显示“这是您的默认项目,用于传统访问”)) 。当我设置不正确,并且按照您引用的创建存储区示例进行操作时,我也收到了403错误,但是,这对您在gsutil中的工作并不合适,但不能直接使用boto。
尝试增加“调试= 2”时,实例化storage_uri对象,像这样:
uri = boto.storage_uri(name, GOOGLE_STORAGE, debug=2)
这将产生在stdout一些附加的调试信息,然后可以与调试输出从类似的比较,运行gsutil示例(通过gsutil -D mb)。
您是否在Google API控制台中激活了Google存储空间?您在哪里设置了需要使用Google服务进行身份验证的oauth client_id,client_secret等? – 2012-02-10 10:18:15
是的,我想我做到了,当我安装了一个gsutil,它工作正常。但是,上面的代码给了我“403禁止”的错误。我设置Python路径的指示(出口PYTHONPATH = $ {PYTHONPATH}:$ HOME/gsutil会/博托:$ HOME/gsutil会),但它仍然给我的问题。 – 2012-02-10 18:50:58
那么这可能有点帮助http://groups.google.com/group/gs-discussion/browse_thread/thread/5abe343e35a6d842?pli=1 – 2012-02-13 10:18:43