|
1 | 1 | use std::{fmt, path::PathBuf, time::Duration}; |
2 | 2 |
|
3 | 3 | use opendal::{services::S3, Operator}; |
| 4 | +use tokio::fs::File; |
4 | 5 |
|
5 | 6 | use crate::{appstate::AppState, config::Config, errors::AtomicServerResult}; |
6 | 7 |
|
@@ -39,7 +40,6 @@ impl FileStore { |
39 | 40 | pub fn encoded(&self) -> String { |
40 | 41 | urlencoding::encode(self.prefix()).into_owned() |
41 | 42 | } |
42 | | - |
43 | 43 | } |
44 | 44 |
|
45 | 45 | impl fmt::Display for FileStore { |
@@ -78,9 +78,13 @@ pub async fn s3_upload_object( |
78 | 78 |
|
79 | 79 | let op: Operator = Operator::new(builder)?.finish(); |
80 | 80 |
|
81 | | - let buffer = std::fs::read(file_path)?; |
82 | | - op.write(&format!("{}/{}", path, &file_id), buffer).await?; |
| 81 | + let mut tmp_file = File::open(file_path).await?; |
| 82 | + let length = tmp_file.metadata().await?.len(); |
83 | 83 |
|
| 84 | + let s3_path = format!("{}/{}", path, &file_id); |
| 85 | + let mut w = op.writer_with(&s3_path).content_length(length).await?; |
| 86 | + tokio::io::copy(&mut tmp_file, &mut w).await?; |
| 87 | + w.close().await?; |
84 | 88 | Ok(()) |
85 | 89 | } |
86 | 90 |
|
@@ -123,7 +127,11 @@ pub async fn get_s3_signed_url( |
123 | 127 |
|
124 | 128 | let op: Operator = Operator::new(builder)?.finish(); |
125 | 129 |
|
126 | | - let uri = op.presign_read(&format!("{}/{}", &path, file_id), duration).await?.uri().to_string(); |
| 130 | + let uri = op |
| 131 | + .presign_read(&format!("{}/{}", &path, file_id), duration) |
| 132 | + .await? |
| 133 | + .uri() |
| 134 | + .to_string(); |
127 | 135 |
|
128 | 136 | Ok(uri) |
129 | 137 | } |
0 commit comments