fqntom
fqntom

Reputation: 51

Is there a way to rename a file while uploading it to google drive using gdrive API?

I am automatically uploading a file on google drive (using google drive API).

I would like to rename the file while uploading it (as its label is originally the full path directory of the file, which is pretty long).

I am using this function to upload the file on gdrive:

def upload_file_gdrive(file_path, base_currency, gdrive_path_id):

    # Authentication (automated using the config files)
    # The config files should be dropped in the current working directory

    gauth = GoogleAuth()
    drive = GoogleDrive(gauth)

    
    # Upload file on gdrive

    print('Uploading the market data on google drive')

    gfile = drive.CreateFile({'parents': [{'id': gdrive_path_id}]})
    gfile.SetContentFile(file_path)
    gfile.Upload()  # Upload the file.

    print('Market data upload on google drive successful')

Do we have a way to rename the file while uploading it ?

Cheers

Upvotes: 2

Views: 1189

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116918

Creating a file is actually done in two parts sending the metadata which describes the file followed by sending the actual contents of the file.

You can just change the name by supplying in the metadata.

gfile = drive.CreateFile({'parents': [{'id': gdrive_path_id}]})
gfile['title'] = 'HelloWorld.txt' # Change title of the file.
gfile.SetContentFile(file_path)
gfile.Upload()  # Upload the file.

Update file metadata

Upvotes: 2

Related Questions