2016-08-12 25 views
4

我下面这个教程将文件上载到我手动创建一个水桶: https://cloud.google.com/storage/docs/xml-api/gspythonlibrary云存储:如何设置服务帐户凭据为Python的Boto程式库

我似乎有设置的麻烦凭据都作为服务帐户或用户帐户。我想在Web服务器中使用它,所以理想情况下应该使用服务帐户进行设置。

我使用控制台中的API Manager创建了一个API并下载了JSON。同时我的gcloud认证是通过我的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 auth activate-service-account --key-file <json file>

但仍然能够与蟒蛇博托访问没有运气。我也将~/.config/gcloud/的ID和密钥复制到~/.boto,但那也不起作用。我不知道我应该如何设置python服务器的身份验证来访问云存储。我没有使用App Engine,而是使用云计算来设置网络服务器。

这里是我的源代码:

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> 
+0

代码在哪里运行?如果它位于GCE或GAE上,则 Google应用程序默认凭证是推荐的身份验证方法。 https://developers.google.com/identity/protocols/application-default-credentials – Dagang

+0

我想在GCE上运行此代码,但也有一个本地开发环境。该文档没有描述它是如何工作的地方环境 – Ankit

+0

这是你在找什么https://cloud.google.com/compute/docs/api/how-tos/authorization#gcloud_auth_login – Dagang

回答

0

好后一些更多实验我放弃了使用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证书。参考: https://cloud.google.com/storage/docs/migrating

+0

等等你是否使用Google密钥和秘密? – frmsaul

相关问题