我正在跟随这篇教程,将一个文件上传到我手动创建的存储桶中:https://cloud.google.com/storage/docs/xml-api/gspythonlibrary
我似乎在设置服务帐户或用户帐户的凭据时遇到了问题。 我想在 Web 服务器上使用它,所以最好使用服务帐户进行设置。
我在 Console 中使用 API 管理器创建了一个 API 并下载了 JSON 文件。 同时,我的 gcloud auth 是使用我的 OAUTH 登录设置的。 我尝试过
我还尝试使用以下命令对服务账户进行身份验证:
我在 Console 中使用 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和Key从~/.config/gcloud/
复制到了~/.boto
,但这也没有奏效。我不确定该如何设置Python服务器以访问云存储进行身份验证。我没有使用App Engine,而是使用Cloud Compute来设置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>