Google cloud storage 使用Python 3时Google云授权不断失败-类型为None,应为(';authorized#u user';,';service#u account';)之一

Google cloud storage 使用Python 3时Google云授权不断失败-类型为None,应为(';authorized#u user';,';service#u account';)之一,google-cloud-storage,google-authentication,Google Cloud Storage,Google Authentication,我第一次尝试从谷歌云存储下载一个文件 我设置了从中下载的googstruct.json服务帐户密钥文件的路径 是否需要在代码之外设置谷歌云的授权?还是有比谷歌网站上的“如何使用谷歌云存储”更好的方法 似乎我将错误的类型传递给了存储\u client=storage.client() 下面是异常字符串 Exception has occurred: google.auth.exceptions.DefaultCredentialsError The file C:\Users\Cary\Docum

我第一次尝试从谷歌云存储下载一个文件

我设置了从中下载的googstruct.json服务帐户密钥文件的路径

是否需要在代码之外设置谷歌云的授权?还是有比谷歌网站上的“如何使用谷歌云存储”更好的方法 似乎我将错误的类型传递给了存储\u client=storage.client() 下面是异常字符串

Exception has occurred: google.auth.exceptions.DefaultCredentialsError
The file C:\Users\Cary\Documents\Programming\Python\QGIS\GoogleCloud\googstruct.json does not have a valid type. 
Type is None, expected one of ('authorized_user', 'service_account').
我的PYTHON 3.7代码

from google.cloud import storage
import os

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=
                                          "C:\\GoogleCloud\\googstruct.json"

# Instantiates a client
storage_client = storage.Client()
bucket_name = 'structure_ssi'
destination_file_name = "C:\\Users\\18809_PIPEM.shp"
source_blob_name = '18809_PIPEM.shp'
download_blob(bucket_name, source_blob_name, destination_file_name)

def download_blob(bucket_name, source_blob_name, destination_file_name):
    """Downloads a blob from the bucket."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(source_blob_name)

    blob.download_to_filename(destination_file_name)

    print('Blob {} downloaded to {}.'.format(
        source_blob_name,
        destination_file_name))
我确实看过这个,但我不知道这是否是我的问题。我两者都试过了


此错误表示您尝试使用的Json服务帐户凭据已损坏或类型错误

文件
googstruct.json
中的第一行(或第二行)应该是
“type”:“service\u account”

要改进您的代码,还有以下几项:

  • 您不需要使用
    \\
    ,只需使用
    /
    使代码更简单即可 而且阅读起来更干净
  • 直接加载凭据,不要修改环境 变量:
  • storage\u client=storage.client.from\u service\u account\u json('C:/GoogleCloud/googlestruct.json')

  • 在try/except中包装API调用。堆叠痕迹不会给客户留下深刻印象。最好有清晰、简单、易于阅读的错误消息
  • 是-没错。:)谢谢我删除了那个,创建了一个新的。