object_storage/oss/
object.rs

1use super::{
2    util::{
3        authorization::create_authorization,
4        datetime::create_datetime,
5        headers::construct_headers,
6        host::{create_host, create_resource, create_url},
7        request::request,
8    },
9    Bucket, Content,
10};
11use anyhow::{anyhow, Result};
12use reqwest::Method;
13
14pub async fn put_object(
15    bucket: &Bucket,
16    file: &[u8],
17    content: &Content,
18    file_name: &str,
19) -> Result<String> {
20    let host = create_host(Some(&bucket.bucket_name), &bucket.endpoint)
21        .await
22        .map_err(|e| anyhow!("{}", e))?;
23    let url = create_url(&host, Some(file_name))
24        .await
25        .map_err(|e| anyhow!("{}", e))?;
26    let datetime = create_datetime().await.map_err(|e| anyhow!("{}", e))?;
27    let caon_resource = create_resource(&bucket.bucket_name, file_name)
28        .await
29        .map_err(|e| anyhow!("{}", e))?;
30    let authorization = create_authorization(
31        &bucket.auth,
32        "PUT",
33        Some(content),
34        &datetime,
35        None,
36        &caon_resource,
37    )
38    .await
39    .map_err(|e| anyhow!("{}", e))?;
40    let content_length = file.len().to_string();
41    let headers = construct_headers(
42        Some(&content.content_md5),
43        Some(&content_length),
44        Some(&content.content_type),
45        &host,
46        &datetime,
47        &authorization,
48    )
49    .await;
50    let _text = request(Method::PUT, &url, headers, Some(file.to_vec()))
51        .await
52        .map_err(|e| anyhow!("{}", e))?;
53    Ok(url)
54}