Google Cloud Storage Python
To interact with Google Cloud Storage using Python, you can use the official Google Cloud Client Library for Python. This library provides a convenient and straightforward way to work with Google Cloud Storage in your Python projects. Here’s a basic example of how you can use the library to perform common tasks with Google Cloud Storage:
Install the required library:
pip install google-cloud-storage
Import the necessary modules:
pythonfrom google.cloud import storage
Authenticate and create a client:
python# Create a client using default credentials
client = storage.Client()
Create a bucket:
pythonbucket_name = "your-bucket-name"
bucket = client.create_bucket(bucket_name)
print(f"Bucket {bucket.name} created.")
Upload a file to the bucket:
pythonfile_path = "path/to/local/file.txt"
destination_blob_name = "file.txt"
bucket = client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(file_path)
print(f"File {file_path} uploaded to {bucket.name}/{blob.name}.")
Download a file from the bucket:
pythonsource_blob_name = "file.txt"
file_path = "path/to/local/destination.txt"
blob = bucket.blob(source_blob_name)
blob.download_to_filename(file_path)
print(f"File {bucket.name}/{blob.name} downloaded to {file_path}.")
List objects in the bucket:
pythonblobs = bucket.list_blobs()
for blob in blobs:
print(f"- {blob.name}")
Delete a file from the bucket:
pythonblob_name = "file.txt"
blob = bucket.blob(blob_name)
blob.delete()
print(f"File {bucket.name}/{blob.name} deleted.")
These are just some basic operations you can perform using the Google Cloud Storage Python library. The library provides more advanced features, such as handling permissions, working with multiple buckets, and managing storage classes. You can refer to the official documentation for more information and additional examples: https://googleapis.dev/python/storage/latest/index.html
Google Cloud Training Demo Day 1 Video:
Conclusion:
Unogeeks is the No.1 IT Training Institute for Google Cloud Platform (GCP) Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Google Cloud Platform (GCP) here – Google Cloud Platform (GCP) Blogs
You can check out our Best In Class Google Cloud Platform (GCP) Training Details here – Google Cloud Platform (GCP) Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks