Release ObjectStore 0.0.9
This commit is contained in:
1 parent
31eb4d092e
commit
0cf5c11202
39 files changed
+4739
-1167
No files matched your search
Generated
+2339
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "lunarsky-objectstore-client"
|
||||
version = "0.0.1"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
description = "Small Rust integration layer for LunarSky ObjectStore"
|
||||
|
||||
[dependencies]
|
||||
aws-config = { version = "1", default-features = false, features = ["default-https-client", "rt-tokio"] }
|
||||
aws-credential-types = "1"
|
||||
aws-sdk-s3 = "1"
|
||||
aws-types = "1"
|
||||
tokio = { version = "1", features = ["fs", "io-util"] }
|
||||
url = "2"
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { version = "1", features = ["macros", "rt-multi-thread", "fs", "io-util"] }
|
||||
@@ -0,0 +1,27 @@
|
||||
# ObjectStore Rust client
|
||||
|
||||
This small adapter configures the official AWS SDK for Rust for ObjectStore's path-style S3 endpoint. It reuses the SDK's signing, connection pooling, and streaming body support. `put_file` streams a file and `get_to_writer` streams a response into any Tokio async writer. `put_bytes` accepts an existing buffer; `presigned_get_url` and `presigned_put_url` make short-lived links for trusted server code. The underlying SDK client is available through `sdk()` for listing, multipart uploads, and other supported S3 operations.
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
lunarsky-objectstore-client = { path = "../rust-client" }
|
||||
tokio = { version = "1", features = ["macros", "rt-multi-thread", "fs"] }
|
||||
```
|
||||
|
||||
```rust
|
||||
use lunarsky_objectstore_client::ObjectStoreClient;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let client = ObjectStoreClient::connect(
|
||||
"http://127.0.0.1:9000", "us-east-1",
|
||||
&std::env::var("S3_ACCESS_KEY")?, &std::env::var("S3_SECRET_KEY")?,
|
||||
).await?;
|
||||
client.put_file("objects", "example.bin", "example.bin").await?;
|
||||
let mut output = tokio::fs::File::create("downloaded.bin").await?;
|
||||
client.get_to_writer("objects", "example.bin", &mut output).await?;
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
|
||||
The constructor requires HTTPS except for loopback tests. Keep access keys on the server, never in browser code. The wrapper is untested against every AWS S3 option; inspect ObjectStore's supported-operation list before using SDK methods directly. This crate is not published yet.
|
||||
@@ -0,0 +1,27 @@
|
||||
use lunarsky_objectstore_client::ObjectStoreClient;
|
||||
use std::time::Duration;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() != 6 {
|
||||
return Err(
|
||||
"usage: presign ENDPOINT BUCKET KEY ACCESS_KEY get|put (set S3_SECRET_KEY)".into(),
|
||||
);
|
||||
}
|
||||
let client = ObjectStoreClient::connect(
|
||||
&args[1],
|
||||
"us-east-1",
|
||||
&args[4],
|
||||
&std::env::var("S3_SECRET_KEY")?,
|
||||
)
|
||||
.await?;
|
||||
let expiry = Duration::from_secs(60);
|
||||
let url = match args[5].as_str() {
|
||||
"get" => client.presigned_get_url(&args[2], &args[3], expiry).await?,
|
||||
"put" => client.presigned_put_url(&args[2], &args[3], expiry).await?,
|
||||
_ => return Err("method must be get or put".into()),
|
||||
};
|
||||
println!("{url}");
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
use lunarsky_objectstore_client::ObjectStoreClient;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() != 7 {
|
||||
return Err(
|
||||
"usage: smoke ENDPOINT BUCKET KEY INPUT OUTPUT ACCESS_KEY (set S3_SECRET_KEY)".into(),
|
||||
);
|
||||
}
|
||||
let client = ObjectStoreClient::connect(
|
||||
&args[1],
|
||||
"us-east-1",
|
||||
&args[6],
|
||||
&std::env::var("S3_SECRET_KEY")?,
|
||||
)
|
||||
.await?;
|
||||
client.put_file(&args[2], &args[3], &args[4]).await?;
|
||||
let mut file = tokio::fs::File::create(&args[5]).await?;
|
||||
client.get_to_writer(&args[2], &args[3], &mut file).await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
use aws_config::BehaviorVersion;
|
||||
use aws_credential_types::Credentials;
|
||||
use aws_sdk_s3::{operation, presigning::PresigningConfig, primitives::ByteStream, Client};
|
||||
use aws_types::region::Region;
|
||||
use std::{error::Error, path::Path, time::Duration};
|
||||
use tokio::io::{AsyncWrite, AsyncWriteExt};
|
||||
use url::Url;
|
||||
|
||||
pub type ClientResult<T> = Result<T, Box<dyn Error + Send + Sync>>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ObjectStoreClient {
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl ObjectStoreClient {
|
||||
pub async fn connect(
|
||||
endpoint: &str,
|
||||
region: &str,
|
||||
access_key: &str,
|
||||
secret_key: &str,
|
||||
) -> ClientResult<Self> {
|
||||
let parsed = Url::parse(endpoint)?;
|
||||
let host = parsed.host_str().ok_or("endpoint needs a host")?;
|
||||
let local = matches!(host, "localhost" | "127.0.0.1" | "::1");
|
||||
if parsed.scheme() != "https" && !(parsed.scheme() == "http" && local) {
|
||||
return Err("use HTTPS for a nonlocal ObjectStore endpoint".into());
|
||||
}
|
||||
if parsed.path() != "/"
|
||||
|| parsed.query().is_some()
|
||||
|| parsed.fragment().is_some()
|
||||
|| !parsed.username().is_empty()
|
||||
|| parsed.password().is_some()
|
||||
{
|
||||
return Err("endpoint must be an origin without path, query, or credentials".into());
|
||||
}
|
||||
let credentials = Credentials::new(access_key, secret_key, None, None, "objectstore");
|
||||
let shared = aws_config::defaults(BehaviorVersion::latest())
|
||||
.credentials_provider(credentials)
|
||||
.region(Region::new(region.to_owned()))
|
||||
.load()
|
||||
.await;
|
||||
let config = aws_sdk_s3::config::Builder::from(&shared)
|
||||
.endpoint_url(endpoint)
|
||||
.force_path_style(true)
|
||||
.build();
|
||||
Ok(Self {
|
||||
client: Client::from_conf(config),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn sdk(&self) -> &Client {
|
||||
&self.client
|
||||
}
|
||||
|
||||
pub async fn put_file(
|
||||
&self,
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
path: impl AsRef<Path>,
|
||||
) -> ClientResult<operation::put_object::PutObjectOutput> {
|
||||
let body = ByteStream::from_path(path.as_ref().to_path_buf()).await?;
|
||||
Ok(self
|
||||
.client
|
||||
.put_object()
|
||||
.bucket(bucket)
|
||||
.key(key)
|
||||
.body(body)
|
||||
.send()
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub async fn put_bytes(
|
||||
&self,
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
bytes: Vec<u8>,
|
||||
) -> ClientResult<operation::put_object::PutObjectOutput> {
|
||||
Ok(self
|
||||
.client
|
||||
.put_object()
|
||||
.bucket(bucket)
|
||||
.key(key)
|
||||
.body(ByteStream::from(bytes))
|
||||
.send()
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub async fn get_to_writer<W: AsyncWrite + Unpin>(
|
||||
&self,
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
writer: &mut W,
|
||||
) -> ClientResult<u64> {
|
||||
let response = self
|
||||
.client
|
||||
.get_object()
|
||||
.bucket(bucket)
|
||||
.key(key)
|
||||
.send()
|
||||
.await?;
|
||||
let mut reader = response.body.into_async_read();
|
||||
let bytes = tokio::io::copy(&mut reader, writer).await?;
|
||||
writer.flush().await?;
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
pub async fn delete(&self, bucket: &str, key: &str) -> ClientResult<()> {
|
||||
self.client
|
||||
.delete_object()
|
||||
.bucket(bucket)
|
||||
.key(key)
|
||||
.send()
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn presigned_get_url(
|
||||
&self,
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
expires_in: Duration,
|
||||
) -> ClientResult<String> {
|
||||
let request = self
|
||||
.client
|
||||
.get_object()
|
||||
.bucket(bucket)
|
||||
.key(key)
|
||||
.presigned(PresigningConfig::expires_in(expires_in)?)
|
||||
.await?;
|
||||
Ok(request.uri().to_string())
|
||||
}
|
||||
|
||||
pub async fn presigned_put_url(
|
||||
&self,
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
expires_in: Duration,
|
||||
) -> ClientResult<String> {
|
||||
let request = self
|
||||
.client
|
||||
.put_object()
|
||||
.bucket(bucket)
|
||||
.key(key)
|
||||
.presigned(PresigningConfig::expires_in(expires_in)?)
|
||||
.await?;
|
||||
Ok(request.uri().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ObjectStoreClient;
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_insecure_remote_endpoint() {
|
||||
assert!(
|
||||
ObjectStoreClient::connect("http://storage.example", "us-east-1", "key", "secret")
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_endpoint_with_path() {
|
||||
assert!(ObjectStoreClient::connect(
|
||||
"https://storage.example/bucket",
|
||||
"us-east-1",
|
||||
"key",
|
||||
"secret"
|
||||
)
|
||||
.await
|
||||
.is_err());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user