简体   繁体   English

我无法使用python脚本将文件上传到谷歌云?

[英]I can't upload a file to google cloud using python script?

I am trying the python script available on GCP reference to upload file to google cloud storage but every time I run it I get the error of file not found for the json key credential file even though it is in the same directory as the python script. 我正在尝试在GCP参考上提供的python脚本将文件上传到谷歌云存储,但每次运行它时都会收到json密钥凭证文件找不到的文件错误,即使它与python脚本位于同一目录中。

The error is : 错误是:

File "c:\\users\\kundan\\appdata\\local\\programs\\python\\python37- 文件“c:\\ users \\ kundan \\ appdata \\ local \\ programs \\ python \\ python37-
32\\lib\\site- packages\\google\\cloud\\client.py", line 75, in from_service_account_json with io.open(json_credentials_path, "r", encoding="utf-8") as json_fi: FileNotFoundError: [Errno 2] No such file or directory: 'key.json' 32 \\ lib \\ site- packages \\ google \\ cloud \\ client.py“,第75行,在from_service_account_json中,io.open(json_credentials_path,”r“,encoding =”utf-8“)为json_fi:FileNotFoundError:[Errno 2]没有这样的文件或目录:'key.json'

The code is as follows: 代码如下:

from google.cloud import storage

def upload_blob(bucket_name, source_file_name, destination_blob_name):

    storage_client = storage.Client.from_service_account_json(
    'key.json')
    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))

bucket='synersense_data'
source_file_name='gcp.txt'
destination_blob_name='prototype'

upload_blob(bucket,source_file_name,destination_blob_name)

This is the code I used to upload a abc.txt file to google storage. 这是我用来将abc.txt文件上传到谷歌存储的代码。

from google.cloud import storage

client = storage.Client.from_service_account_json('key.json')

def upload_blob(bucket_name, blob_name, filename):
    """ 
       Upload a blob to the bucket. 
       filename: source file name
       blob_name: destination blob name
    """
    bucket = client.get_bucket(bucket_name)
    blob = bucket.blob(blob_name)
    blob.upload_from_filename(filename)

print(upload_blob("XXXXXXXXXXXXXXX", "temp/abc.txt", "abc.txt"))

And, below is the output: 而且,以下是输出:

在此输入图像描述

Initially, I was also getting the same error as you were: 最初,我也遇到了和你一样的错误:

Traceback (most recent call last): File "gcp_upload.py", line 9, in client = storage.Client.from_service_account_json('key.json') File "E:\\Anaconda3\\envs\\py35\\lib\\site-packages\\google\\cloud\\client.py", line 75, in from_service_account_json with io.open(json_credentials_path, "r", encoding="utf-8") as json_fi: FileNotFoundError: [Errno 2] No such file or directory: 'key.json' 回溯(最近一次调用最后一次):文件“gcp_upload.py”,第9行,在client = storage.Client.from_service_account_json('key.json')文件“E:\\ Anaconda3 \\ envs \\ py35 \\ lib \\ site-packages \\ 'google \\ cloud \\ client.py“,第75行,在from_service_account_json中,io.open(json_credentials_path,”r“,encoding =”utf-8“)为json_fi:FileNotFoundError:[Errno 2]没有这样的文件或目录:'key以.json”

I found the mistake I was making initially. 我发现了我最初犯的错误。

在此输入图像描述

Check if the google storage credential json name is key.json or key.json.json . 检查google存储凭据json名称是否为key.jsonkey.json.json It might be possible that while renaming the original google storage credential json file, you named it key.json , but .json extension is automatically applied after renaming, so it would have been saved as key.json.json file, but you are passing key.json in storage.Client.from_service_account_json() (which actually needs to be key.json.json ). 这可能是可能的,而重命名原来的谷歌存储凭证JSON文件,将其命名为key.json ,但.json扩展名重命名后自动应用,所以它会被保存为key.json.json文件,但你是路过storage.Client.from_service_account_json() key.json (实际上需要是key.json.json )。 Try checking it using ls or dir command. 尝试使用lsdir命令检查它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 Google Cloud Console 上上传 python 脚本。 (错误响应 [13] && 错误响应 [9]) - Can't Upload python script on Google Cloud Console. (Error response [13] && Error response [9]) 每当在谷歌云存储上创建指定文件时,我想使用云 function 触发 python 脚本 - I want to trigger a python script using a cloud function whenever a specified file is created on the google cloud storage 如何将辅助python文件上传到Google云平台? - How do I upload a secondary python file to google cloud platform? 使用 Python 将 CSV 文件上传到 Google Cloud Storage - Upload CSV file to Google Cloud Storage using Python Python:使用 URL 将文件上传到 Google Cloud Storage - Python: Upload file to Google Cloud Storage using URL 使用 Python 将文件上传到 Google Cloud Storage Bucket 子目录 - Upload File to Google Cloud Storage Bucket Sub Directory using Python 如何使用google-cloud客户端将大于32MB的文件上传到GCS? - How can I upload a file larger than 32MB to GCS using the google-cloud client? Google Cloud App Engine Cron无法执行python脚本 - Google Cloud App Engine Cron can't execute python script 如何使用AJAX将图像上传到Google云存储? - How can I upload an image using AJAX to google cloud storage? 如何在 Google 云平台 gcp 上托管 python 脚本? - How can I host a python script on Google cloud platform gcp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM