使用Python操作Google Cloud Storage
我按照手册设置了Google Cloud Storage所需的环境。
我安装了“gsutil”,并设置了所有的路径。
我的gsutil运行得很好,但当我尝试运行下面的代码时,
#!/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>
因为我对这些还不太熟悉,所以有点难以理解为什么会这样。
有人能帮我吗?谢谢。
1 个回答
1
boto库应该会自动找到并使用你家目录下的.boto文件。你需要检查的一件事是:确保你正在使用的项目被设置为默认的旧版访问项目(在API控制台中,点击“存储访问”,确认上面写着“这是你默认的旧版访问项目”)。当我设置不正确时,如果我按照你提到的创建存储桶的例子去做,我也会遇到403错误。不过,这样的情况在你用gsutil时能成功,但直接用boto却不行,这就有点说不通了。
试着在创建storage_uri对象时加上“debug=2”,像这样:
uri = boto.storage_uri(name, GOOGLE_STORAGE, debug=2)
这样会在标准输出中生成一些额外的调试信息,你可以把这些信息和一个类似的、正常工作的gsutil例子的调试输出进行对比(通过gsutil -D mb命令)。