Reading file from Google Cloud Storage in Google Colab
Start your free 7-days trial now!
In this short guide, we will go through the steps to import a simple text file from Google Cloud Storage (GCS) to Google Colab.
1. Authentication
We must first authenticate ourselves to fetch resources from GCS:
from google.colab import authauth.authenticate_user()
This will open up a popup - log in with the same account that hosts the file on Google Cloud Platform (GCP).
2. Saving file from Google Cloud Storage to local file system on Google Colab
Set the current GCP project to the one where your file resides on GCS:
project_id = 'gcs-project-354207'!gcloud config set project {project_id}
Updated property [core/project].
Getting the project ID
To get the GCP project ID required in the above step, log into GCP console and click on the dropdown menu in the header:
This will show a list of all your projects on GCP as well as the corresponding IDs:
Now, suppose we have a file called sample.txt
under the bucket example-bucket-skytowner
on GCS. To download this file into the root directory of our Colab machine, use the pre-installed gsutil cp
command:
bucket_name = 'example-bucket-skytowner'# Download the file from the given Google Cloud Storage bucket.!gsutil cp gs://{bucket_name}/sample.txt sample.txt
Here, cp
stands for copy.
If the downloaded file does not appear on the left side panel, manually hit the refresh button below:
We should see our sample.txt
file in the root directory of our Colab machine: