1
0
mirror of https://github.com/actix/examples synced 2025-03-20 10:35:18 +01: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
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
4 changed files with 95 additions and 37 deletions

View File

@ -14,6 +14,8 @@ cd forms/multipart-s3
1. edit `.env` key `AWS_SECRET_ACCESS_KEY` = your_key_secret 1. edit `.env` key `AWS_SECRET_ACCESS_KEY` = your_key_secret
1. edit `.env` key `AWS_S3_BUCKET_NAME` = your_bucket_name 1. edit `.env` key `AWS_S3_BUCKET_NAME` = your_bucket_name
The AWS SDK automatically reads these environment variables to configure the S3 client.
```sh ```sh
cargo run cargo run
``` ```
@ -25,6 +27,8 @@ Or, start the upload using [HTTPie]:
```sh ```sh
http --form POST :8080/ file@Cargo.toml http --form POST :8080/ file@Cargo.toml
http --form POST :8080/ file@Cargo.toml file@README.md meta='{"namespace":"foo"}' http --form POST :8080/ file@Cargo.toml file@README.md meta='{"namespace":"foo"}'
http GET :8080/file/<key_from_upload>
``` ```
Or, using cURL: Or, using cURL:
@ -32,6 +36,8 @@ Or, using cURL:
```sh ```sh
curl -X POST http://localhost:8080/ -F 'file=@Cargo.toml' curl -X POST http://localhost:8080/ -F 'file=@Cargo.toml'
curl -X POST http://localhost:8080/ -F 'file=@Cargo.toml' -F 'file=@README.md' -F 'meta={"namespace":"foo"}' curl -X POST http://localhost:8080/ -F 'file=@Cargo.toml' -F 'file=@README.md' -F 'meta={"namespace":"foo"}'
curl http://localhost:8080/file/<key_from_upload>
``` ```
[httpie]: https://httpie.org [httpie]: https://httpie.org

View File

@ -1,9 +1,9 @@
use std::env; use std::env;
use actix_web::Error; use actix_web::{error, web::Bytes, Error};
use aws_config::SdkConfig as AwsConfig; use aws_config::SdkConfig as AwsConfig;
use aws_sdk_s3::{types::ByteStream, Client as S3Client}; 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 tokio::{fs, io::AsyncReadExt as _};
use crate::{TempFile, UploadedFile}; 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( pub async fn upload_files(
&self, &self,
temp_files: Vec<TempFile>, temp_files: Vec<TempFile>,
@ -82,24 +106,19 @@ impl Client {
.body(ByteStream::from(contents)) .body(ByteStream::from(contents))
.send() .send()
.await .await
.expect("Failed to put test object"); .expect("Failed to put object");
self.url(key) self.url(key)
} }
pub async fn delete_files(&self, keys: Vec<&str>) { /// Attempts to deletes object from S3. Returns true if successful.
for key in keys { pub async fn delete_file(&self, key: &str) -> bool {
self.delete_object(key).await;
}
}
async fn delete_object(&self, key: &str) {
self.s3 self.s3
.delete_object() .delete_object()
.bucket(&self.bucket_name) .bucket(&self.bucket_name)
.key(key) .key(key)
.send() .send()
.await .await
.expect("Couldn't delete object"); .is_ok()
} }
} }

View File

@ -1,35 +1,38 @@
<!-- TODO: fix me -->
<html> <html>
<head><title>Upload Test</title></head> <head><title>S3 Upload Test</title></head>
<body> <body>
<form target="/" method="post" enctype="multipart/form-data" id="myForm" > <form target="/" method="post" enctype="multipart/form-data" id="s3UploadForm">
<input type="text" id="text" name="text" value="test_text"/> <label>
<input type="number" id="number" name="number" value="123123"/> Namespace:
<input type="button" value="Submit" onclick="myFunction()"></button> <input type="text" name="namespace" value="default" />
</form> </label>
<input type="file" multiple name="file" id="myFile"/> <input type="file" multiple name="file" />
</body> <input type="button" value="Submit" onclick="upload()"></button>
</form>
<script> <script>
async function upload() {
var $form = document.getElementById('s3UploadForm');
var $files = document.getElementsByName('file')[0];
function myFunction(){ const formData = new FormData();
var myForm = document.getElementById('myForm');
var myFile = document.getElementById('myFile');
let formData = new FormData(); const meta = { namespace: $form.namespace.value };
const obj = { console.log(meta);
text: document.getElementById('text').value,
number: Number(document.getElementById('number').value)
};
const json = JSON.stringify(obj);
console.log(obj);
console.log(json);
formData.append("data", json); formData.append("meta", JSON.stringify(meta));
formData.append("myFile", myFile.files[0]);
var request = new XMLHttpRequest(); for (const file in $files.files) {
request.open("POST", ""); formData.append("file", file);
request.send(formData); }
}
await fetch(new URL(window.location), {
method: 'POST',
formData
});
}
</script> </script>
</body>
</html> </html>

View File

@ -1,9 +1,12 @@
use std::fs; use std::fs;
use actix_multipart::Multipart; use actix_multipart::Multipart;
use actix_web::body::SizedStream;
use actix_web::{delete, error};
use actix_web::{ use actix_web::{
get, middleware::Logger, post, web, App, Error, HttpResponse, HttpServer, Responder, get, middleware::Logger, post, web, App, Error, HttpResponse, HttpServer, Responder,
}; };
use actix_web_lab::extract::Path;
use actix_web_lab::respond::Html; use actix_web_lab::respond::Html;
use aws_config::meta::region::RegionProviderChain; use aws_config::meta::region::RegionProviderChain;
use dotenv::dotenv; use dotenv::dotenv;
@ -58,6 +61,31 @@ async fn upload_to_s3(
}))) })))
} }
#[get("/file/{s3_key}*")]
async fn fetch_from_s3(
s3_client: web::Data<Client>,
Path((s3_key,)): Path<(String,)>,
) -> Result<impl Responder, Error> {
let (file_size, file_stream) = s3_client
.fetch_file(&s3_key)
.await
.ok_or_else(|| error::ErrorNotFound("file with specified key not found"))?;
Ok(HttpResponse::Ok().body(SizedStream::new(file_size, file_stream)))
}
#[delete("/file/{s3_key}*")]
async fn delete_from_s3(
s3_client: web::Data<Client>,
Path((s3_key,)): Path<(String,)>,
) -> Result<impl Responder, Error> {
if s3_client.delete_file(&s3_key).await {
Ok(HttpResponse::NoContent().finish())
} else {
Err(error::ErrorNotFound("file with specified key not found"))
}
}
#[get("/")] #[get("/")]
async fn index() -> impl Responder { async fn index() -> impl Responder {
Html(include_str!("./index.html").to_owned()) Html(include_str!("./index.html").to_owned())
@ -87,6 +115,8 @@ async fn main() -> std::io::Result<()> {
App::new() App::new()
.service(index) .service(index)
.service(upload_to_s3) .service(upload_to_s3)
.service(fetch_from_s3)
.service(delete_from_s3)
.wrap(Logger::default()) .wrap(Logger::default())
.app_data(web::Data::new(s3_client.clone())) .app_data(web::Data::new(s3_client.clone()))
}) })