A stub emulator for the Google Cloud Storage API
Project description
Local Emulator for Google Cloud Storage
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 .
CLI Usage
Starting the emulator
Start the emulator with:
gcp-storage-emulator start --port=9090
By default, data is stored under $PWD/.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 (e.g. for text execution) by calling server.wipe()
This can also be achieved (e.g. during tests) by hitting the /wipe
endpoint
Docker
Pull the Docker image.
docker pull oittaa/gcp-storage-emulator
Or build it.
docker build -t gcp-storage-emulator .
Run the container. Directory cloudstorage
will be used for the emulated storage.
docker run -d \
-p 8080:8080 \
--name gcp-storage-emulator \
-v "$(pwd)"/cloudstorage:/app/.cloudstorage \
gcp-storage-emulator
Example
Create a test file emulator-test.py
.
from google.auth.credentials import AnonymousCredentials
from google.cloud import storage, exceptions
BUCKET = "test-bucket"
client = storage.Client(
credentials=AnonymousCredentials(),
project="test",
)
try:
bucket = client.get_bucket(BUCKET)
except exceptions.NotFound:
bucket = client.bucket(BUCKET)
bucket = client.create_bucket(bucket)
blob = bucket.blob("key1")
blob.upload_from_string("test1")
blob = bucket.blob("key2")
blob.upload_from_string("test2")
for blob in bucket.list_blobs():
print(f"Blob: {blob.name}")
Run the following commands to insert test data into the emulated storage.
export STORAGE_EMULATOR_HOST=http://localhost:8080
python3 emulator-test.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gcp-storage-emulator-2021.2.12.tar.gz
.
File metadata
- Download URL: gcp-storage-emulator-2021.2.12.tar.gz
- Upload date:
- Size: 17.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a4d433bccbccd84ecfddf19fedd4bef21b8ab482657607e4dfd8e26b1701725 |
|
MD5 | cc7ef05ccda7b5df564d4c1bf8bad2b7 |
|
BLAKE2b-256 | 560fbcb14f60fe5b8158ebbf187694eb08f348b53dd5fd4fffb9e09a7b52e43c |
File details
Details for the file gcp_storage_emulator-2021.2.12-py3-none-any.whl
.
File metadata
- Download URL: gcp_storage_emulator-2021.2.12-py3-none-any.whl
- Upload date:
- Size: 21.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3807fc802b8966d7d59a404417b50564f3b712c89f9ca5293f8eda7b03b7b73 |
|
MD5 | 7c9f867d4fb9c1c03e5c291153e1ba87 |
|
BLAKE2b-256 | 62dcc8c786179ac9c8522f8150870d4fd36ac4fb30e07fc17a9a25b73980e924 |