-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy paths3.rs
92 lines (79 loc) · 2.89 KB
/
s3.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
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
use async_trait::async_trait;
use aws_sdk_s3::{
error::SdkError, operation::write_get_object_response::WriteGetObjectResponseError, primitives::ByteStream,
Client as S3Client,
};
use lambda_runtime::tracing;
use std::{error, io::Read};
pub trait GetFile {
fn get_file(&self, url: String) -> Result<Vec<u8>, Box<dyn error::Error>>;
}
#[async_trait]
pub trait SendFile {
async fn send_file(&self, route: String, token: String, vec: Vec<u8>) -> Result<String, Box<dyn error::Error>>;
}
impl GetFile for S3Client {
fn get_file(&self, url: String) -> Result<Vec<u8>, Box<dyn error::Error>> {
tracing::info!("get file url {}", url);
let resp = ureq::get(&url).call()?;
let len: usize = resp.header("Content-Length").unwrap().parse()?;
let mut bytes: Vec<u8> = Vec::with_capacity(len);
std::io::Read::take(resp.into_reader(), 10_000_000).read_to_end(&mut bytes)?;
tracing::info!("got {} bytes", bytes.len());
Ok(bytes)
}
}
#[async_trait]
impl SendFile for S3Client {
async fn send_file(&self, route: String, token: String, vec: Vec<u8>) -> Result<String, Box<dyn error::Error>> {
tracing::info!("send file route {}, token {}, length {}", route, token, vec.len());
let bytes = ByteStream::from(vec);
let write = self
.write_get_object_response()
.request_route(route)
.request_token(token)
.status_code(200)
.body(bytes)
.send()
.await;
if let Err(err) = write {
check_error(err);
Err("WriteGetObjectResponse creation error".into())
} else {
Ok("File sent.".to_string())
}
}
}
fn check_error(error: SdkError<WriteGetObjectResponseError>) {
match error {
SdkError::ConstructionFailure(_err) => {
tracing::info!("ConstructionFailure");
}
SdkError::DispatchFailure(err) => {
tracing::info!("DispatchFailure");
if err.is_io() {
tracing::info!("IO error");
}
if err.is_timeout() {
tracing::info!("Timeout error");
}
if err.is_user() {
tracing::info!("User error");
}
if err.is_other() {
tracing::info!("Other error");
}
}
SdkError::ResponseError(_err) => tracing::info!("ResponseError"),
SdkError::TimeoutError(_err) => tracing::info!("TimeoutError"),
SdkError::ServiceError(err) => {
tracing::info!("ServiceError");
let wgore = err.into_err();
let meta = wgore.meta();
let code = meta.code().unwrap_or_default();
let msg = meta.message().unwrap_or_default();
tracing::info!("code: {}, message: {}, meta: {}", code, msg, meta);
}
_ => tracing::info!("other error"),
}
}