Python – Read files from Google Cloud Storage
Today in this article we shall see how to use Python code to read the files. CSV or .Text files from Google Cloud Storage.
Today in this article, we will cover below aspects,
We shall be using the Python Google storage library to read files for this example.
Prerequisites
- Create an account in the google cloud project.
Getting Started
Create any Python application.
Add below Google Cloud storage Python packages to the application,
Using CLI
pip3 install google-cloud-storage
Additionally if needed,please perform below,
pip install --upgrade google-cloud-storage
Alternatively, one can use Requirements.txt for resolving the dependency
If using Requirements.txt, please add the required package as below,
google-cloud-storage == 1.28.1
Please use the required version as required. Else use the latest available version.
Or you can use setup.py file to register the dependencies as explained in the below article,
Please add the below namespace to your Python files,
from google.cloud import storage
Below is a sample example for reading a file from Google Bucket storage,
Read a file from Google Cloud Storage using Python
Below is a sample example of the file(pi.txt) which we shall read from Google Cloud Storage.
I shall be reading the above sample file for demonstration purposes.
We shall be uploading sample files from the local machine “pi.txt” to Google Cloud storage.
def read_file_blob(bucket_name, destination_blob_name):
"""Read a file from the bucket."""
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
# read as string
read_output = blob.download_as_string()
print(
"File {} read successfully from Bucket {}.".format(
destination_blob_name, bucket_name
)
)
Finally below, we can read the data successfully. (Below I have used Visual Studio IDE).
The above code will read the blob correctly with the name specified i.e “pi.txt” from the google cloud storage location “thecodebuzz“.
Below is how you call the method,
read_file_blob("thecodebuzz","pi.txt")
Once successfully read, data can be used for other required operations.
Do you have any comments or suggestions?
Please sound off your comments below.
References:
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.