Skip to content

Commit dd0e636

Browse files
authored
Support deleting multiple R2 keys, fixes #780 (#781)
1 parent 3f0db65 commit dd0e636

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

worker-sandbox/src/r2.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ pub async fn delete(_req: Request, env: Env, _data: SomeSharedData) -> Result<Re
257257

258258
bucket.delete("key").await?;
259259

260+
let keys: Vec<String> = (0..1000).map(|i| format!("key_{i}")).collect();
261+
for key in &keys {
262+
bucket.put(key, Data::Empty).execute().await?;
263+
}
264+
let objects = bucket.list().execute().await?;
265+
assert_eq!(objects.objects().len(), keys.len());
266+
267+
bucket.delete_multiple(keys).await?;
260268
let objects = bucket.list().execute().await?;
261269
assert_eq!(objects.objects().len(), 0);
262270

worker-sys/src/types/r2/bucket.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ extern "C" {
2323
#[wasm_bindgen(method, catch)]
2424
pub fn delete(this: &R2Bucket, key: String) -> Result<js_sys::Promise, JsValue>;
2525

26+
#[wasm_bindgen(method, catch, js_name=delete)]
27+
pub fn delete_multiple(this: &R2Bucket, keys: Vec<JsValue>)
28+
-> Result<js_sys::Promise, JsValue>;
29+
2630
#[wasm_bindgen(method, catch)]
2731
pub fn list(this: &R2Bucket, options: JsValue) -> Result<js_sys::Promise, JsValue>;
2832

worker/src/r2/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, convert::TryInto};
1+
use std::{collections::HashMap, convert::TryInto, ops::Deref};
22

33
pub use builder::*;
44

@@ -79,6 +79,23 @@ impl Bucket {
7979
Ok(())
8080
}
8181

82+
/// Deletes the given values and metadata under the associated keys. Once
83+
/// the delete succeeds, returns void.
84+
///
85+
/// R2 deletes are strongly consistent. Once the Promise resolves, all
86+
/// subsequent read operations will no longer see the provided key value
87+
/// pairs globally.
88+
///
89+
/// Up to 1000 keys may be deleted per call.
90+
pub async fn delete_multiple(&self, keys: Vec<impl Deref<Target = str>>) -> Result<()> {
91+
let fut: JsFuture = self
92+
.inner
93+
.delete_multiple(keys.into_iter().map(|key| JsValue::from(&*key)).collect())?
94+
.into();
95+
fut.await?;
96+
Ok(())
97+
}
98+
8299
/// Returns an [Objects] containing a list of [Objects]s contained within the bucket. By
83100
/// default, returns the first 1000 entries.
84101
pub fn list(&self) -> ListOptionsBuilder {

0 commit comments

Comments
 (0)