---
title: "How to Install & Use the Google Cloud Storage Python Client"
description: "Discover how to use the Google Cloud Storage Python client in your projects and make your DevOps pipelines work even better!"
canonical: "https://adamtheautomator.com/google-cloud-storage-python-client/"
---

# How to Install & Use the Google Cloud Storage Python Client

> Discover how to use the Google Cloud Storage Python client in your projects and make your DevOps pipelines work even better!

Source: https://adamtheautomator.com/google-cloud-storage-python-client/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Install & Use the Google Cloud Storage Python Client](https://adamtheautomator.com/wp-content/uploads/2023/01/google-cloud-storage-python.jpg)

# How to Install & Use the Google Cloud Storage Python Client

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)4 January 20237 min. read

Categories: [DevOps](/category/devops/)

Tags:[Google Cloud](/tag/google-cloud/)[Python](/tag/python/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Setting up a Local Development Environment](#setting-up-a-local-development-environment)
*   [Installing the Google Cloud Storage Python Client](#installing-the-google-cloud-storage-python-client)
*   [Authenticating Environment with Google Cloud](#authenticating-environment-with-google-cloud)
*   [Creating a New Cloud Storage Bucket](#creating-a-new-cloud-storage-bucket)
*   [Listing Available Buckets in the Google Cloud Storage](#listing-available-buckets-in-the-google-cloud-storage)
*   [Uploading Objects to a Bucket](#uploading-objects-to-a-bucket)
*   [Conclusion](#conclusion)

Interacting with your cloud storage buckets and objects can be a pain at some point, especially if you need the right tools. But why struggle when there is the Google Cloud Storage Python Client?

The Google Cloud Storage Python Client lets you securely store, organize and access data from anywhere in the world. And in this tutorial, you will learn to install the Google Cloud Storage Python Client and use it to interact with your Google Cloud Storage resources.

Ready? Stay tuned to improve your cloud storage management efficiently!

## Prerequisites

This tutorial will be a hands-on demonstration. To follow along, be sure you have the following:

*   A Linux machine – This tutorial uses Ubuntu 20.04, but any recent Linux distribution works.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   A Google Cloud Platform (GCP) account with active billing and admin privileges – A [free trial account](https://cloud.google.com/free/) will suffice.
*   Both [gcloud CLI](https://cloud.google.com/sdk/docs/install) and [Python](https://docs.python-guide.org/starting/install3/linux/) are installed on your Linux machine.

Related:[How Do You Install Python 3.6?](https://adamtheautomator.com/install-python-36/)

Related:[How to Perform a gcloud CLI Install and Manage Google Cloud](https://adamtheautomator.com/gcloud-cli-install/)

## Setting up a Local Development Environment

Before taking advantage of the Google Cloud Storage Python Client, you must set up a local development environment. A local development environment is where you can write and test your code without affecting your production environment. Testing out new things should not have to break anything.

You must first install the PIP package manager and activate a virtual environment to set up a local development environment. A virtual environment allows you to work on your project in an isolated environment.

1.  Open a terminal and run the below commands to perform the following:

*   Update your system package index
*   Install `wget` on your machine.

Related:[Install Python Wget and Automate your File Downloads](https://adamtheautomator.com/python-wget/)

*   Download, and install the PIP package manager.

```bash
sudo apt update -y && sudo apt install wget -y && wget https://bootstrap.pypa.io/get-pip.py && sudo python3 get-pip.py
```

![Installing wget and PIP](https://adamtheautomator.com/wp-content/uploads/2023/01/image-31.png)

Installing wget and PIP

2\. Once installed, run the following `pip3` command to check the installed PIP version (`-V`).

```bash
pip3 -V
```

![Checking the installed PIP version](https://adamtheautomator.com/wp-content/uploads/2023/01/image-32.png)

Checking the installed PIP version

3\. Now, run the following `pip` command to `install` `virtualenv`, a tool for creating isolated Python environments.

```bash
pip install virtualenv
```

![Installing virtualenv](https://adamtheautomator.com/wp-content/uploads/2023/01/image-33.png)

Installing virtualenv

4\. After installing `virtualenv`, run the below commands, which do not provide output but create your project directory and move into it.

This directory is where you will write and test your code.

```bash
mkdir my-project && cd my-project
```

5\. Next, run the following `virtualenv` command to start a new virtual environment. You can name your virtual environment differently, but this tutorial’s choice is `my-project.`

```bash
virtualenv my-project
```

![Starting a new virtual environment](https://adamtheautomator.com/wp-content/uploads/2023/01/image-34.png)

Starting a new virtual environment

6\. Lastly, run the below `source` command to activate your [virtual environment](https://www.geeksforgeeks.org/python-virtual-environment/), so you can start working on it.

With your virtual environment activated, any changes you make to your project’s dependencies (e.g., installing new libraries) only affect the virtual environment. Moreover, you can have different versions of the same library or dependency installed on your machine. You never have to worry about conflicts or compatibility issues.

```bash
source my-project/bin/activate
```

You will see your prompt change **(my-project)**, as shown below, to indicate you are working in the virtual environment.

Now sharing your project with others becomes a breeze since you can include the virtual environment. Anyone who clones your project gets the same dependencies and libraries installed. This feature helps ensure your project works the same way for everyone who uses it.

![Activating the virtual environment](https://adamtheautomator.com/wp-content/uploads/2023/01/image-35.png)

Activating the virtual environment

## Installing the Google Cloud Storage Python Client

Now you have a local development environment and can start installing the Google Cloud Storage Python Client. This client allows you to access and manage Google Cloud Storage from within your Python code.

Run the below `pip` command to download and `install` the latest version of the `google-cloud-storage` Python Client library from PyPI to your system.

The `google-cloud-storage` package is a [Python library](https://adamtheautomator.com/ansible-pip/) that provides a user-friendly interface for interacting with the Google Cloud Storage API.

Related:[How to Manage Python Libraries with Ansible Pip](https://adamtheautomator.com/ansible-pip/)

```python
pip install google-cloud-storage
```

Once the library is installed, you can perform various tasks with your Google Cloud Storage data. For example, you can create new buckets, list buckets, upload objects, and more.

![Installing the Google Cloud Storage Python Client](https://adamtheautomator.com/wp-content/uploads/2023/01/image-36.png)

Installing the Google Cloud Storage Python Client

## Authenticating Environment with Google Cloud

You have successfully installed the Google Cloud Storage Python Client library, but the Google Cloud Storage API still requires authentication. Why? Doing so lets the Google Cloud Storage API access and manage your cloud storage data.

There are several ways to authenticate with Google Cloud. But in this tutorial, you use the default application method provided by Google.

1\. Run the below [`gcloud auth`](https://cloud.google.com/sdk/gcloud/reference/auth) command to authenticate your environment with Google Cloud.

```bash
gcloud auth application-default login
```

Note down the URL in the output, as shown below. You will need it to access the Google Cloud Platform authentication page.

![Authenticating environment with Google Cloud](https://adamtheautomator.com/wp-content/uploads/2023/01/image-37.png)

Authenticating environment with Google Cloud

2\. Next, navigate to the URL you noted in step one to a new browser tab, and click **Allow**, as shown below, to confirm the authentication.

![Confirming the authentication](https://adamtheautomator.com/wp-content/uploads/2023/01/image-38.png)

Confirming the authentication

3\. Copy the generated code when authentication is successful.

![Copying the generated code](https://adamtheautomator.com/wp-content/uploads/2023/01/image-39.png)

Copying the generated code

4\. Now, switch back to your terminal, paste the code into your terminal, and press Enter to complete the authentication process.

![Finalizing the authentication](https://adamtheautomator.com/wp-content/uploads/2023/01/image-40.png)

Finalizing the authentication

## Creating a New Cloud Storage Bucket

You are now ready to use the Google Cloud Storage Python Client library with your environment authenticated. But first, you need a place to organize and store data — a bucket. A bucket (like a folder) is a logical container for storing objects (like files), and each object’s name is unique.

Buckets allow you to group objects and control access to them. For example, you can create a bucket for a specific project and grant access to only the team members working on that project. This feature helps prevent unauthorized access to your data.

To create a new bucket, follow the steps below:

1\. Create a Python (`create_bucket.py`), which you can name as you like, with your preferred editor. This tutorial uses the `nano` text editor.

```bash
nano create_bucket.py
```

2\. Next, populate the code below into the _create\_bucket_._py_ file. The code below uses `google.cloud.storage` to create a new bucket in your Google Cloud Storage.

<table><tbody><tr><td>Notes to keep in mind when using this code</td></tr><tr><td>The bucket’s name must be unique within your Google Cloud Storage project and comply with buckets’ naming conventions. The name must be between three and 63 characters long and can only contain lowercase letters, numbers, and hyphens.</td></tr><tr><td>This code does not verify a specified bucket, which results in an error if the bucket you are trying to create already exists. To avoid getting an error, check all existing buckets before calling the create_bucket() method in your script.</td></tr></tbody></table>

```python
# Imports the 'storage' module from the google.cloud package
# to allow interactions with the Google Cloud Storage.
from google.cloud import storage

# Creates a Client object that allows the script to communicate
# with Google Cloud Storage and perform operations on it (like creating a bucket).
client = storage.Client()

# Creates a new bucket with a specified name
bucket = client.create_bucket("my-first-bucket14755286")

# Prints a message indicating the bucket was successfully created.
print("Bucket {} created.".format(bucket.name))
```

3\. Save and close the _create\_buckets_._py_ file, and execute the below command to run the script.

```bash
python create_bucket.py
```

If all goes well, you will see a message that says Bucket <bucket-name> created., as shown below.

![Creating a new bucket](https://adamtheautomator.com/wp-content/uploads/2023/01/image-41.png)

Creating a new bucket

## Listing Available Buckets in the Google Cloud Storage

Now you have created a new bucket, and you can use the Google Cloud Storage Client library to list all existing buckets in your project. Why? Listing all available buckets in your project is good practice before performing operations like uploading objects.

To list all the buckets in your project, follow these steps:

1\. Create a Python file (named as you desire) with your editor.

```bash
nano list_buckets.py
```

2\. Next, add the code below to the _list\_buckets.py_ file.

This code lists all the buckets in your specified project and checks if the created bucket is in the list. If found, a message prints saying `Bucket found!` along with information about the bucket.

```python
from google.cloud import storage

# Create a client object

client = storage.Client()

# List all the buckets in your project

buckets = list(client.list_buckets())

# Check if the bucket you created is in the list of buckets

for bucket in buckets:

 if bucket.name == "my-first-bucket14755286":

  print("Bucket found!")

  print(buckets)
```

3\. Save and close the _list\_buckets_.py file and run it.

```python
python list_buckets.py
```

If the bucket you created is found, you will see a similar output to the one below.

![Listing all buckets](https://adamtheautomator.com/wp-content/uploads/2023/01/image-42.png)

Listing all buckets

## Uploading Objects to a Bucket

Now that you have created an empty bucket, the first step in making your bucket useful is uploading objects to the bucket.

Which type of objects can you upload? Any file type will do, such as an image, video, text file, etc. But before uploading, you need an object first.

1\. Create a new text file named _my-file.txt_, a test file you will upload to your bucket.

2\. Next, create a Python file called _upload\_object_.py (or any other name), and populate the code below into the file.

The code below uses the `upload_from_filename` method to upload your text file to your bucket.

```python
from google.cloud import storage

# Create a client object

client = storage.Client()

# Get a reference to the bucket you want to upload to

bucket = client.bucket("my-first-bucket14755286")

# Create a new blob object

blob = bucket.blob("my-file.txt")

# Upload the file to the bucket

if blob.upload_from_filename("./my-file.txt"):

 print("File successfully uploaded to bucket.")

else:

 print("Error uploading file to bucket.")
```

3\. Save the file, and execute the below command to run the script (`upload_object.py`).

```bash
python upload_object.py
```

If all goes well, you will see a message that says **File successfully uploaded to bucket**, as shown below. At this point, the file has been uploaded to your bucket.

![Uploading an object to the bucket](https://adamtheautomator.com/wp-content/uploads/2023/01/image-43.png)

Uploading an object to the bucket

4\. Now, navigate the [Storage page](https://console.cloud.google.com/storage/) in the Google Cloud Console to verify the uploaded object exists.

You will see your bucket (**my-first-bucket14755286**) with the uploaded object (**my-file.txt**) listed, as shown below.

![Verifying the uploaded object exists in the bucket](https://adamtheautomator.com/wp-content/uploads/2023/01/image-44.png)

Verifying the uploaded object exists in the bucket

5\. Finally, run the following `deactivate` command to leave or exit your virtual environment.

```python
deactivate
```

Successfully deactivating your virtual environment switches you back to your default prompt, as in the output below.

![Deactivating the virtual environment](https://adamtheautomator.com/wp-content/uploads/2023/01/image-45.png)

Deactivating the virtual environment

## Conclusion

This tutorial taught you how to create buckets and manage objects with the Google Cloud Storage Python Client. Whether you are a beginner or an experienced developer, the Google Cloud Storage Python Client is a valuable addition to your toolkit for working with data in the cloud.

Now, why not explore the advanced features? Indulge in [object versioning](https://cloud.google.com/storage/docs/using-object-versioning), [lifecycle management](https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.LifecycleRuleConditions), and [permissions](https://cloud.google.com/python/docs/reference/cloudasset/3.8.1/google.cloud.asset_v1p1beta1.types.Permissions) for deeper control over your data!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fgoogle-cloud-storage-python-client%2F&text=How%20to%20Install%20%26%20Use%20the%20Google%20Cloud%20Storage%20Python%20Client)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fgoogle-cloud-storage-python-client%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fgoogle-cloud-storage-python-client%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2025/11/55418e927e511ae263219c072e27d637c2a967a5036de7020b329582db775c26.png)

### [Automating Docker Container Health Checks with Python and Local Notifications](/docker-health-checks-python/)

Docker's built-in health checks are passive—they tell Docker when a container fails, but do they tell you? In this tutorial, we'll build a lightweight Python monitoring system that runs entirely on your infrastructure with zero external dependencies. You'll learn to detect container failures in real-time, send instant alerts, and maintain a complete audit log of every state change.

![](https://adamtheautomator.com/wp-content/uploads/2022/12/gcp-cloud-functions.jpg)

### [GCP Cloud Functions Customize Your Serverless Workflow](/gcp-cloud-functions/)

Learn how to build and deploy scalable, serverless applications using GCP Cloud Functions. This tutorial covers essential concepts and best practices for streamlined workflows.

![](https://adamtheautomator.com/wp-content/uploads/2022/12/gsutil.jpg)

### [Learning the gsutil Command Through Examples](/gsutil/)

Discover the many uses of the gsutil command and learn through examples how to utilize this command-line tool to make your management job easier!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
