chore: migrate to object_store
Move to the more modern and maintained object_store library to access generic buckets. We should be able also to do streaming of files now, and proceed by implementing paginated results for listings if we want. Fixes #1.
This commit is contained in:
parent
996be0f6df
commit
5d6648ecba
5 changed files with 381 additions and 453 deletions
|
@ -1,16 +1,20 @@
|
|||
// SPDX-FileCopyrightText: © Matteo Settenvini <matteo.settenvini@montecristosoftware.eu>
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
use {anyhow::anyhow, rocket::serde::Deserialize, serde::de::Error};
|
||||
use {
|
||||
object_store::{ObjectStore, aws},
|
||||
rocket::serde::Deserialize,
|
||||
serde::de::Error,
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
pub struct Settings {
|
||||
#[serde(deserialize_with = "deserialize_s3_bucket")]
|
||||
pub s3_bucket: Box<s3::Bucket>,
|
||||
pub s3_bucket: Box<dyn ObjectStore>,
|
||||
}
|
||||
|
||||
fn deserialize_s3_bucket<'de, D>(deserializer: D) -> Result<Box<s3::Bucket>, D::Error>
|
||||
fn deserialize_s3_bucket<'de, D>(deserializer: D) -> Result<Box<dyn ObjectStore>, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
|
@ -31,37 +35,26 @@ pub struct S3Config {
|
|||
pub secret_access_key: String,
|
||||
}
|
||||
|
||||
impl TryInto<Box<s3::Bucket>> for S3Config {
|
||||
impl TryInto<Box<dyn ObjectStore>> for S3Config {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_into(self) -> Result<Box<s3::Bucket>, Self::Error> {
|
||||
let region = s3::Region::Custom {
|
||||
region: self.region,
|
||||
endpoint: self.endpoint,
|
||||
};
|
||||
|
||||
let credentials = s3::creds::Credentials::new(
|
||||
Some(&self.access_key_id),
|
||||
Some(&self.secret_access_key),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
)?;
|
||||
fn try_into(self) -> Result<Box<dyn ObjectStore>, Self::Error> {
|
||||
// TODO: support object stores other than than AWS
|
||||
let object_store = aws::AmazonS3Builder::new()
|
||||
.with_region(self.region)
|
||||
.with_endpoint(&self.endpoint)
|
||||
.with_bucket_name(&self.name)
|
||||
.with_access_key_id(self.access_key_id)
|
||||
.with_secret_access_key(self.secret_access_key)
|
||||
.with_virtual_hosted_style_request(!self.path_style)
|
||||
.build()?;
|
||||
|
||||
log::info!(
|
||||
"Serving contents from bucket {} at {}",
|
||||
&self.name,
|
||||
region.endpoint()
|
||||
self.endpoint,
|
||||
self.name,
|
||||
);
|
||||
|
||||
let bucket = s3::Bucket::new(&self.name, region, credentials).map_err(|e| anyhow!(e));
|
||||
if self.path_style {
|
||||
bucket.map(|mut b| {
|
||||
b.set_path_style();
|
||||
b
|
||||
})
|
||||
} else {
|
||||
bucket
|
||||
}
|
||||
Ok(Box::new(object_store))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue