abdolence / gcloud-sdk-rs

Async Google Cloud Platform (GCP) gRPC/REST APIs client implementation based on Tonic middleware and Reqwest.
Apache License 2.0
73 stars 22 forks source link

Looking for code examples on how to connect Google Cloud Storage to `fsouza/fake-gcs-server` #165

Closed grindarius closed 1 month ago

grindarius commented 1 month ago

Hello and first of all thank you for this amazing library. I am currently wanted to experiment with this library and setup reproducible local environment. But I have a question, how can I connect this library to local fake google cloud server on Docker like fsouza/fake-gcs-server? I have looked into how to initialize GoogleRestApi client that is used for the storage connections but I don't really see how to integrate or set up the client locally. Is there any code examples I can follow somewhere? Thank you.

I have tried setting up fsouza/fake-gcs-server like this through docker compose.

services:
  google-cloud-storage:
    image: fsouza/fake-gcs-server:latest
    container_name: gcloud-storage-anonymous-gcloud
    ports:
      - 4443:4443
    command: [ "-scheme", "http", "-port", "4443", "-public-host", "gcs:4443" ]
    volumes:
      - ./buckets/gee2/:/data/gee2/
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:4443/storage/v1/b/"]
      interval: 5s
      timeout: 10s
      retries: 3

Then I wrote some code like this

use std::env::set_var;

use gcloud_sdk::google_rest_apis::storage_v1::objects_api::{
    storage_objects_list, StoragePeriodObjectsPeriodListParams,
};
use gcloud_sdk::GoogleRestApi;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let google_client = GoogleRestApi::new().await?;

    set_var("STORAGE_EMULATOR_HOST", "http://localhost:4443");

    let files = storage_objects_list(
        &google_client.create_google_storage_v1_config().await?,
        StoragePeriodObjectsPeriodListParams {
            bucket: "gee2".to_string(),
            ..Default::default()
        },
    )
    .await?;

    println!("{:?}", files.items.unwrap());
    println!("Hello, world!");

    Ok(())
}

Turned out I'm trying to connect the production bucket and not my local bucket, So I am a bit confused how to go on next from here. Thank you for any help.

abdolence commented 1 month ago

Hey, GCP SDK REST client is basically slim wrapper on reqwest client, so it doesn't have anything fancy for specific APIs such as emulators.

To achieve that you just need to create a config yourself when you need to connect to emulator. Something like:

    let config = gcloud_sdk::google_rest_apis::storage_v1::configuration::Configuration {
        client: google_rest_client.client.clone(),
        base_path: "https://localhost:4443/storage/v1".to_owned(),
        ..Default::default()
    };

you probably want to extract it some kind of function and based on environment variable or config create different configs for prod and localhost.

grindarius commented 1 month ago

Thank you for the help. I have decided to make a function that I will call instead of calling create_google_storage_v1_config() directly to create the local/fake configuration now. Thank you for the help.