1
0
mirror of https://github.com/actix/examples synced 2025-06-27 09:29:02 +02:00

add read and detele object endpoints to s3 example

This commit is contained in:
Rob Ede
2022-08-01 03:53:29 +01:00
parent c8a4544ea1
commit 7a036d97b9
4 changed files with 95 additions and 37 deletions

View File

@ -1,9 +1,9 @@
use std::env;
use actix_web::Error;
use actix_web::{error, web::Bytes, Error};
use aws_config::SdkConfig as AwsConfig;
use aws_sdk_s3::{types::ByteStream, Client as S3Client};
use futures_util::{stream, StreamExt as _};
use futures_util::{stream, Stream, StreamExt as _, TryStreamExt as _};
use tokio::{fs, io::AsyncReadExt as _};
use crate::{TempFile, UploadedFile};
@ -32,6 +32,30 @@ impl Client {
)
}
pub async fn fetch_file(
&self,
key: &str,
) -> Option<(u64, impl Stream<Item = Result<Bytes, actix_web::Error>>)> {
let object = self
.s3
.get_object()
.bucket(&self.bucket_name)
.key(key)
.send()
.await
.ok()?;
Some((
object
.content_length()
.try_into()
.expect("file has invalid size"),
object
.body
.map_err(|err| error::ErrorInternalServerError(err)),
))
}
pub async fn upload_files(
&self,
temp_files: Vec<TempFile>,
@ -82,24 +106,19 @@ impl Client {
.body(ByteStream::from(contents))
.send()
.await
.expect("Failed to put test object");
.expect("Failed to put object");
self.url(key)
}
pub async fn delete_files(&self, keys: Vec<&str>) {
for key in keys {
self.delete_object(key).await;
}
}
async fn delete_object(&self, key: &str) {
/// Attempts to deletes object from S3. Returns true if successful.
pub async fn delete_file(&self, key: &str) -> bool {
self.s3
.delete_object()
.bucket(&self.bucket_name)
.key(key)
.send()
.await
.expect("Couldn't delete object");
.is_ok()
}
}