Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions worker-sandbox/src/r2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions worker-sys/src/types/r2/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down
19 changes: 18 additions & 1 deletion worker/src/r2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, convert::TryInto};
use std::{collections::HashMap, convert::TryInto, ops::Deref};

pub use builder::*;

Expand Down Expand Up @@ -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 {
Expand Down