Skip to main content

image_thumbs/
gcs.rs

1use crate::{ImageThumbs, ThumbsResult, model::Params};
2use object_store::gcp::{GoogleCloudStorage, GoogleCloudStorageBuilder};
3use std::sync::Arc;
4use tokio::sync::RwLock;
5
6impl ImageThumbs<GoogleCloudStorage> {
7    /// Creates new ImageThumbs instance connected to Google Cloud Storage using the environment
8    /// variables `GOOGLE_BUCKET` and `GOOGLE_SERVICE_ACCOUNT_KEY` to connect to GCS.
9    /// The later should be in the JSON format.
10    ///
11    /// Reads the config YAML file to know which thumbnails to create
12    ///
13    /// The config file must look like the example in `examples/image_thumbs.yaml`:
14    /// ```yaml
15    #[doc = include_str!("../examples/image_thumbs.yaml")]
16    /// ```
17    ///
18    /// # Arguments
19    /// * `config` - Path to the config file from the crate root (`.yaml` may be omitted)
20    pub fn new(config: &str) -> ThumbsResult<Self> {
21        let client = GoogleCloudStorageBuilder::from_env()
22            .with_client_options(Self::client_options())
23            .build()?;
24
25        Ok(Self {
26            client: Arc::new(RwLock::new(client)),
27            settings: Arc::new(Self::settings(config)?),
28        })
29    }
30
31    pub fn new_with_settings(settings: Vec<Params>) -> ThumbsResult<Self> {
32        let client = GoogleCloudStorageBuilder::from_env()
33            .with_client_options(Self::client_options())
34            .build()?;
35
36        Ok(Self {
37            client: Arc::new(RwLock::new(client)),
38            settings: Arc::new(settings),
39        })
40    }
41}