繁体   English   中英

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

[英]Cloud Storage: how to setup service account credentials for python boto library

我正在按照本教程将文件上传到我手动创建的存储桶: https//cloud.google.com/storage/docs/xml-api/gspythonlibrary

我似乎无法将凭据设置为服务帐户或用户帐户。 我想在Web服务器中使用它,所以理想情况下应该使用服务帐户进行设置。

我在Console中使用API​​ Manager创建了一个API并下载了JSON。 同时我的gcloud auth是通过我的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>服务帐户: gcloud auth activate-service-account --key-file <json file>

但是使用python boto启用访问仍然没有运气。 我还将ID和密钥从~/.config/gcloud/复制到了~/.boto但是也没有用。 我不确定我应该如何为python服务器设置身份验证以访问云存储。 我没有使用App Engine而是使用Cloud Compute来设置网络服务器。

这是我的源代码:

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凭据。 参考: https//cloud.google.com/storage/docs/migrating

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM