Cloud Storage:如何为 python boto 库设置服务帐户凭据
Cloud Storage: how to setup service account credentials for python boto library
我正在按照本教程将文件上传到我手动创建的存储桶:
https://cloud.google.com/storage/docs/xml-api/gspythonlibrary
我似乎无法将凭据设置为服务帐户或用户帐户。我想在 Web 服务器中使用它,因此理想情况下应该使用服务帐户进行设置。
我在控制台中使用 API 管理器创建了 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
我正在按照本教程将文件上传到我手动创建的存储桶: https://cloud.google.com/storage/docs/xml-api/gspythonlibrary
我似乎无法将凭据设置为服务帐户或用户帐户。我想在 Web 服务器中使用它,因此理想情况下应该使用服务帐户进行设置。
我在控制台中使用 API 管理器创建了 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