Skip to content

Commit 233b23a

Browse files
authored
Add impl<T: Signed> Signed for PrefixStore<T> (#739)
Small convenience feature
1 parent a9a83f1 commit 233b23a

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

src/prefix.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use std::ops::Range;
2222

2323
use crate::multipart::{MultipartStore, PartId};
2424
use crate::path::Path;
25+
#[cfg(feature = "cloud")]
26+
use crate::signer::Signer;
2527
use crate::{
2628
CopyOptions, GetOptions, GetResult, ListResult, MultipartId, MultipartUpload, ObjectMeta,
2729
ObjectStore, PutMultipartOptions, PutOptions, PutPayload, PutResult, RenameOptions, Result,
@@ -234,6 +236,36 @@ impl<T: MultipartStore> MultipartStore for PrefixStore<T> {
234236
}
235237
}
236238

239+
#[cfg(feature = "cloud")]
240+
#[async_trait::async_trait]
241+
impl<T: Signer> Signer for PrefixStore<T> {
242+
async fn signed_url(
243+
&self,
244+
method: http::Method,
245+
path: &Path,
246+
expires_in: std::time::Duration,
247+
) -> Result<url::Url> {
248+
self.inner
249+
.signed_url(method, &self.full_path(path), expires_in)
250+
.await
251+
}
252+
253+
async fn signed_urls(
254+
&self,
255+
method: http::Method,
256+
paths: &[Path],
257+
expires_in: std::time::Duration,
258+
) -> Result<Vec<url::Url>> {
259+
self.inner
260+
.signed_urls(
261+
method,
262+
&paths.iter().map(|p| self.full_path(p)).collect::<Vec<_>>(),
263+
expires_in,
264+
)
265+
.await
266+
}
267+
}
268+
237269
#[cfg(not(target_arch = "wasm32"))]
238270
#[cfg(test)]
239271
mod tests {
@@ -363,4 +395,46 @@ mod tests {
363395
multipart_out_of_order(&store).await;
364396
multipart_race_condition(&store, true).await;
365397
}
398+
399+
#[cfg(feature = "cloud")]
400+
#[tokio::test]
401+
async fn signer() {
402+
#[derive(Debug)]
403+
struct Foo;
404+
405+
#[async_trait::async_trait]
406+
impl Signer for Foo {
407+
async fn signed_url(
408+
&self,
409+
method: http::Method,
410+
path: &Path,
411+
_expires_in: std::time::Duration,
412+
) -> Result<url::Url> {
413+
Ok(url::Url::parse(&format!("ex:{path}?method={method}")).unwrap())
414+
}
415+
}
416+
417+
assert_eq!(
418+
PrefixStore::new(Foo, "prefix")
419+
.signed_url(
420+
http::Method::GET,
421+
&"foo".into(),
422+
std::time::Duration::from_secs(1)
423+
)
424+
.await
425+
.unwrap(),
426+
url::Url::parse("ex:prefix/foo?method=GET").unwrap()
427+
);
428+
assert_eq!(
429+
PrefixStore::new(Foo, "prefix")
430+
.signed_urls(
431+
http::Method::GET,
432+
&["foo".into()],
433+
std::time::Duration::from_secs(1)
434+
)
435+
.await
436+
.unwrap(),
437+
vec![url::Url::parse("ex:prefix/foo?method=GET").unwrap()]
438+
);
439+
}
366440
}

0 commit comments

Comments
 (0)