Python – Create Google Cloud Storage Bucket

Create Google Cloud Storage Bucket using Python

Today in this article, we shall learn how to use Python – Create Google Cloud Storage Bucket.

We will create Cloud Storage buckets in the GCP environment programmatically.

We shall be using the Python Google storage library to perform the same.

Today in this article, we will cover below aspects,

It’s pretty simple to use Google storage libraries and perform numerous operations on the cloud storage like read, create or delete the storage.

Getting Started

Create any Python application.

Add below Python packages to the application

Using CLI

pip3 install google-cloud-storage

Additionally if needed,

pip install --upgrade google-cloud-storage

Alternatively,

Using Requirements.txt

google-cloud-storage == 1.28.1

Or

you can use setup.py file to register the dependencies as explained in the below article,

Please add below namespace to your python files,

from google.cloud import storage

Below is sample example for creating New Bucket storage,

  def create_bucket(bucket_name):
    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    bucket.storage_class = "Standard"
    new_bucket = storage_client.create_bucket(bucket, location="us")

    print(
        "Created bucket successfully {} in location {}".format(
            new_bucket.name, new_bucket.location
        )
    )
    return new_bucket

Let’s use the bucket name as “thecodebuzz”

GCP Storage classes

In the above example, the user can define a storage location and use the storage class as required.

If the storage class value is not specified when the bucket is created, it will default to the STANDARD storage class.

The storage class type defines how the objects in the bucket are stored.

Storage class determines the SLA and the cost of storage.

User can use any of the below supported Storage class available.

Storage class Tyoes

Users can use any of the below types of storage class based on their SLA and Cost requirements of storing the data.

  • NEARLINE – Nearline Storage is a low-cost, highly durable suitable for lower availability storage requirements.

  • COLDLINE – Coldline Storage is a very-low-cost, highly durable storage service with slightly lower availability.

  • ARCHIVE – Archive Storage is the lowest-cost, highly durable storage service mainly used for use cases like data archiving, online backup, or disaster recovery

  • STANDARD – Standard Storage is best for data that is frequently accessed for brief periods of time. It is the default storage class when the storage class value is not specified when the bucket is created,

Other storage classes are as below,

  • MULTI_REGIONAL_LEGACY
  • REGIONAL_LEGACY

That’s All once you run the above logic, you shall see your storage bucket is ready to be used.

Python Create Google Cloud Storage Bucket

Please make sure to set the Environment variable GOOGLE_APPLICATION_CREDENTIALS using a secured service account file.

import os

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'secured.json'

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



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.



Leave a Reply

Your email address will not be published. Required fields are marked *