diff --git a/worker-sandbox/src/r2.rs b/worker-sandbox/src/r2.rs index a1c30592d..f55c3b8e4 100644 --- a/worker-sandbox/src/r2.rs +++ b/worker-sandbox/src/r2.rs @@ -257,6 +257,14 @@ pub async fn delete(_req: Request, env: Env, _data: SomeSharedData) -> Result = (0..1000).map(|i| format!("key_{i}")).collect(); + for key in &keys { + bucket.put(key, Data::Empty).execute().await?; + } + let objects = bucket.list().execute().await?; + assert_eq!(objects.objects().len(), keys.len()); + + bucket.delete_multiple(keys).await?; let objects = bucket.list().execute().await?; assert_eq!(objects.objects().len(), 0); diff --git a/worker-sys/src/types/r2/bucket.rs b/worker-sys/src/types/r2/bucket.rs index 1903b7f65..4cca61913 100644 --- a/worker-sys/src/types/r2/bucket.rs +++ b/worker-sys/src/types/r2/bucket.rs @@ -23,6 +23,10 @@ extern "C" { #[wasm_bindgen(method, catch)] pub fn delete(this: &R2Bucket, key: String) -> Result; + #[wasm_bindgen(method, catch, js_name=delete)] + pub fn delete_multiple(this: &R2Bucket, keys: Vec) + -> Result; + #[wasm_bindgen(method, catch)] pub fn list(this: &R2Bucket, options: JsValue) -> Result; diff --git a/worker/src/r2/mod.rs b/worker/src/r2/mod.rs index 368693759..d5f3dc1d7 100644 --- a/worker/src/r2/mod.rs +++ b/worker/src/r2/mod.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, convert::TryInto}; +use std::{collections::HashMap, convert::TryInto, ops::Deref}; pub use builder::*; @@ -79,6 +79,23 @@ impl Bucket { Ok(()) } + /// Deletes the given values and metadata under the associated keys. Once + /// the delete succeeds, returns void. + /// + /// R2 deletes are strongly consistent. Once the Promise resolves, all + /// subsequent read operations will no longer see the provided key value + /// pairs globally. + /// + /// Up to 1000 keys may be deleted per call. + pub async fn delete_multiple(&self, keys: Vec>) -> Result<()> { + let fut: JsFuture = self + .inner + .delete_multiple(keys.into_iter().map(|key| JsValue::from(&*key)).collect())? + .into(); + fut.await?; + Ok(()) + } + /// Returns an [Objects] containing a list of [Objects]s contained within the bucket. By /// default, returns the first 1000 entries. pub fn list(&self) -> ListOptionsBuilder {