forked from kenkoooo/AtCoderProblems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3.rs
More file actions
46 lines (41 loc) · 1.29 KB
/
Copy paths3.rs
File metadata and controls
46 lines (41 loc) · 1.29 KB
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
use anyhow::Result;
use s3::bucket::Bucket;
use s3::creds::Credentials;
const BUCKET_NAME: &str = "kenkoooo.com";
const REGION: &str = "ap-northeast-1";
pub struct S3Client {
bucket: Bucket,
}
impl S3Client {
pub fn new() -> Result<Self> {
let region = REGION.parse()?;
let credentials = Credentials::from_instance_metadata()?;
let bucket = Bucket::new(BUCKET_NAME, region, credentials)?;
Ok(Self { bucket })
}
pub async fn update(&self, data: Vec<u8>, path: &str) -> Result<bool> {
log::info!("Fetching old data ...");
let old_data = self
.bucket
.get_object(path)
.await
.map(|(data, _)| data)
.unwrap_or_else(|e| {
log::error!("{:?}", e);
Vec::new()
});
if old_data != data {
log::info!("Uploading new data to {} ...", path);
let (data, status) = self
.bucket
.put_object_with_content_type(path, &data, "application/json;charset=utf-8")
.await?;
log::info!("data={:?}", data);
log::info!("status={}", status);
Ok(true)
} else {
log::info!("No update on {}", path);
Ok(false)
}
}
}