All checks were successful
continuous-integration/drone/push Build is passing
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [rust-s3](https://github.com/durch/rust-s3) | dependencies | minor | `0.33.0` -> `0.34.0` | --- ### Release Notes <details> <summary>durch/rust-s3 (rust-s3)</summary> ### [`v0.34.0`](https://github.com/durch/rust-s3/releases/tag/v0.34.0) [Compare Source](https://github.com/durch/rust-s3/compare/v0.33.0...v0.34.0) #### What's Changed - Fix etag generation for small files by [@​theCapypara](https://github.com/theCapypara) in https://github.com/durch/rust-s3/pull/324 - add support for new region eu-central-2 by [@​robinfriedli](https://github.com/robinfriedli) in https://github.com/durch/rust-s3/pull/314 - fix: presigned custom header order by [@​zifeo](https://github.com/zifeo) in https://github.com/durch/rust-s3/pull/309 - refactor(s3): don't depend on reqwest by [@​tomkarw](https://github.com/tomkarw) in https://github.com/durch/rust-s3/pull/299 - Remove superfluous println by [@​zaninime](https://github.com/zaninime) in https://github.com/durch/rust-s3/pull/345 - Move to the smaller, cargo-team maintained `home` crate by [@​utkarshgupta137](https://github.com/utkarshgupta137) in https://github.com/durch/rust-s3/pull/338 - remove misleading comment from GCP example by [@​pascualex](https://github.com/pascualex) in https://github.com/durch/rust-s3/pull/356 - Add il-central-1 region by [@​ArielHorwitz](https://github.com/ArielHorwitz) in https://github.com/durch/rust-s3/pull/359 - Add feature-gate on Credentials::refresh by [@​olback](https://github.com/olback) in https://github.com/durch/rust-s3/pull/361 - Only send metadata on Initiate of multipart upload by [@​jcrossley3](https://github.com/jcrossley3) in https://github.com/durch/rust-s3/pull/353 - Fix put_object_stream sync for small files by [@​jsurkont](https://github.com/jsurkont) in https://github.com/durch/rust-s3/pull/357 - Rework presign_post to be correct by [@​urkle](https://github.com/urkle) in https://github.com/durch/rust-s3/pull/358 - Implement a new `ListBuckets` command and its consumer API. by [@​aalekhpatel07](https://github.com/aalekhpatel07) in https://github.com/durch/rust-s3/pull/348 - feat: re-expose 'http-credentials' from 's3' by [@​woshilapin](https://github.com/woshilapin) in https://github.com/durch/rust-s3/pull/364 - Fix Bytes version requirement by [@​Ten0](https://github.com/Ten0) in https://github.com/durch/rust-s3/pull/365 - Fix fail-on-err description by [@​randomairborne](https://github.com/randomairborne) in https://github.com/durch/rust-s3/pull/371 #### New Contributors - [@​theCapypara](https://github.com/theCapypara) made their first contribution in https://github.com/durch/rust-s3/pull/324 - [@​robinfriedli](https://github.com/robinfriedli) made their first contribution in https://github.com/durch/rust-s3/pull/314 - [@​zifeo](https://github.com/zifeo) made their first contribution in https://github.com/durch/rust-s3/pull/309 - [@​tomkarw](https://github.com/tomkarw) made their first contribution in https://github.com/durch/rust-s3/pull/299 - [@​zaninime](https://github.com/zaninime) made their first contribution in https://github.com/durch/rust-s3/pull/345 - [@​utkarshgupta137](https://github.com/utkarshgupta137) made their first contribution in https://github.com/durch/rust-s3/pull/338 - [@​pascualex](https://github.com/pascualex) made their first contribution in https://github.com/durch/rust-s3/pull/356 - [@​ArielHorwitz](https://github.com/ArielHorwitz) made their first contribution in https://github.com/durch/rust-s3/pull/359 - [@​olback](https://github.com/olback) made their first contribution in https://github.com/durch/rust-s3/pull/361 - [@​jcrossley3](https://github.com/jcrossley3) made their first contribution in https://github.com/durch/rust-s3/pull/353 - [@​jsurkont](https://github.com/jsurkont) made their first contribution in https://github.com/durch/rust-s3/pull/357 - [@​urkle](https://github.com/urkle) made their first contribution in https://github.com/durch/rust-s3/pull/358 - [@​aalekhpatel07](https://github.com/aalekhpatel07) made their first contribution in https://github.com/durch/rust-s3/pull/348 - [@​woshilapin](https://github.com/woshilapin) made their first contribution in https://github.com/durch/rust-s3/pull/364 - [@​randomairborne](https://github.com/randomairborne) made their first contribution in https://github.com/durch/rust-s3/pull/371 **Full Changelog**: https://github.com/durch/rust-s3/compare/v0.33.0...v0.34.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MDguMSIsInVwZGF0ZWRJblZlciI6IjM3LjQwOC4xIiwidGFyZ2V0QnJhbmNoIjoibWFzdGVyIiwibGFiZWxzIjpbXX0=--> Reviewed-on: #218
73 lines
2.0 KiB
Rust
73 lines
2.0 KiB
Rust
use crate::app_config::AppConfig;
|
|
use s3::error::S3Error;
|
|
use s3::{Bucket, BucketConfiguration};
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
enum BucketServiceError {
|
|
#[error("Failed to fetch bucket information!")]
|
|
FailedFetchBucketInfo,
|
|
}
|
|
|
|
/// Create S3 bucket if required
|
|
pub async fn create_bucket_if_required() -> anyhow::Result<()> {
|
|
if AppConfig::get().s3_skip_auto_create_bucket {
|
|
log::debug!("Skipping bucket existence check");
|
|
return Ok(());
|
|
}
|
|
|
|
let bucket = AppConfig::get().s3_bucket()?;
|
|
|
|
match bucket.location().await {
|
|
Ok(_) => {
|
|
log::debug!("The bucket already exists.");
|
|
return Ok(());
|
|
}
|
|
Err(S3Error::HttpFailWithBody(404, s)) if s.contains("<Code>NoSuchKey</Code>") => {
|
|
log::warn!("Failed to fetch bucket location, but it seems that bucket exists.");
|
|
return Ok(());
|
|
}
|
|
Err(S3Error::HttpFailWithBody(404, s)) if s.contains("<Code>NoSuchBucket</Code>") => {
|
|
log::warn!("The bucket does not seem to exists, trying to create it!")
|
|
}
|
|
Err(e) => {
|
|
log::error!("Got unexpected error when querying bucket info: {}", e);
|
|
return Err(BucketServiceError::FailedFetchBucketInfo.into());
|
|
}
|
|
}
|
|
|
|
Bucket::create_with_path_style(
|
|
&bucket.name,
|
|
bucket.region,
|
|
AppConfig::get().s3_credentials()?,
|
|
BucketConfiguration::private(),
|
|
)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Upload a new file to the bucket
|
|
pub async fn upload_file(path: &str, content: &[u8]) -> anyhow::Result<()> {
|
|
let bucket = AppConfig::get().s3_bucket()?;
|
|
|
|
bucket.put_object(path, content).await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Get a file
|
|
pub async fn get_file(path: &str) -> anyhow::Result<Vec<u8>> {
|
|
let bucket = AppConfig::get().s3_bucket()?;
|
|
|
|
Ok(bucket.get_object(path).await?.to_vec())
|
|
}
|
|
|
|
/// Delete a file, if it exists
|
|
pub async fn delete_file_if_exists(path: &str) -> anyhow::Result<()> {
|
|
let bucket = AppConfig::get().s3_bucket()?;
|
|
|
|
bucket.delete_object(path).await?;
|
|
|
|
Ok(())
|
|
}
|