Chapter 78 – Fetching Media Files Using Google Cloud Storage and Python

Automatic media file uploading and fetching is one of the critical parts in RPA. Being said, not all of the platforms accept the media URL as an endpoint to retrieve media materials. Thus in this piece, I would walk you through the flow and basic Python script to make this run using Google Cloud Storage.

Automatic media file uploading and fetching is one of the critical parts in RPA. Being said, not all of the platforms accept the media URL as an endpoint  to retrieve media materials. Thus in this piece, I would walk you through the flow and basic Python script to make this run using Google Cloud Storage.

Tables of Content: Fetching Media Files Using Google Cloud Storage and Python 

Google Cloud Library and Credential Authentication

First thing first, google cloud storage is the official Python SDK package. We need to install and import it and blob in the Python script.

from google.cloud import storage

from google.cloud.storage import blob

Then, I assume you have created a project and credential account on GC platform, which means you have the credential JSON file already. Here is the way to authenticate your identity and access to the account in the script.

# storage_client = storage.Client.from_service_account_json('your credential json file path')

Or

storage_client = storage.Client.from_service_account_info(‘your credential dict type’ )

Create a Bucket on Google Cloud Storage

All files are stored by bucket. First thing first, we need to create a bucket under your account on GCS.

bucket_name = abc"

bucket = storage_client.get_bucket(bucket_name)

Then, we need to name the file we like to upload data to from our local or other machines. From here, I would suggest to create a folder because your project might have different types of materials and it’s easier to manage later on.

object_name_in_gcs_bucket = bucket.blob('abc/test123.png')

Upload Files to specific folders

Last but not least, here is a sample of uploading a single photo. Being said that, you can create a loop to upload in bulk

object_name_in_gcs_bucket.upload_from_filename('4.png')

Fetch the Files in Bytes Format

Now it’s ready for you to deploy automation and fetch any files you want. Generally, if it’s an online to online endpoint interaction and communication, we would use download_as_bytes(). Being said, it really depends on your actual needs. This method can allow to fetch video, gif, images, pdf, etc.

Full Python Script of Fetching Media Files Using Google Cloud Storage and Python 

If you are interested in Chapter 78 – Fetching Media Files Using Google Cloud Storage and Python, please subscribe to our newsletter by adding the message ‘Chapter 77 + Linkedin api’. We would send you the script immediately to your mailbox.

I hope you enjoy reading Chapter 78 – Fetching Media Files Using Google Cloud Storage and Python. If you did, please support us by doing one of the things listed below, because it always helps out our channel.