Skip to content

Commit 59049a0

Browse files
Backport #87426 to 25.7: Ignore only not found errors for s3_plain_rewritable (and some other S3 code)
1 parent 7e68b82 commit 59049a0

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

src/Disks/ObjectStorages/S3/S3ObjectStorage.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,7 @@ void S3ObjectStorage::removeObjectsIfExist(const StoredObjects & objects)
347347
std::optional<ObjectMetadata> S3ObjectStorage::tryGetObjectMetadata(const std::string & path) const
348348
{
349349
auto settings_ptr = s3_settings.get();
350-
auto object_info = S3::getObjectInfo(
351-
*client.get(), uri.bucket, path, {}, /* with_metadata= */ true, /* throw_on_error= */ false);
350+
auto object_info = S3::getObjectInfoIfExists(*client.get(), uri.bucket, path, {}, /* with_metadata= */ true);
352351

353352
if (object_info.size == 0 && object_info.last_modification_time == 0 && object_info.metadata.empty())
354353
return {};

src/IO/S3/getObjectInfo.cpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,42 +72,56 @@ bool isNotFoundError(Aws::S3::S3Errors error)
7272
|| error == Aws::S3::S3Errors::NO_SUCH_BUCKET;
7373
}
7474

75+
ObjectInfo getObjectInfoIfExists(
76+
const S3::Client & client,
77+
const String & bucket,
78+
const String & key,
79+
const String & version_id,
80+
bool with_metadata)
81+
{
82+
Expect404ResponseScope scope; // 404 is not an error
83+
84+
auto [object_info, error] = tryGetObjectInfo(client, bucket, key, version_id, with_metadata);
85+
if (object_info)
86+
return *object_info;
87+
88+
if (isNotFoundError(error.GetErrorType()))
89+
return {};
90+
91+
throw S3Exception(
92+
error.GetErrorType(),
93+
"Failed to get object info: {}. HTTP response code: {}",
94+
error.GetMessage(),
95+
static_cast<size_t>(error.GetResponseCode()));
96+
}
97+
7598
ObjectInfo getObjectInfo(
7699
const S3::Client & client,
77100
const String & bucket,
78101
const String & key,
79102
const String & version_id,
80-
bool with_metadata,
81-
bool throw_on_error)
103+
bool with_metadata)
82104
{
83-
std::optional<Expect404ResponseScope> scope; // 404 is not an error
84-
if (!throw_on_error)
85-
scope.emplace();
105+
Expect404ResponseScope scope; // 404 is not an error
86106

87107
auto [object_info, error] = tryGetObjectInfo(client, bucket, key, version_id, with_metadata);
88108
if (object_info)
89-
{
90109
return *object_info;
91-
}
92-
if (throw_on_error)
93-
{
94-
throw S3Exception(
95-
error.GetErrorType(),
96-
"Failed to get object info: {}. HTTP response code: {}",
97-
error.GetMessage(),
98-
static_cast<size_t>(error.GetResponseCode()));
99-
}
100-
return {};
110+
111+
throw S3Exception(
112+
error.GetErrorType(),
113+
"Failed to get object info: {}. HTTP response code: {}",
114+
error.GetMessage(),
115+
static_cast<size_t>(error.GetResponseCode()));
101116
}
102117

103118
size_t getObjectSize(
104119
const S3::Client & client,
105120
const String & bucket,
106121
const String & key,
107-
const String & version_id,
108-
bool throw_on_error)
122+
const String & version_id)
109123
{
110-
return getObjectInfo(client, bucket, key, version_id, {}, throw_on_error).size;
124+
return getObjectInfo(client, bucket, key, version_id, /*with_metadata=*/ false).size;
111125
}
112126

113127
bool objectExists(

src/IO/S3/getObjectInfo.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,26 @@ struct ObjectInfo
2020
std::map<String, String> metadata = {}; /// Set only if getObjectInfo() is called with `with_metadata = true`.
2121
};
2222

23+
/// Ignore if object does not exist
24+
ObjectInfo getObjectInfoIfExists(
25+
const S3::Client & client,
26+
const String & bucket,
27+
const String & key,
28+
const String & version_id = {},
29+
bool with_metadata = false);
30+
2331
ObjectInfo getObjectInfo(
2432
const S3::Client & client,
2533
const String & bucket,
2634
const String & key,
2735
const String & version_id = {},
28-
bool with_metadata = false,
29-
bool throw_on_error = true);
36+
bool with_metadata = false);
3037

3138
size_t getObjectSize(
3239
const S3::Client & client,
3340
const String & bucket,
3441
const String & key,
35-
const String & version_id = {},
36-
bool throw_on_error = true);
42+
const String & version_id = {});
3743

3844
bool objectExists(
3945
const S3::Client & client,

0 commit comments

Comments
 (0)