2017-06-20 153 views
1

我有一个很艰难的时期让应用程序的默认Credientials /OAuth2.0的事情在我的计划工作...谷歌云存储访问客户端API

背景:

我有一个传感器装置收集图像,我想将这些图像存储在谷歌云存储中。理想情况下,代码只需按下一个按钮即可运行,因此在程序运行时需要执行授权。

从阅读和重新阅读的文档,这是我迄今打算:

from oauth2client.client import GoogleCredentials 
from google.cloud import storage 
import google.auth 
import os 

# Establish cloud credientials with a locally 
# stored service account key 
home_path = os.path.expanduser('~') + "/Project/" 
auth_file = "<service account access key file>.json" 
auth_file_path = home_path + auth_file 

# I'm trying two different ways to verify my credentials.. 
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = auth_file_path 
credentials, project_id = google.auth.default() 

# Get storage bucket for image files 
storage = storage.Client(project_id) 
bucket = storage.lookup_bucket('bucket0000') 

其结果是,我得到一个回溯错误:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<string>", line 110, in <module> 
    File "/Users/joshua/anaconda/lib/python3.5/site-packages/google/cloud/storage/client.py", line 193, in lookup_bucket 
    return self.get_bucket(bucket_name) 
    File "/Users/joshua/anaconda/lib/python3.5/site-packages/google/cloud/storage/client.py", line 173, in get_bucket 
    bucket.reload(client=self) 
    File "/Users/joshua/anaconda/lib/python3.5/site-packages/google/cloud/storage/_helpers.py", line 99, in reload 
    _target_object=self) 
    File "/Users/joshua/anaconda/lib/python3.5/site-packages/google/cloud/_http.py", line 303, in api_request 
    error_info=method + ' ' + url) 
google.cloud.exceptions.Forbidden: 403 Caller does not have storage.buckets.get access to bucket bucket0000. (GET https://www.googleapis.com/storage/v1/b/bucket0000?projection=noAcl) 

地方似乎我仍被视为匿名用户(根据追踪中的googleapis.com链接)

{ 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "required", 
    "message": "Anonymous users does not have storage.buckets.get access to bucket bucket0000.", 
    "locationType": "header", 
    "location": "Authorization" 
    } 
    ], 
    "code": 401, 
    "message": "Anonymous users does not have storage.buckets.get access to bucket bucket0000." 
} 
} 

再次尝试,这次google.auth,我创建了一个证书变量,并观察什么打印似乎有效:<oauth2client.service_account._JWTAccessCredentials object at 0x107df0358>

我试图把这个凭证变量为storage.Client(),但似乎并不上班。

# Get storage bucket for image files 
storage = storage.Client(project_id, credentials=credentials) 
bucket = storage.lookup_bucket('bucket0000') 

>>> Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<string>", line 6, in <module> 
    File "/Users/joshua/anaconda/lib/python3.5/site-packages/google/cloud/storage/client.py", line 59, in __init__ 
    _http=_http) 
    File "/Users/joshua/anaconda/lib/python3.5/site-packages/google/cloud/client.py", line 212, in __init__ 
    Client.__init__(self, credentials=credentials, _http=_http) 
    File "/Users/joshua/anaconda/lib/python3.5/site-packages/google/cloud/client.py", line 126, in __init__ 
    raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP) 
ValueError: This library only supports credentials from google-auth-library-python. See https://google-cloud-python.readthedocs.io/en/latest/google-cloud-auth.html for help on authentication with this library 

当然,这回溯指导我的页面google-cloud-python.readthedocs.io ......不存在......

任何方式来获得API调用到云存储在没有使用控制台/网络登录的情况下工作?

非常感谢!

+0

您真的在使用Google App Engine吗?从代码中不清楚。针对应用引擎和其他应用的云端存储身份验证有所不同,因此务必要清楚这一点。 – snakecharmerb

回答

0

看来你没有给你的凭证管理存储的权限。您可以在IAM或每个桶下轻松应用它。

相关问题