[pbs-devel] [RFC v2 proxmox-backup 09/42] s3 client: add dedicated type for s3 object keys
Christian Ebner
c.ebner at proxmox.com
Thu May 29 16:31:34 CEST 2025
S3 objects are uniquely identified within a bucket by their object
key [0].
Implements conversion and utility traits to easily convert and encode
a string or a chunk digest as corresponding object key for the S3
storage backend. Adds type checking for s3 client operations requiring
an object key.
[0] https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---
pbs-s3-client/src/lib.rs | 2 ++
pbs-s3-client/src/object_key.rs | 64 +++++++++++++++++++++++++++++++++
2 files changed, 66 insertions(+)
create mode 100644 pbs-s3-client/src/object_key.rs
diff --git a/pbs-s3-client/src/lib.rs b/pbs-s3-client/src/lib.rs
index 5a60b92ec..a4081df15 100644
--- a/pbs-s3-client/src/lib.rs
+++ b/pbs-s3-client/src/lib.rs
@@ -1,3 +1,5 @@
mod aws_sign_v4;
mod client;
pub use client::{S3Client, S3ClientOptions};
+mod object_key;
+pub use object_key::{S3ObjectKey, S3_CONTENT_PREFIX};
diff --git a/pbs-s3-client/src/object_key.rs b/pbs-s3-client/src/object_key.rs
new file mode 100644
index 000000000..362c0cd55
--- /dev/null
+++ b/pbs-s3-client/src/object_key.rs
@@ -0,0 +1,64 @@
+use crate::aws_sign_v4::aws_sign_v4_uri_encode;
+
+pub const S3_CONTENT_PREFIX: &str = ".content";
+
+#[derive(Clone)]
+pub struct S3ObjectKey {
+ object_key: String,
+}
+
+// All regular keys (non-digests) get prefixed by a `/.contents`, so that
+// content listing without all the chunks can be done by that prefix.
+impl core::convert::From<&str> for S3ObjectKey {
+ fn from(object_key: &str) -> Self {
+ let object_key = object_key.strip_prefix("/").unwrap_or(object_key);
+ let object_key = format!(
+ "/{S3_CONTENT_PREFIX}/{object_key}",
+ object_key = aws_sign_v4_uri_encode(object_key, true)
+ );
+
+ Self { object_key }
+ }
+}
+
+impl core::convert::From<&[u8; 32]> for S3ObjectKey {
+ fn from(digest: &[u8; 32]) -> Self {
+ // Use the same layout as on regular PBS datastores, including the 4 hex digit prefix
+ let object_key = hex::encode(digest);
+ let prefix = &object_key[..4];
+ Self {
+ object_key: format!("/.chunks/{prefix}/{object_key}"),
+ }
+ }
+}
+
+impl core::convert::From<[u8; 32]> for S3ObjectKey {
+ fn from(digest: [u8; 32]) -> Self {
+ Self::from(&digest)
+ }
+}
+
+impl std::ops::Deref for S3ObjectKey {
+ type Target = str;
+
+ fn deref(&self) -> &Self::Target {
+ &self.object_key
+ }
+}
+
+impl std::fmt::Display for S3ObjectKey {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{}", self.object_key)
+ }
+}
+
+impl S3ObjectKey {
+ /// Generate source key for copy object operations given the source bucket.
+ pub fn to_copy_source_key(&self, source_bucket: &str) -> Self {
+ Self {
+ // object key already contains the required separator slash in-between source bucket
+ // and source object key.
+ object_key: format!("{source_bucket}{}", self.object_key),
+ }
+ }
+}
--
2.39.5
More information about the pbs-devel
mailing list