Skip to content

Commit

Permalink
Docker example (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
oittaa authored Dec 21, 2021
1 parent 5b9e8c6 commit 0a1d8ff
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ This can also be achieved (e.g. during tests) by hitting the `/wipe` endpoint
Create a test file `emulator-test.py`.

```python
from google.auth.credentials import AnonymousCredentials
import os

from google.cloud import storage
from gcp_storage_emulator.server import create_server

Expand All @@ -95,26 +96,27 @@ BUCKET = "test-bucket"
server = create_server(HOST, PORT, in_memory=True, default_bucket=BUCKET)
server.start()

client = storage.Client(
credentials=AnonymousCredentials(),
project="test",
)
# Avoid authentication when connecting to a storage emulator
# https://github.com/googleapis/python-storage/issues/324
os.environ["STORAGE_EMULATOR_HOST"] = f"http://{HOST}:{PORT}"
client = storage.Client.create_anonymous_client()
client.project = "test"

bucket = client.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))
print(f"Blob [{blob.name}]: {content}")

server.stop()
```

Run the following commands to test the emulated storage.

```bash
export STORAGE_EMULATOR_HOST=http://localhost:9023
python3 emulator-test.py
```

Expand All @@ -129,10 +131,36 @@ 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 under `/storage` in the container. In the following example the host's directory `$(pwd)/cloudstorage` will be bound to the emulated storage.

```
```bash
docker run -d \
-p 8080:8080 \
-e PORT=9023 \
-p 9023:9023 \
--name gcp-storage-emulator \
-v "$(pwd)/cloudstorage":/storage \
oittaa/gcp-storage-emulator
```

```python
import os

from google.cloud import exceptions, storage

HOST = "localhost"
PORT = 9023
BUCKET = "test-bucket"

# Avoid authentication when connecting to a storage emulator
# https://github.com/googleapis/python-storage/issues/324
os.environ["STORAGE_EMULATOR_HOST"] = f"http://{HOST}:{PORT}"
client = storage.Client.create_anonymous_client()
client.project = "test"

try:
bucket = client.create_bucket(BUCKET)
except exceptions.Conflict:
bucket = client.bucket(BUCKET)

blob = bucket.blob("blob1")
blob.upload_from_string("test1")
print(blob.download_as_bytes())
```

0 comments on commit 0a1d8ff

Please sign in to comment.