1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::path::PathBuf;

use async_trait::async_trait;
use aws_sdk_s3::ByteStream;
use bytes::Buf;
use cargolifter_core::config::S3Config;

pub struct S3Storage {
    client: aws_sdk_s3::Client,
    config: S3Config,
}

impl S3Storage {
    pub async fn new(s3config: S3Config) -> Self {
        let loader = aws_config::from_env();
        if let Some(credentials) = &s3config.credentials {
            let credentials = aws_types::Credentials::new(
                credentials.access_key.clone(),
                credentials.secret_key.clone(),
                credentials.secret_token.clone(),
                None,
                "s3storage",
            );
            let config = loader.credentials_provider(credentials).load().await;
            Self {
                client: aws_sdk_s3::Client::new(&config),
                config: s3config,
            }
        } else {
            let config = loader.load().await;
            Self {
                client: aws_sdk_s3::Client::new(&config),
                config: s3config,
            }
        }
    }
}

#[async_trait]
impl cargolifter_core::Storage for S3Storage {
    async fn get(
        &self,
        crate_name: &str,
        crate_version: &str,
    ) -> Result<Vec<u8>, cargolifter_core::models::StorageError> {
        let path = PathBuf::new();
        let path = path.join(cargolifter_core::get_crate_path(crate_name));
        let path = path.join(crate_version);
        tracing::info!("trying to get '{}'", path.to_str().unwrap());

        match self
            .client
            .get_object()
            .set_bucket(Some(self.config.bucket.clone()))
            .set_key(Some(path.to_str().unwrap().into()))
            .send()
            .await
        {
            Ok(resp) => {
                let stream = resp.body;
                match stream.collect().await {
                    Ok(bytes) => Ok(bytes.chunk().to_vec()),
                    Err(_) => Err(cargolifter_core::models::StorageError::DetailMeLater),
                }
            }
            Err(_) => Err(cargolifter_core::models::StorageError::DetailMeLater),
        }
    }

    async fn put(
        &mut self,
        crate_name: &str,
        crate_version: &str,
        data: &[u8],
    ) -> Result<(), cargolifter_core::models::StorageError> {
        let path = PathBuf::new();
        let path = path.join(cargolifter_core::get_crate_path(crate_name));
        let path = path.join(crate_version);
        tracing::info!("adding '{}' to storage", path.to_str().unwrap());

        let byte_stream = ByteStream::from(data.to_vec());
        match self
            .client
            .put_object()
            .set_bucket(Some(self.config.bucket.clone()))
            .set_key(Some(path.to_str().unwrap().into()))
            .set_body(Some(byte_stream))
            .send()
            .await
        {
            Ok(_) => Ok(()),
            Err(_) => Err(cargolifter_core::models::StorageError::DetailMeLater),
        }
    }
}