Introduction: Local Object Storage Development

Object storage is a fundamental component of modern cloud applications, providing a scalable way to store and retrieve files, images, documents, and other unstructured data. Unlike traditional file systems, object storage organizes data as objects within containers called buckets, making it ideal for web applications, mobile apps, and distributed systems.

When developing applications that use Google Cloud Storage, you typically need to set up authentication, configure billing, and manage cloud resources. However, this creates friction during development and testing. This is where emulators become invaluable — they provide a local environment that mimics the behavior of cloud services without requiring any cloud setup or incurring costs.

In this course, we'll use fake-gcs-server, a lightweight emulator that provides Google Cloud Storage functionality. This emulator allows you to develop and test your storage operations locally with complete control over your environment. You can create buckets, upload files, download content, and perform all the standard storage operations without touching the actual cloud.

By the end of this lesson, you'll have a complete working setup that demonstrates the core workflow: starting the emulator, connecting with Python, creating a bucket, and performing basic upload and download operations. This foundation will prepare you for more advanced storage workflows in the upcoming lessons.

Starting the Cloud Storage Emulator

fake-gcs-server is a popular open-source emulator that provides a local implementation of Google Cloud Storage APIs. In the CodeSignal IDE, all necessary tools are already installed and configured for you, so no additional downloads are required. To start the emulator, open a terminal and execute the following command:

This command starts the emulator using HTTP protocol on port 4443. The emulator will be accessible at http://localhost:4443.

The emulator starts immediately and begins listening for storage requests. This local endpoint becomes your storage target, replacing the actual Google Cloud Storage URLs that your Python client would normally connect to.

To redirect your Python storage client to use this local emulator instead of the real Google Cloud Storage, you need to set the STORAGE_EMULATOR_HOST environment variable. This tells the Google Cloud Storage client library to send all requests to your local emulator rather than the cloud service.

Note: If you're working outside of the CodeSignal IDE, you can download fake-gcs-server from its GitHub releases page at https://github.com/fsouza/fake-gcs-server/releases

Connecting with the Python Storage Client

With the emulator running, you can now configure your Python application to connect to it. The Google Cloud Storage client library automatically detects when you're using an emulator through the STORAGE_EMULATOR_HOST environment variable.

The setdefault method sets the environment variable only if it hasn't been set already, which is useful for flexibility in different environments. When you print the emulator target, you should see http://localhost:4443, confirming that your client will connect to the local emulator.

Creating a storage client in emulator mode is straightforward. You instantiate the client with a project parameter, but unlike with real Google Cloud Storage, this project name doesn't need to correspond to an actual Google Cloud project. The emulator ignores authentication and project validation, making development much simpler.

The project name demo-local is arbitrary — you could use any string. The emulator treats all requests as valid regardless of the project specified, allowing you to focus on your application logic rather than cloud configuration details.

Creating Your First Bucket

Buckets are the top-level containers in object storage systems. Before you can store any objects, you need to create a bucket to hold them. With your client connected to the emulator, creating a bucket is a simple operation.

This code creates a bucket reference object, but it doesn't actually create the bucket in storage yet. The bucket reference is just a local object that represents the bucket you want to work with. To actually create the bucket in the emulator, you need to call the create_bucket method.

The create_bucket method attempts to create the bucket with the specified location. In a real Google Cloud environment, the location parameter determines which geographic region stores your data. In the emulator, this parameter is accepted but doesn't affect the actual storage location since everything runs locally.

The try-except block handles the Conflict exception, which occurs when you try to create a bucket that already exists. This is common during development when you run your code multiple times. By catching and ignoring this exception, your code can run repeatedly without failing on subsequent executions.

Complete Example: Ping Object Upload and Download

Now that you have a bucket, you can perform the core object storage operations: uploading and downloading data. Let's walk through a complete example that demonstrates the full cycle.

The first part of this code sets up the emulator connection and creates the bucket, as we've discussed. The interesting part begins with creating a blob object. A blob represents an individual object within your bucket — in this case, a file named ping.txt.

It's important to understand that a blob in object storage is not a traditional file on a file system. It's an object — a data entity stored inside a bucket, along with metadata. It behaves like a file in many ways: it has a name (ping.txt), content, and can be uploaded/downloaded. The upload_from_string method takes the string "hello emulator" and stores it as the content of the file ping.txt in the local storage emulator. This is ideal for storing simple text, configuration data, or any other string-based information.

The upload happens immediately, and the content is now stored in your local emulator. To verify that the upload worked correctly, the code immediately downloads the content using download_as_text(). This method retrieves the stored data and returns it as a string, allowing you to confirm that the round-trip operation was successful.

When you run this complete example, you should see output similar to:

Summary and Practice Preparation

You've now established a complete local development environment for object storage operations. The key concepts you've mastered include setting up fake-gcs-server, configuring the Python client to connect to your local environment, creating storage buckets, and performing basic upload and download operations with blob objects.

Your local development environment is now ready for more advanced storage workflows. The emulator provides a safe, cost-free environment where you can experiment with different storage patterns and test your applications thoroughly before deploying to the cloud.

In the upcoming practice exercises, you'll build on this foundation to work with more sophisticated scenarios. You'll handle image uploads with validation, learn to generate public URLs for your stored objects, and explore advanced object management operations like listing, filtering, and deletion. The solid groundwork you've established in this lesson will make these advanced topics much more approachable and practical.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal