r2kit 0.2.1

A safe, ergonomic Rust toolkit for Cloudflare R2 object storage.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
use std::{
    borrow::{Borrow, Cow},
    fmt,
    ops::Deref,
    str::FromStr,
    time::Duration,
};

use crate::{Error, ValidationError};

pub(crate) const MAX_KEY_BYTES: usize = 1_024;
pub(crate) const MIN_BUCKET_NAME_LEN: usize = 3;
pub(crate) const MAX_BUCKET_NAME_LEN: usize = 63;

// R2's platform limit is 5 MiB below 5 GiB for a single request/part.
// https://developers.cloudflare.com/r2/platform/limits/
pub(crate) const MIN_MULTIPART_PART_SIZE: u64 = 5 * 1024 * 1024;
pub(crate) const MAX_UPLOAD_SIZE: u64 = 5 * 1024 * 1024 * 1024 - MIN_MULTIPART_PART_SIZE;
pub(crate) const MAX_MULTIPART_PART_SIZE: u64 = MAX_UPLOAD_SIZE;
pub(crate) const MAX_MULTIPART_OBJECT_SIZE: u64 =
    5 * 1024 * 1024 * 1024 * 1024 - 5 * 1024 * 1024 * 1024;
pub(crate) const MAX_PRESIGN_SECONDS: u64 = 7 * 24 * 60 * 60;

pub(crate) const fn mebibytes(value: u64) -> u64 {
    match value.checked_mul(1024 * 1024) {
        Some(bytes) => bytes,
        None => u64::MAX,
    }
}

pub(crate) fn validate_prefix(prefix: &str) -> Result<(), Error> {
    if prefix.len() > MAX_KEY_BYTES {
        return Err(Error::InvalidInput {
            field: "prefix",
            reason: "must not exceed 1,024 UTF-8 bytes",
        });
    }
    Ok(())
}

pub(crate) fn validate_expiry(expires_in: Duration) -> Result<(), Error> {
    if expires_in < Duration::from_secs(1) || expires_in > Duration::from_secs(MAX_PRESIGN_SECONDS)
    {
        return Err(ValidationError::PresignExpiryOutOfRange {
            provided: expires_in,
            min: Duration::from_secs(1),
            max: Duration::from_secs(MAX_PRESIGN_SECONDS),
        }
        .into());
    }
    Ok(())
}

pub(crate) fn validate_part_size(part_size: u64) -> Result<(), Error> {
    if !(MIN_MULTIPART_PART_SIZE..=MAX_MULTIPART_PART_SIZE).contains(&part_size) {
        return Err(ValidationError::PartSizeOutOfRange {
            provided: part_size,
            min: MIN_MULTIPART_PART_SIZE,
            max: MAX_MULTIPART_PART_SIZE,
        }
        .into());
    }
    Ok(())
}

/// A validated Cloudflare R2 object key.
///
/// An object key must contain between 1 and 1,024 UTF-8 bytes.
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(try_from = "String", into = "String"))]
pub struct ObjectKey(String);

impl ObjectKey {
    /// Creates and validates a new [`ObjectKey`].
    pub fn new(key: impl Into<String>) -> Result<Self, Error> {
        let key = key.into();
        validate_key_str(&key)?;
        Ok(Self(key))
    }

    /// Returns the object key as a string slice.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Consumes the [`ObjectKey`], returning the inner [`String`].
    #[must_use]
    pub fn into_inner(self) -> String {
        self.0
    }
}

fn validate_key_str(key: &str) -> Result<(), Error> {
    if key.is_empty() || key.len() > MAX_KEY_BYTES {
        return Err(Error::InvalidInput {
            field: "key",
            reason: "must contain between 1 and 1,024 UTF-8 bytes",
        });
    }
    Ok(())
}

impl Deref for ObjectKey {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<str> for ObjectKey {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl Borrow<str> for ObjectKey {
    fn borrow(&self) -> &str {
        &self.0
    }
}

impl fmt::Debug for ObjectKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("ObjectKey").field(&self.0).finish()
    }
}

impl fmt::Display for ObjectKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl TryFrom<&str> for ObjectKey {
    type Error = Error;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl TryFrom<String> for ObjectKey {
    type Error = Error;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl FromStr for ObjectKey {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::new(s)
    }
}

impl From<ObjectKey> for String {
    fn from(key: ObjectKey) -> Self {
        key.0
    }
}

impl From<&ObjectKey> for String {
    fn from(key: &ObjectKey) -> Self {
        key.0.clone()
    }
}

/// A validated Cloudflare R2 bucket name.
///
/// A bucket name must contain between 3 and 63 bytes, using lowercase ASCII letters,
/// digits, or interior hyphens (cannot start or end with a hyphen).
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(try_from = "String", into = "String"))]
pub struct BucketName(String);

