https://github.com/tofull/gcs_simulator
Simulator of Google Cloud Storage python API using local folder as "bucket".
https://github.com/tofull/gcs_simulator
Last synced: 10 months ago
JSON representation
Simulator of Google Cloud Storage python API using local folder as "bucket".
- Host: GitHub
- URL: https://github.com/tofull/gcs_simulator
- Owner: Tofull
- License: mit
- Created: 2020-12-11T03:04:11.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-12-11T17:07:41.000Z (over 5 years ago)
- Last Synced: 2025-06-27T19:47:22.680Z (12 months ago)
- Language: Python
- Size: 11.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Google Cloud Storage simulator
A simple simulator which allows to use a local folder as "bucket" and try to mimic the `google.cloud.storage` API.
Implemented behaviours:
- `Client`
- `Bucket`
- `get_blob`
- `blob`
- `Blob`
- `download_to_filename`
- `upload_from_filename`
- `delete`
## Example
Here is a quick overview how to use this simulator.
```python
from unittest import mock
from gcs_simulator.storage import MockClient
# define a fake client which will replace the google.cloud.storage.Client object.
class FakeClient(MockClient):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_local_root_folder_used_for_simulation("local_folder_used_by_simulator")
# ...
# and later, use the fake client as follow:
from google.cloud import storage
@mock.patch("google.cloud.storage.Client", FakeClient)
def any_function_which_uses_storage_API():
# this function will use the mocked Client
storage_client = storage.Client()
# retrieve blob
any_blob = storage_client.bucket("any_bucket").get_blob("any_stored_blob")
any_blob.download_to_filename("any_local_filename")
# upload blob
another_blob = storage_client.bucket("another_bucket").blob("another_blob")
another_blob.upload_from_filename("any_local_filename")
# manage blob
any_blob.delete()
```
See [example folder](./example) to see how to use this simulator with background cloud functions.
# Note for developers
```sh
conda env create --file environment.yml
conda activate gcs_simulator_dev_environment
pre-commit install
python -m pytest
```