python - 将文件夹上传到 Google Cloud - Python 2.7
问题描述
我正在尝试将本地计算机中的文件夹上传到谷歌云存储桶。我收到凭据错误。我应该在哪里提供凭据以及其中需要哪些所有信息。
from_dest = '/Users/xyzDocuments/tmp'
gsutil_link = 'gs://bucket-1991'
from google.cloud import storage
try:
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print('File {} uploaded to {}.'.format(source_file_name,destination_blob_name))
except Exception as e:
print e
错误是
could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://developers.google.com/accounts/do`cs/application-default-credentials.
解决方案
您需要为您的项目获取应用程序默认凭据并将它们设置为环境变量:
- 转到 GCP Console 中的创建服务帐号密钥页面。
- 从服务帐户下拉列表中,选择新建服务帐户。
- 在服务帐户名称字段中输入名称。
- 从角色下拉列表中,选择项目>所有者。
- 单击创建。一个 JSON 文件,其中包含您下载到计算机的密钥。
然后,设置一个环境变量,当它在本地运行时,它将向您的应用程序提供应用程序凭据:
$ export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/[FILE_NAME].json"
推荐阅读
- c++ - C ++多线程,在并行有多个线程时出现段错误
- charts - How to create a clustered stacked column chart in Google Sheets?
- r - 在 R 中使用 data.table 制作函数
- android - Clicking on individual view of recyclerview and changing the icon ,text color and making some part of layout visible and invisible
- vb.net - 将单个文本框中的数字相加并使用方法将值返回到列表框
- sed - sed and replacing + not replacing a second string
- r - R中直方图的x轴上有2个变量
- javascript - Regex get all content between third (:) and characters
- scala - 如何在键值对中包含所有小写键 - Scala
- scala - Scala Traits - 如何实现方法