impl BucketName {
    /// Creates and validates a new [`BucketName`].
    pub fn new(name: impl Into<String>) -> Result<Self, Error> {
        let name = name.into();
        validate_bucket_name_str(&name)?;
        Ok(Self(name))
    }

    /// Returns the bucket name as a string slice.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Consumes the [`BucketName`], returning the inner [`String`].
    #[must_use]
    pub fn into_inner(self) -> String {
        self.0
    }
}

fn validate_bucket_name_str(name: &str) -> Result<(), Error> {
    if name.len() < MIN_BUCKET_NAME_LEN || name.len() > MAX_BUCKET_NAME_LEN {
        return Err(Error::InvalidInput {
            field: "bucket",
            reason: "must contain between 3 and 63 bytes",
        });
    }
    if !name
        .bytes()
        .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
        || name.starts_with('-')
        || name.ends_with('-')
    {
        return Err(Error::InvalidInput {
            field: "bucket",
            reason: "must use lowercase ASCII letters, digits, or interior hyphens",
        });
    }
    Ok(())
}

impl Deref for BucketName {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<str> for BucketName {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl Borrow<str> for BucketName {
    fn borrow(&self) -> &str {
        &self.0
    }
}

impl fmt::Debug for BucketName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("BucketName").field(&self.0).finish()
    }
}

impl fmt::Display for BucketName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl TryFrom<&str> for BucketName {
    type Error = Error;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl TryFrom<String> for BucketName {
    type Error = Error;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl FromStr for BucketName {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::new(s)
    }
}

impl From<BucketName> for String {
    fn from(name: BucketName) -> Self {
        name.0
    }
}

/// A validated threshold for selecting single PUT vs multipart upload.
///
/// Must be at least [`UploadThreshold::MIN_BYTES`] (5 MiB).
/// Defaults to [`UploadThreshold::DEFAULT_BYTES`] (8 MiB).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(try_from = "u64", into = "u64"))]
pub struct UploadThreshold(u64);

impl UploadThreshold {
    /// Minimum upload threshold in bytes (5 MiB), matching R2's minimum multipart part size.
    pub const MIN_BYTES: u64 = 5 * 1024 * 1024;
    /// Default upload threshold in bytes (8 MiB).
    pub const DEFAULT_BYTES: u64 = 8 * 1024 * 1024;

    /// Creates and validates a new [`UploadThreshold`].
    ///
    /// # Errors
    /// Returns [`ValidationError::PartSizeOutOfRange`] if `bytes` is less than [`Self::MIN_BYTES`]
    /// or exceeds the maximum multipart object limit.
    pub fn new(bytes: u64) -> Result<Self, ValidationError> {
        if !(Self::MIN_BYTES..=MAX_MULTIPART_OBJECT_SIZE).contains(&bytes) {
            return Err(ValidationError::PartSizeOutOfRange {
                provided: bytes,
                min: Self::MIN_BYTES,
                max: MAX_MULTIPART_OBJECT_SIZE,
            });
        }
        Ok(Self(bytes))
    }

    /// Returns the threshold value in bytes.
    #[must_use]
    pub const fn get(self) -> u64 {
        self.0
    }
}

impl Default for UploadThreshold {
    fn default() -> Self {
        Self(Self::DEFAULT_BYTES)
    }
}

impl fmt::Display for UploadThreshold {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl TryFrom<u64> for UploadThreshold {
    type Error = ValidationError;

