Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(filemanager): make HeadObject optional #355

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ pub enum Error {
MissingEnvironmentVariable(String),
#[error("Invalid environment variable: `{0}`")]
InvalidEnvironmentVariable(String),
#[error("S3 error: `{0}`")]
S3Error(String),
#[error("credential generator error: `{0}`")]
CredentialGeneratorError(String),
#[error("S3 inventory error: `{0}`")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!

use async_trait::async_trait;
use aws_sdk_s3::operation::head_object::{HeadObjectError, HeadObjectOutput};
use aws_sdk_s3::operation::head_object::HeadObjectOutput;
use aws_sdk_s3::primitives;
use aws_sdk_s3::types::StorageClass::Standard;
use chrono::{DateTime, Utc};
Expand All @@ -16,8 +16,8 @@ use crate::clients::aws::s3::Client;
use crate::clients::aws::s3::Client as S3Client;
#[double]
use crate::clients::aws::sqs::Client as SQSClient;
use crate::error::Error::InvalidEnvironmentVariable;
use crate::error::Error::{DeserializeError, SQSError};
use crate::error::Error::{InvalidEnvironmentVariable, S3Error};
use crate::error::Result;
use crate::events::aws::{
EventType, FlatS3EventMessage, FlatS3EventMessages, StorageClass, TransposedS3EventMessages,
Expand Down Expand Up @@ -142,26 +142,8 @@ impl Collecter {
}

/// Gets S3 metadata from HeadObject such as creation/archival timestamps and statuses.
pub async fn head(
client: &Client,
key: &str,
bucket: &str,
) -> Result<Option<HeadObjectOutput>> {
let head = client.head_object(key, bucket).await;

match head {
Ok(head) => Ok(Some(head)),
Err(err) => {
let err = err.into_service_error();
if let HeadObjectError::NotFound(_) = err {
// Object not found, could be deleted.
Ok(None)
} else {
// I.e: Cannot connect to server
Err(S3Error(err.to_string()))
}
}
}
pub async fn head(client: &Client, key: &str, bucket: &str) -> Option<HeadObjectOutput> {
client.head_object(key, bucket).await.ok()
}

/// Process events and add header and datetime fields.
Expand All @@ -182,7 +164,7 @@ impl Collecter {
// Race condition: it's possible that an object gets deleted so quickly that it
// occurs before calling head. This means that there may be cases where the storage
// class and other fields are not known.
if let Some(head) = Self::head(client, &event.key, &event.bucket).await? {
if let Some(head) = Self::head(client, &event.key, &event.bucket).await {
trace!(head = ?head, "received head object output");

let HeadObjectOutput {
Expand Down Expand Up @@ -254,6 +236,7 @@ pub(crate) mod tests {
use std::result;

use aws_sdk_s3::error::SdkError;
use aws_sdk_s3::operation::head_object::HeadObjectError;
use aws_sdk_s3::primitives::{DateTimeFormat, SdkBody};
use aws_sdk_s3::types;
use aws_sdk_s3::types::error::NotFound;
Expand Down Expand Up @@ -331,9 +314,7 @@ pub(crate) mod tests {

set_s3_client_expectations(&mut collecter.client, vec![|| Ok(expected_head_object())]);

let result = Collecter::head(&collecter.client, "key", "bucket")
.await
.unwrap();
let result = Collecter::head(&collecter.client, "key", "bucket").await;
assert_eq!(result, Some(expected_head_object()));
}

Expand All @@ -346,9 +327,7 @@ pub(crate) mod tests {
vec![|| Err(expected_head_object_not_found())],
);

let result = Collecter::head(&collecter.client, "key", "bucket")
.await
.unwrap();
let result = Collecter::head(&collecter.client, "key", "bucket").await;
assert!(result.is_none());
}

Expand Down