bucket client (but needs tokio runtime!)

This commit is contained in:
Brandon Thomas committed 2021-05-23 02:31:31 -04:00
1 parent a484207eb6
commit bf3c0c19da
7 files changed
+261 -132

No files matched your search

+106
View File
@@ -0,0 +1,106 @@
use anyhow::bail;
use log::info;
use s3::bucket::Bucket;
use s3::creds::Credentials;
use s3::region::Region;
use std::path::{PathBuf, Path};
use std::str::FromStr;
use std::fs::File;
use std::io::Read;
#[derive(Clone)]
pub struct BucketClient {
bucket: Bucket,
}
impl BucketClient {
pub fn create(
access_key: &str,
secret_key: &str,
region_name: &str,
bucket_name: &str) -> anyhow::Result<Self>
{
let credentials = Credentials::new(
Some(&access_key),
Some(&secret_key),
None,
None,
None,
)?;
// NB: The GCS buckets aren't supported by default.
let region = Region::Custom {
region: region_name.to_owned(),
endpoint: "https://storage.googleapis.com".to_owned(),
};
let bucket = Bucket::new(&bucket_name, region, credentials)?;
Ok(Self {
bucket,
})
}
pub async fn upload_file(&self, object_name: &str, bytes: &[u8]) -> anyhow::Result<()> {
info!("Filename for bucket: {}", object_name);
let (_, code) = self.bucket.put_object(object_name, bytes).await?;
info!("upload code: {}", code);
Ok(())
}
pub fn upload_file_blocking(&self, object_name: &str, bytes: &[u8]) -> anyhow::Result<()> {
info!("Filename for bucket: {}", object_name);
let (_, code) = self.bucket.put_object_blocking(object_name, bytes)?;
info!("upload code: {}", code);
Ok(())
}
pub async fn upload_filename(&self, object_name: &str, filename: &Path) -> anyhow::Result<()> {
// TODO: does a newer version of this crate handle streaming/buffering file contents?
let mut file = File::open(filename)?;
let mut buffer : Vec<u8> = Vec::new();
file.read_to_end(&mut buffer)?;
self.upload_file(object_name, &buffer).await
}
pub fn upload_filename_blocking(&self, object_name: &str, filename: &Path) -> anyhow::Result<()> {
// TODO: does a newer version of this crate handle streaming/buffering file contents?
let mut file = File::open(filename)?;
let mut buffer : Vec<u8> = Vec::new();
file.read_to_end(&mut buffer)?;
self.upload_file_blocking(object_name, &buffer)
}
pub async fn download_file(&self, path: &str) -> anyhow::Result<Vec<u8>> {
info!("downloading from bucket: {}", path);
let (bytes, code) = self.bucket.get_object(path).await?;
match code {
404 => bail!("File not found in bucket: {}", path),
_ => {},
}
info!("download code: {}", code);
Ok(bytes)
}
pub fn download_file_blocking(&self, path: &str) -> anyhow::Result<Vec<u8>> {
info!("downloading from bucket: {}", path);
let (bytes, code) = self.bucket.get_object_blocking(path)?;
match code {
404 => bail!("File not found in bucket: {}", path),
_ => {},
}
info!("download code: {}", code);
Ok(bytes)
}
}
+1
View File
@@ -1,4 +1,5 @@
pub mod anyhow_result;
pub mod bucket_client;
pub mod cookie_manager;
pub mod filesystem;
pub mod ip_address;