    fn try_from(value: u64) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl From<UploadThreshold> for u64 {
    fn from(threshold: UploadThreshold) -> Self {
        threshold.get()
    }
}

impl From<&BucketName> for String {
    fn from(name: &BucketName) -> Self {
        name.0.clone()
    }
}

/// Types that can be converted into a validated [`ObjectKey`].
pub trait IntoObjectKey {
    /// Attempts conversion into a validated [`ObjectKey`].
    fn into_object_key(self) -> Result<ObjectKey, Error>;
}

impl IntoObjectKey for ObjectKey {
    fn into_object_key(self) -> Result<ObjectKey, Error> {
        Ok(self)
    }
}

impl IntoObjectKey for &ObjectKey {
    fn into_object_key(self) -> Result<ObjectKey, Error> {
        Ok(self.clone())
    }
}

impl IntoObjectKey for &str {
    fn into_object_key(self) -> Result<ObjectKey, Error> {
        ObjectKey::new(self)
    }
}

impl IntoObjectKey for String {
    fn into_object_key(self) -> Result<ObjectKey, Error> {
        ObjectKey::new(self)
    }
}

impl IntoObjectKey for &String {
    fn into_object_key(self) -> Result<ObjectKey, Error> {
        ObjectKey::new(self.as_str())
    }
}

impl IntoObjectKey for Cow<'_, str> {
    fn into_object_key(self) -> Result<ObjectKey, Error> {
        ObjectKey::new(self.into_owned())
    }
}

/// Types that can be converted into a validated [`BucketName`].
pub trait IntoBucketName {
    /// Attempts conversion into a validated [`BucketName`].
    fn into_bucket_name(self) -> Result<BucketName, Error>;
}

impl IntoBucketName for BucketName {
    fn into_bucket_name(self) -> Result<BucketName, Error> {
        Ok(self)
    }
}

impl IntoBucketName for &BucketName {
    fn into_bucket_name(self) -> Result<BucketName, Error> {
        Ok(self.clone())
    }
}

impl IntoBucketName for &str {
    fn into_bucket_name(self) -> Result<BucketName, Error> {
        BucketName::new(self)
    }
}

impl IntoBucketName for String {
    fn into_bucket_name(self) -> Result<BucketName, Error> {
        BucketName::new(self)
    }
}

impl IntoBucketName for &String {
    fn into_bucket_name(self) -> Result<BucketName, Error> {
        BucketName::new(self.as_str())
    }
}

impl IntoBucketName for Cow<'_, str> {
    fn into_bucket_name(self) -> Result<BucketName, Error> {
        BucketName::new(self.into_owned())
    }
}

/// Types that can be converted into a validated MIME media type.
pub trait IntoContentType {
    /// Attempts conversion into a validated [`mime::Mime`].
    fn into_content_type(self) -> Result<mime::Mime, Error>;
}

impl IntoContentType for mime::Mime {
    fn into_content_type(self) -> Result<mime::Mime, Error> {
        Ok(self)
    }
}

impl IntoContentType for &mime::Mime {
    fn into_content_type(self) -> Result<mime::Mime, Error> {
        Ok(self.clone())
    }
}

impl IntoContentType for &str {
    fn into_content_type(self) -> Result<mime::Mime, Error> {
        self.parse::<mime::Mime>().map_err(|_| Error::InvalidInput {
            field: "content_type",
            reason: "must be a valid MIME media type",
        })
    }
}

impl IntoContentType for String {
    fn into_content_type(self) -> Result<mime::Mime, Error> {
        self.as_str().into_content_type()
    }
}

impl IntoContentType for &String {
    fn into_content_type(self) -> Result<mime::Mime, Error> {
        self.as_str().into_content_type()
    }
}

impl IntoContentType for Cow<'_, str> {
    fn into_content_type(self) -> Result<mime::Mime, Error> {
        self.as_ref().into_content_type()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn object_key_validates_length_bounds() {
        assert!(ObjectKey::new("").is_err());
        assert!(ObjectKey::new("a").is_ok());
        assert!(ObjectKey::new("a".repeat(1_024)).is_ok());
        assert!(ObjectKey::new("a".repeat(1_025)).is_err());
    }

    #[test]
    fn bucket_name_validates_rfc1035_and_r2_invariants() {
        assert!(BucketName::new("ab").is_err());
        assert!(BucketName::new("abc").is_ok());
        assert!(BucketName::new("my-bucket-123").is_ok());
        assert!(BucketName::new("-starts-with-hyphen").is_err());
        assert!(BucketName::new("ends-with-hyphen-").is_err());
        assert!(BucketName::new("UPPERCASE").is_err());
        assert!(BucketName::new("a".repeat(63)).is_ok());
        assert!(BucketName::new("a".repeat(64)).is_err());
    }

    #[test]
    fn into_traits_convert_smoothly() {
        let key_str = "hello.txt";
        let key = key_str.into_object_key().unwrap();
        assert_eq!(key.as_str(), "hello.txt");
        assert_eq!((&key).into_object_key().unwrap(), key);

        let bucket_str = "my-bucket";
        let b = bucket_str.into_bucket_name().unwrap();
        assert_eq!(b.as_str(), "my-bucket");
        assert_eq!((&b).into_bucket_name().unwrap(), b);
    }
}