我使用GCS做了第一步
首先,我在项目中创建了服务帐户,并在json中获得了密钥文件
接下来,我尝试编写像sample
这样的脚本但是...
from gcloud import storage
client = storage.Client.from_service_account_json('/path/to/keyfile.json')
bucket = client.get_bucket('enggeo')
# Then do other things...
blob = bucket.get_blob('/ETicket.pdf')
print blob.download_as_string()
blob2 = bucket.blob('/wtt.txt')
blob2.upload_from_filename(filename='/home/test2/.www/test')
收到错误
File "tt.py", line 3, in <module>
client = storage.Client.from_service_account_json('/path/to/keyfile.json') # TODO: rel paths
File "/home/test2/lib/python2.7/site-packages/gcloud/client.py", line 64, in from_service_account_json
return cls(*args, **kwargs)
File "/home/test2/lib/python2.7/site-packages/gcloud/storage/client.py", line 53, in __init__
http=http)
File "/home/test2/lib/python2.7/site-packages/gcloud/client.py", line 181, in __init__
_ClientProjectMixin.__init__(self, project=project)
File "/home/test2/lib/python2.7/site-packages/gcloud/client.py", line 146, in __init__
raise ValueError('Project was not passed and could not be '
项目未通过,无法从环境中确定。
有什么问题?
答案 0 :(得分:6)
client = storage.Client.from_service_account_json(&#39; /path/to/keyfile.json' ;,&#39; project&#39;)
需要为JSONClient指定args
https://googlecloudplatform.github.io/gcloud-python/stable/gcloud-api.html#gcloud.client.JSONClient