search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Reading file from Google Cloud Storage in Google Colab

schedule Aug 10, 2023
Last updated
local_offer
Cloud Computing
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
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 auth
auth.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:

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!