我正在尝试将本地计算机中的文件夹上传到google cloud bucket。我的凭证出现错误。我应该在哪里提供凭据以及其中需要什么信息。
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.
答案 0 :(得分:1)
您需要获取项目的应用程序默认凭据,并将其设置为环境变量:
然后,设置一个环境变量,该变量将在本地运行时为您的应用程序提供应用程序凭据:
$ export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/[FILE_NAME].json"
答案 1 :(得分:0)
通常由于多种原因(例如文件丢失,凭据路径无效,环境变量分配不正确)而未正确地对应用程序进行认证时,会引发此错误消息。请记住,在会话中设置环境变量值时,每次删除会话时都会重置该值。
基于此,我建议您验证是否正确分配了凭证文件和文件路径,并遵循Obtaining and providing service account credentials manually指南,以便在代码中直接直接指定服务帐户文件;这样,您将能够对其进行永久设置,并验证您是否正确传递了服务凭据。
在代码示例中将路径传递到服务帐户密钥:
def explicit():
from google.cloud import storage
# Explicitly use service account credentials by specifying the private key
# file.
storage_client = storage.Client.from_service_account_json('service_account.json')
# Make an authenticated API request
buckets = list(storage_client.list_buckets())
print(buckets)