How to Upload files to GCS bucket Python 3
Before uploading files to Google Cloud Storage Bucket/GCS you should first create Bucket on Google Cloud platform. Follow the mentioned link for how to Create a Google Cloud Storage bucket Using Python language. Suppose you want to upload a text file i.e. ‘test12.txt’ which is located on your computer/desktop at path ‘C:\\Users\\hp\\Desktop\\gcloud_files\\’ and you want to upload this file on GCS/Google Cloud Storage Bucket ‘Test889820787’. Follow the below set of Python code to upload a file from your local computer to Google Cloud Storage Bucket/GCS.
from google.cloud import storage
path_to_private_key = 'C:\\Users\\hp\\Downloads\\avid-influence-XXXXXX-XXXXXXXXXXXX.json'
client = storage.Client.from_service_account_json(json_credentials_path=path_to_private_key)
bucket = storage.Bucket(client, 'Test889820787')
list_files_to_upload = ['test12.txt']
#list_files_to_upload = ['avid-influence-XXXXXX-XXXXXXXXXXXX.json']
for str_file_name in list_files_to_upload:
# File name on GCS i.e. Google Cloud Storage Bucket after uploaded
blob = bucket.blob(str_file_name)
# Path of files on your desktop/local computer
local_file_path = f'C:\\Users\\hp\\Desktop\\ai_dataset\\gcloud_files\\{str_file_name}'
blob.upload_from_filename(local_file_path)
Suppose you want to upload multiple files on Google Cloud Storage Bucket/GCS, then add files names in the below line of code
list_files_to_upload = ['demo.py', ‘test12.txt’]
The complete code is given below
from google.cloud import storage
path_to_private_key = 'C:\\Users\\hp\\Downloads\\avid-influence-XXXXXX-XXXXXXXXXXXX.json'
client = storage.Client.from_service_account_json(json_credentials_path=path_to_private_key)
bucket = storage.Bucket(client, 'Test889820787')
list_files_to_upload = ['demo.py', ‘test12.txt’]
#list_files_to_upload = ['avid-influence-XXXXXX-XXXXXXXXXXXX.json']
for str_file_name in list_files_to_upload:
# File name on GCS i.e. Google Cloud Storage Bucket after uploaded
blob = bucket.blob(str_file_name)
# Path of files on your desktop/local computer
local_file_path = f'C:\\Users\\hp\\Desktop\\ai_dataset\\gcloud_files\\{str_file_name}'
blob.upload_from_filename(local_file_path)
Send Query