简体   繁体   中英

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.

The error is :

File "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'

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.

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'

I found the mistake I was making initially.

在此输入图像描述

Check if the google storage credential json name is key.json or key.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 ). Try checking it using ls or dir command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM