Package: gcp-storage-emulator
Local Emulator for Google Cloud StorageGoogle 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 APIInstallationpip install gcp-storage-emulatorCLI UsageStarting the emulatorStart the emulator with:gcp-storage-emulatorstartBy default, the server will listen onhttp://localhost:9023and data is stored under./.cloudstorage. You can configure the folder using the env variablesSTORAGE_BASE(default./) andSTORAGE_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--in-memoryparameter. For tests, you might want to consider starting up the server from your code (see thePython APIs)If you're using the Google client library (e.g.google-cloud-storagefor Python) then you can set theSTORAGE_EMULATOR_HOSTenvironment variable to tell the library to connect to your emulator endpoint rather than the standardhttps://storage.googleapis.com, e.g.:exportSTORAGE_EMULATOR_HOST=http://localhost:9023Wiping dataYou can wipe the data by runninggcp-storage-emulatorwipeYou can pass--keep-bucketsto wipe the data while keeping the buckets.ExampleUse in-memory storage and automatically create default storage bucketmy-bucket.gcp-storage-emulatorstart--host=localhost--port=9023--in-memory--default-bucket=my-bucketPython APIsTo start a server from your code you can dofromgcp_storage_emulator.serverimportcreate_serverserver=create_server("localhost",9023,in_memory=False)server.start()# ........server.stop()You can wipe the data by callingserver.wipe()This can also be achieved (e.g. during tests) by hitting the/wipeHTTP endpointExampleimportosfromgoogle.cloudimportstoragefromgcp_storage_emulator.serverimportcreate_serverHOST="localhost"PORT=9023BUCKET="test-bucket"# default_bucket parameter creates the bucket automaticallyserver=create_server(HOST,PORT,in_memory=True,default_bucket=BUCKET)server.start()os.environ["STORAGE_EMULATOR_HOST"]=f"http://{HOST}:{PORT}"client=storage.Client()bucket=client.bucket(BUCKET)blob=bucket.blob("blob1")blob.upload_from_string("test1")blob=bucket.blob("blob2")blob.upload_from_string("test2")forblobinbucket.list_blobs():content=blob.download_as_bytes()print(f"Blob [{blob.name}]:{content}")server.stop()DockerPull the Docker image.dockerpulloittaa/gcp-storage-emulatorInside the container instance, the value of thePORTenvironment variable always reflects the port to which requests are sent. It defaults to8080. The directory used for the emulated storage is located under/storagein the container. In the following example the host's directory$(pwd)/cloudstoragewill be bound to the emulated storage.dockerrun-d\-ePORT=9023\-p9023:9023\--namegcp-storage-emulator\-v"$(pwd)/cloudstorage":/storage\oittaa/gcp-storage-emulatorimportosfromgoogle.cloudimportexceptions,storageHOST="localhost"PORT=9023BUCKET="test-bucket"os.environ["STORAGE_EMULATOR_HOST"]=f"http://{HOST}:{PORT}"client=storage.Client()try:bucket=client.create_bucket(BUCKET)exceptexceptions.Conflict:bucket=client.bucket(BUCKET)blob=bucket.blob("blob1")blob.upload_from_string("test1")print(blob.download_as_bytes())