Google Cloud: Python Integration — Working with Storage Bucket and Jupyter Notebook

Share At:

Introduction

In this article, we will be learning how to use the Python Library for Google Cloud. We will be using Python Library for Google Cloud Storage and few lines of python code to upload a sample image file in our storage bucket.

Feel free to use any type of file for the upload.

We will be using Jupyter Notebooks for our Python Environment.

Download the Service account Key

  1. Click on the Hamburger Icon on the Top Left Corner.
  2. Now click on IAM & Admin > Service-Accounts. You will see our default service account.

3. Now Click on 3 Dots at the right under Actions and click Manage keys.

4. Now click on Add Key and Create New key.

5. Select JSON and Click Create.

6. The Key will be downloaded to your Computer. Click Close to close the window.

7. Now on your computer, go to Downloads. You will see a JSON key there. Rename it to credentials21.json

Creating A Storage Bucket

  1. Read my previous article Google Cloud: Cloud Storage Bucket — Giving Roles and Permissions to an object” on how to create a storage bucket.
  2. Let’s keep the name as “py-python21”

Setting Up Jupyter Environment and Installing Google Cloud Python library

The Jupyter notebook extends the console-based approach to interactive computing in a qualitatively new direction, providing a web-based application suitable for capturing the whole computation process: developing, documenting, and executing code, as well as communicating the results.

The Jupyter notebook combines two components:

  • A web application: a browser-based tool for interactive authoring of documents that combine explanatory text, mathematics, computations, and their rich media output.
  • Notebook documents: a representation of all content visible in the web application, including inputs and outputs of the computations, explanatory text, mathematics, images, and rich media representations of objects.
  1. Open a Jupyter Notebook and name it as “Python-GCP-Integration”.

2. Now put the below command in a cell and run the code. We will be using the pip python installer to install the library.

!pip install google-cloud-storage

3. Now upload both the files —in the same directory where our jupyter notebook exists.

  • The file which we want to move to Google cloud storage bucket. In our case, it is named as “my_Image_file.png”.
  • Service account key (credentials.json)file.

So far so good. Now we are ready to code.

Running Python code to upload file in GCP Storage Bucket

Go Back to our Jupyter notebook again.

  1. In a new cell, type the below commands:

from google.cloud import storage
# Setting credentials using the downloaded JSON file
client = storage.Client.from_service_account_json(json_credentials_path=’credentials21.json’)
# Creating bucket object
bucket = client.get_bucket(‘py-python21’)
# Name of the object to be stored in the bucket
object_name_in_gcs_bucket = bucket.blob(‘my_Image.png’)
# Name of the object in local file system
object_name_in_gcs_bucket.upload_from_filename(‘my_Image_file.png’)

2. Now run the cell by selecting the cell and by pressing CTRL+ENTER.

That’s It. The file will be uploaded to the storage bucket.

3. Now go to Cloud Storage > Browser > Click on py-python21 bucket. You will see our uploaded file.

That’s great. We have learnt some serious stuff!

Download Link of IPYNB notebook.

Happy learning!


Share At:
0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
melhores sites para minerar bitcoin gratis

Reading your article helped me a lot and I agree with you. But I still have some doubts, can you clarify for me? I’ll keep an eye out for your answers.

Back To Top

Contact Us