Skip to content

Commit

Permalink
Add custom headers to put_object
Browse files Browse the repository at this point in the history
Signed-off-by: Alessandro Passaro <[email protected]>
  • Loading branch information
passaro committed Oct 14, 2024
1 parent e3acafb commit d1f1e2a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mountpoint-s3-client/src/object_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ pub struct PutObjectParams {
/// If `server_side_encryption` has a valid value of aws:kms or aws:kms:dsse, this value may be used to specify AWS KMS key ID to be used
/// when creating new S3 object
pub ssekms_key_id: Option<String>,
/// Custom headers to add to the request
pub custom_headers: Vec<(String, String)>,
}

impl PutObjectParams {
Expand Down Expand Up @@ -347,6 +349,12 @@ impl PutObjectParams {
self.ssekms_key_id = value;
self
}

/// Add a custom header to the request.
pub fn add_custom_header(mut self, name: String, value: String) -> Self {
self.custom_headers.push((name, value));
self
}
}

/// How CRC32c checksums are used for parts of a multi-part PutObject request
Expand Down
7 changes: 7 additions & 0 deletions mountpoint-s3-client/src/s3_crt_client/put_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ impl S3CrtClient {
};
message.set_checksum_config(checksum_config);

for (name, value) in &params.custom_headers {
message
.inner
.add_header(&Header::new(name, value))
.map_err(S3RequestError::construction_failure)?;
}

let review_callback = ReviewCallbackBox::default();
let callback = review_callback.clone();

Expand Down
27 changes: 27 additions & 0 deletions mountpoint-s3-client/tests/put_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,30 @@ async fn test_concurrent_put_objects(throughput_target_gbps: f64, max_concurrent
// Cancel all put_object requests.
drop(req_vec);
}

#[tokio::test]
async fn test_put_object_header() {
let (bucket, prefix) = get_test_bucket_and_prefix("test_put_object_header");
let client = get_test_client();
let key = format!("{prefix}hello");

let content_type = "application/json";
let params = PutObjectParams::new().add_custom_header("Content-Type".to_owned(), content_type.to_owned());
let mut request = client
.put_object(&bucket, &key, &params)
.await
.expect("put_object should succeed");
request
.write(b"{ \"key\": \"value\" }")
.await
.expect("write should succeed");
request
.complete()
.await
.expect("the upload should complete successfully");

let sdk_client = get_test_sdk_client().await;
let output = sdk_client.head_object().bucket(bucket).key(key).send().await.unwrap();

assert_eq!(Some(content_type), output.content_type());
}

0 comments on commit d1f1e2a

Please sign in to comment.