Serving up data from Cloud Storage

Jenny Brown
3 min readJan 27, 2021

--

When it comes to the cloud, there’s more than one way to serve a file. In this post, we’ll walk you through all the ways to serve data from Cloud Storage — so you can determine the best fit for your needs!

You can use Cloud Storage to serve assets to your customers, and depending on your application, there are a variety of methods you might use to get that data out of a Cloud Storage bucket. In this post, we’ll cover four ways to serve those assets, but feel free to read more in the documentation, or for general, conceptual information on uploads and downloads, read this.

Here we go!

Client Libraries

First, we’ve got Client Libraries. If you need to download assets directly into your existing application, this is something for you to explore!

And trust me when I say…we speak your language. With code samples in C++, C#, Go, Java, Node.js, PHP, Python, and Ruby — we’ve got you covered. Check out the documentation for more.

Here’s an example of downloading from your Cloud Storage bucket using Python:

from google.cloud import storage

def download_blob(bucket_name, source_blob_name,

destination_file_name):

“””Downloads a blob from the bucket.”””

# bucket_name = “your-bucket-name”

# source_blob_name = “storage-object-name”

# destination_file_name = “local/path/to/file”

storage_client = storage.Client()

bucket = storage_client.bucket(bucket_name)

blob = bucket.blob(source_blob_name)

blob.download_to_filename(source_file_name)

gsutil

Up next, you have the gsutil application. gsutil is a Python application that lets you access Cloud Storage from the command line.

For our purposes, the `cp` command allows you to upload files from your local machine to google cloud.

For example, running *this* command will download from a Cloud Storage bucket to a local path on your device.

Additionally, gsutil comes with a plethora of options to configure to your specific use case; like the `-m` command, which allows for copy options to occur in parallel.

REST APIs

You can also directly go to the REST APIS, using other programs like cURL to fetch your files directly and allowing the user to log in with OAuth. More on that here.

curl -X GET \

-H “Authorization: Bearer OAUTH2_TOKEN” \

-o “SAVE_TO_LOCATION” \

“https://storage.googleapis.com/BUCKET_NAME/OBJECT_NAME"

Cloud Console

Finally, you can always access your objects right from the Cloud Console. Simply select the desired bucket, or object, and click “Download” in the handy “more actions” drop down menu.

This is a great way to grab individual files when you’re debugging or going through things manually.

Note: For some object types, selecting “Download” opens the object in the browser. To download these objects to your local computer, right-click on “Download” and select “Save Link As.”

What’s Next?

Retrieving files is really just the start, and you’ll want to make sure you’ve got a handle on controlling access — You know we’ve got another post for that, so stay tuned!

Learn more about your storage options in Cloud Storage Bytes, or check out the documentation for more information, including tutorials.

--

--

Jenny Brown
Jenny Brown

Written by Jenny Brown

Google Cloud Developer Advocate, Thinker, Feeler, Adventurer, Surfer, Burner. Opinions are my own, but I’m happy to share.

No responses yet