Skip to main content

A stub emulator for the Google Cloud Storage API

Project description

Local Emulator for Google Cloud Storage

CI codecov Code style: black

Google doesn't (yet) ship an emulator for the Cloud Storage API like they do for Cloud Datastore.

This is a stub emulator so you can run your tests and do local development without having to connect to the production Storage APIs.

THIS IS A WORK IN PROGRESS AND ONLY SUPPORTS A LIMITED SUBSET OF THE API


Installation

pip install gcp-storage-emulator

CLI Usage

Starting the emulator

Start the emulator with:

gcp-storage-emulator start --port=9090

By default, data is stored under ./.cloudstorage. You can configure the folder using the env variables STORAGE_BASE (default ./) and STORAGE_DIR (default .cloudstorage).

If you wish to run the emulator in a testing environment or if you don't want to persist any data, you can use the --no-store-on-disk parameter. For tests, you might want to consider starting up the server from your code (see the Python APIs)

If you're using the Google client library (e.g. google-cloud-storage for Python) then you can set the STORAGE_EMULATOR_HOST environment variable to tell the library to connect to your emulator endpoint rather than the standard https://storage.googleapis.com, e.g.:

export STORAGE_EMULATOR_HOST=http://localhost:9090

Wiping data

You can wipe the data by running

gcp-storage-emulator wipe

Python APIs

To start a server from your code you can do

from gcp_storage_emulator.server import create_server

server = create_server("localhost", 9023, in_memory=False)

server.start()
# ........
server.stop()

You can wipe the data by calling server.wipe()

This can also be achieved (e.g. during tests) by hitting the /wipe endpoint

Example

Create a test file emulator-test.py.

from google.auth.credentials import AnonymousCredentials
from google.cloud import storage
from gcp_storage_emulator.server import create_server

PORT = 8080
BUCKET = "test-bucket"

server = create_server("localhost", PORT, in_memory=True)
server.start()

client = storage.Client(
    credentials=AnonymousCredentials(),
    project="test",
)
bucket = client.bucket(BUCKET)
bucket = client.create_bucket(bucket)
blob = bucket.blob("blob1")
blob.upload_from_string("test1")
blob = bucket.blob("blob2")
blob.upload_from_string("test2")
for blob in bucket.list_blobs():
    content = blob.download_as_bytes()
    print("Blob [{}]: {}".format(blob.name, content))
server.stop()

Run the following commands to test the emulated storage.

export STORAGE_EMULATOR_HOST=http://localhost:8080
python3 emulator-test.py

Docker

Pull the Docker image.

docker pull oittaa/gcp-storage-emulator

Inside the container instance, the value of the PORT environment variable always reflects the port to which requests are sent. It defaults to 8080. The directory used for the emulated storage is located in /app/.cloudstorage. In the following example the host's directory $(pwd)/cloudstorage will be bound to the emulated storage.

docker run -d \
  -p 8080:8080 \
  --name gcp-storage-emulator \
  -v "$(pwd)"/cloudstorage:/app/.cloudstorage \
  oittaa/gcp-storage-emulator

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gcp-storage-emulator-2021.3.27.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

gcp_storage_emulator-2021.3.27-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

File details

Details for the file gcp-storage-emulator-2021.3.27.tar.gz.

File metadata

  • Download URL: gcp-storage-emulator-2021.3.27.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for gcp-storage-emulator-2021.3.27.tar.gz
Algorithm Hash digest
SHA256 54a0b9c9ee3475c48d6a6f45b191e2fcfae96311504be243bb8f6f131003f2a3
MD5 956a5f886c37689444720e80c6cc3b9c
BLAKE2b-256 c2b7c28e6469a98218fc60c8675c9761d13ab6a93da3ca78a9b70ea6a9cf24ae

See more details on using hashes here.

File details

Details for the file gcp_storage_emulator-2021.3.27-py3-none-any.whl.

File metadata

  • Download URL: gcp_storage_emulator-2021.3.27-py3-none-any.whl
  • Upload date:
  • Size: 24.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for gcp_storage_emulator-2021.3.27-py3-none-any.whl
Algorithm Hash digest
SHA256 954ff7315ebe93c8f73345bb8c71e326f892fd0c2760eba6c98a8a854f9b7828
MD5 ec6767c510d14b32b8f6e8368a2afd4a
BLAKE2b-256 4cef4f7e58a78922da965821445367587848787c3adaf5dae5c33de448df5912

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page