extras.fake_gcs — Google Cloud Storage Emulation


A YellowService emulating Google Cloud Storage. Runs the docker image of fake-gcs-server, fsouza’s unofficial emulator. You may use any client such as google-cloud-storage to connect to the service.

Note

Requires the gcs extra. For more information, see our installation guide.

class extras.fake_gcs.FakeGoogleCloudStorage(docker_client: DockerClient, image: str = 'fsouza/fake-gcs-server:1.42', scheme: str = 'https', command: str = '', *, container_create_kwargs: dict[str, Any] | None = None, **kwargs)[source]

A service that runs an fake GCS server. Inherits from SingleContainerService. Usable with RunMixin and AsyncRunMixin.

Parameters:
  • docker_client – The docker client to used to pull and create the Azurite container.

  • image – The image name to create a container of.

  • scheme – the scheme of the face service to serve

  • command – additional suffix to the service startup command (docker run --rm fsouza/fake-gcs-server:latest -help for a full list of commands)

  • container_create_kwargs – Additional keyword arguments passed to docker.models.containers.ContainerCollection.create().

  • **kwargs – Additional keyword arguments passed to SingleContainerService.

Has the following additional methods:

client_port() int[source]

Returns the port to be used when connecting to GCS from the docker host.

local_url(scheme: str | None = ...) str[source]

Returns a url to connect from host to container.

Parameters:

scheme – If used, will format the scheme of the url with the one provided, default is to use the scheme given to the service during construction. If None, the url will not include a scheme.

Note

To connect from a different container, see container_url() or host_url().

container_url(alias: str, hostname: str | None = ...) str[source]

Returns a url to connect from containers in a common network.

Parameters:
  • hostname – The alias of the fake GCS container within the network.

  • scheme – If used, will format the scheme of the url with the one provided, default is to use the scheme given to the service during construction. If None, the url will not include a scheme.

host_url(scheme: str | None = ...) str[source]

Returns a url to connect from containers across the docker host.

Parameters:

scheme – If used, will format the scheme of the url with the one provided, default is to use the scheme given to the service during construction. If None, the url will not include a scheme.

patch_gcloud_aio() AbstractContextManager[source]

Patches the global variables in the gcloud-aio-storage module, making new storage clients target self as an emulator. Returns a context manager that changes the global variables and restores them on exit.

Note

This method is not required when using gcloud-aio-storage 8.0.0 or upwards.

Raises:

ImportError – if gcloud-aio-storage is not installed

Example
cgs: FakeGoogleCloudStorage
with cgs.patch_gcloud_aio():
    async with ClientSession(connector=TCPConnector(ssl=False)) as session:
        storage = Storage(session=session)  # this storage will connect to gcs

Warning

This feature is temperamental as it effectively changes consts in an external module. No storage client created inside the context should exist outside of it and vice-versa.

create_bucket(bucket_name: str) dict[str, Any][source]

Creates a new bucket in the emulator. Returns the parsed response from the container (supposed to follow the google api)

Parameters:

bucket_name – The name of the bucket to create

clear_bucket(bucket_name: str, prefix: str | None = None) Iterable[str][source]

Removes all objects in a bucket. Returns an iterable of the names of all objects deleted.

Parameters:
  • bucket_name – The name of the bucket to clear.

  • prefix – If specified, will only delete object with the specified prefix.

delete_bucket(bucket_name: str, force: bool = False, missing_ok: bool = False)[source]

Deletes a bucket in the emulator.

Parameters:
  • bucket_name – The name of the bucket to delete

  • force – If set to True, will also delete all objects in the bucket beforehand. Deleting a non-empty bucket without force=True will raise an exception.

  • missing_ok – If set to True, will not raise an exception if the bucket does not exist.