Skip to content

Commit

Permalink
Add custom headers to put_object_single
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 9ea9c7e commit e3acafb
Show file tree
Hide file tree
Showing 3 changed files with 33 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 @@ -383,6 +383,8 @@ pub struct PutObjectSingleParams {
/// 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 PutObjectSingleParams {
Expand Down Expand Up @@ -414,6 +416,12 @@ impl PutObjectSingleParams {
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
}
}

/// A checksum used by the object client for integrity checks on uploads.
Expand Down
6 changes: 6 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 @@ -128,6 +128,12 @@ impl S3CrtClient {
.set_checksum_header(checksum)
.map_err(S3RequestError::construction_failure)?;
}
for (name, value) in &params.custom_headers {
message
.inner
.add_header(&Header::new(name, value))
.map_err(S3RequestError::construction_failure)?;
}

let body_input_stream =
InputStream::new_from_slice(&self.inner.allocator, slice).map_err(S3RequestError::CrtError)?;
Expand Down
19 changes: 19 additions & 0 deletions mountpoint-s3-client/tests/put_object_single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,22 @@ async fn test_put_object_sse(sse_type: Option<&str>, kms_key_id: Option<String>)
let put_object_result = test_put_object_single(&client, &bucket, &key, request_params.clone()).await;
check_sse(&bucket, &key, sse_type, &kms_key_id, put_object_result).await;
}

#[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 = PutObjectSingleParams::new().add_custom_header("Content-Type".to_owned(), content_type.to_owned());
client
.put_object_single(&bucket, &key, &params, b"{ \"key\": \"value\" }")
.await
.expect("put_object should succeed");

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 e3acafb

Please sign in to comment.