forked from JSH32/Backpack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage.rs
48 lines (43 loc) · 1.51 KB
/
storage.rs
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
use rusoto_s3::{*};
use rusoto_core::{*};
use infer;
pub struct Storage {
bucket: String,
client: S3Client
}
impl Storage {
pub fn new(bucket: &str, access_key: &str, secret_key: &str, s3_region: Region) -> Self {
let credential_provider = credential::StaticProvider::new_minimal(access_key.to_string(), secret_key.to_string());
Self {
client: S3Client::new_with(HttpClient::new().expect("S3 dispatcher could not be created"), credential_provider, s3_region),
bucket: bucket.into()
}
}
pub async fn put_object(&self, name: &str, data: Vec<u8>) -> Result<(), RusotoError<PutObjectError>> {
// Attempt to detect content type
let content_type = match infer::get(&data) {
Some(kind) => {
Some(kind.mime_type().to_string())
},
None => None
};
// Upload to S3 API
self.client.put_object(PutObjectRequest {
bucket: self.bucket.clone(),
body: Some(ByteStream::from(data)),
key: name.to_string(),
acl: Some("public-read".into()),
content_type: content_type,
..Default::default()
}).await?;
Ok(())
}
pub async fn delete_object(&self, name: &str) -> Result<(), RusotoError<DeleteObjectError>> {
self.client.delete_object(DeleteObjectRequest {
bucket: self.bucket.clone(),
key: name.to_string(),
..Default::default()
}).await?;
Ok(())
}
}