Support deleting multiple R2 keys, fixes #780 (#781)

This commit is contained in:
Luke Valenta 2025-07-16 19:09:47 -04:00 committed by GitHub
parent 3f0db65e0f
commit dd0e6363d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 1 deletions

View File

@ -257,6 +257,14 @@ pub async fn delete(_req: Request, env: Env, _data: SomeSharedData) -> Result<Re
bucket.delete("key").await?;
let keys: Vec<String> = (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);

View File

@ -23,6 +23,10 @@ extern "C" {
#[wasm_bindgen(method, catch)]
pub fn delete(this: &R2Bucket, key: String) -> Result<js_sys::Promise, JsValue>;
#[wasm_bindgen(method, catch, js_name=delete)]
pub fn delete_multiple(this: &R2Bucket, keys: Vec<JsValue>)
-> Result<js_sys::Promise, JsValue>;
#[wasm_bindgen(method, catch)]
pub fn list(this: &R2Bucket, options: JsValue) -> Result<js_sys::Promise, JsValue>;

View File

@ -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<impl Deref<Target = str>>) -> 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 {