云存储:如何为python boto库设置服务帐户凭据

云存储:如何为python boto库设置服务帐户凭据,python,google-cloud-storage,google-compute-engine,Python,Google Cloud Storage,Google Compute Engine,我将按照本教程将文件上载到我手动创建的bucket: 我似乎很难将凭据设置为服务帐户或用户帐户。我想在web服务器中使用它,因此理想情况下,它应该使用服务帐户进行设置 我在控制台中使用API管理器创建了一个API,并下载了JSON。同时,我的gcloudauth是用我的OAUTH登录名设置的。我确实尝试了gsutil config-e,但出现了错误: CommandException: OAuth2 is the preferred authentication mechanism with

我将按照本教程将文件上载到我手动创建的bucket:

我似乎很难将凭据设置为服务帐户或用户帐户。我想在web服务器中使用它,因此理想情况下,它应该使用服务帐户进行设置

我在控制台中使用API管理器创建了一个API,并下载了JSON。同时,我的gcloudauth是用我的OAUTH登录名设置的。我确实尝试了
gsutil config-e
,但出现了错误:

CommandException: OAuth2 is the preferred authentication mechanism with the Cloud SDK. Run "gcloud auth login" to configure authentication, unless you want to authenticate with an HMAC access key and secret, in which case run "gsutil config -a".
我还尝试使用以下方法验证服务帐户:
gcloud身份验证激活服务帐户--密钥文件

但是仍然无法使用python boto启用访问。我还将ID和Key从
~/.config/gcloud/
复制到
~/.boto
,但这也不起作用。我不确定如何设置python服务器访问云存储的身份验证。我没有使用AppEngine,而是使用云计算来设置Web服务器

以下是我的源代码:

import boto
import gcs_oauth2_boto_plugin
import os
import shutil
import StringIO
import tempfile
import time

CLIENT_ID = 'my client id from ~/.config/gcloud/credentials'
CLIENT_SECRET = 'my client secret from ~/.config/gcloud/credentials'
gcs_oauth2_boto_plugin.SetFallbackClientIdAndSecret(CLIENT_ID, CLIENT_SECRET)

uri = boto.storage_uri('', 'gs')
project_id = 'my-test-project-id'
header_values = {"x-test-project-id": project_id}
# If the default project is defined, call get_all_buckets() without arguments.
for bucket in uri.get_all_buckets(headers=header_values):
    print bucket.name
最近的错误:

Traceback (most recent call last):
  File "upload/uploader.py", line 14, in <module>
    for bucket in uri.get_all_buckets(headers=header_values):
  File "/Users/ankitjain/dev/metax/venv/lib/python2.7/site-packages/boto/storage_uri.py", line 574, in get_all_buckets
    return conn.get_all_buckets(headers)
  File "/Users/ankitjain/dev/metax/venv/lib/python2.7/site-packages/boto/s3/connection.py", line 444, in get_all_buckets
    response.status, response.reason, body)
boto.exception.GSResponseError: GSResponseError: 403 Forbidden
<?xml version='1.0' encoding='UTF-8'?><Error><Code>InvalidSecurity</Code><Message>The provided security credentials are not valid.</Message><Details>Incorrect Authorization header</Details></Error>

好的,在做了更多的实验之后,我放弃了使用GCE库将数据上传到云存储。事实上,我发现使用AWS boto上传到云存储的效果要好得多。我所要做的就是在s3库中指定Google的主机:

conn = boto.connect_s3(app.config['S3_KEY'], app.config['S3_SECRET'], "c.storage.googleapis.com")
bucket = conn.get_bucket(app.config['S3_BUCKET'], validate=False)
我使用了按照Google文档中描述的方式生成的HMAC凭据。参考:

您的代码在哪里运行?如果是在GCE或GAE上,建议使用Google应用程序默认凭据进行身份验证。我想在GCE上运行这段代码,但也要有一个本地开发环境。文档没有描述它是如何在本地环境下工作的。这是你一直在寻找的吗?等等,那么你是在使用Google密钥和密码吗?