微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在按照本教程将文件上传到我手动创建的存储桶:
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>

但是使用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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