Repository: ClickHouse/ClickHouse
File: src/Storages/ObjectStorage/S3/Configuration.cpp (C++)

Code before:
    const auto & settings = context->getSettingsRef();
    return StorageObjectStorageQuerySettings{
        .truncate_on_insert = settings[Setting::s3_truncate_on_insert],
        .create_new_file_on_insert = settings[Setting::s3_create_new_file_on_insert],
        .schema_inference_use_cache = settings[Setting::schema_inference_use_cache_for_s3],
        .schema_inference_mode = settings[Setting::schema_inference_mode],
        .skip_empty_files = settings[Setting::s3_skip_empty_files],
        .list_object_keys_size = settings[Setting::s3_list_object_keys_size],
        .throw_on_zero_files_match = settings[Setting::s3_throw_on_zero_files_match],
        .ignore_non_existent_file = settings[Setting::s3_ignore_file_doesnt_exist],
    };
}

ObjectStoragePtr StorageS3Configuration::createObjectStorage(ContextPtr context, bool /* is_readonly */, CredentialsConfigurationCallback refresh_credentials_callback, const StorageID & storage_id) /// NOLINT
{
    assertInitialized();

    if (!headers_from_ast.empty())
    {
        s3_settings->auth_settings.headers.insert(
            s3_settings->auth_settings.headers.end(),
            headers_from_ast.begin(), headers_from_ast.end());
    }

    auto client = getClient(url, *s3_settings, context, /* for_disk_s3 */false);
    auto client_refresher = [refresh_credentials_callback, this, context] (const DB::StorageID & sid)
    {
        auto new_client = getClient(url, *s3_settings, context, /* for_disk_s3 */false, /*opt_disk_name*/ {}, refresh_credentials_callback, sid);
        return std::unique_ptr<const S3::Client>(std::move(new_client));
    };
    return std::make_shared<S3ObjectStorage>(
        std::move(client),
        std::make_unique<S3Settings>(*s3_settings),
        url,
        *s3_capabilities,
        /*key_generator=*/nullptr,
        "StorageS3",
        false,
        client_refresher,
        storage_id);
}

void S3StorageParsedArguments::fromNamedCollection(const NamedCollection & collection, ContextPtr context)
{
    const auto & settings = context->getSettingsRef();
    validateNamedCollection(collection, required_configuration_keys, optional_configuration_keys);

    auto filename = collection.getOrDefault<String>("filename", "");
    if (!filename.empty())
        url = S3::URI(
            std::filesystem::path(collection.get<String>("url")) / filename,

Review comment (refactor):
Can we add storage_id to the function itself? If you don't have places in the code where storage_id is got later than callback, I guess you can (and this will simplify the code a lot)

Code after:
    const auto & settings = context->getSettingsRef();
    return StorageObjectStorageQuerySettings{
        .truncate_on_insert = settings[Setting::s3_truncate_on_insert],
        .create_new_file_on_insert = settings[Setting::s3_create_new_file_on_insert],
        .schema_inference_use_cache = settings[Setting::schema_inference_use_cache_for_s3],
        .schema_inference_mode = settings[Setting::schema_inference_mode],
        .skip_empty_files = settings[Setting::s3_skip_empty_files],
        .list_object_keys_size = settings[Setting::s3_list_object_keys_size],
        .throw_on_zero_files_match = settings[Setting::s3_throw_on_zero_files_match],
        .ignore_non_existent_file = settings[Setting::s3_ignore_file_doesnt_exist],
    };
}

ObjectStoragePtr StorageS3Configuration::createObjectStorage(ContextPtr context, bool /* is_readonly */, CredentialsConfigurationCallback refresh_credentials_callback) /// NOLINT
{
    assertInitialized();

    if (!headers_from_ast.empty())
    {
        s3_settings->auth_settings.headers.insert(
            s3_settings->auth_settings.headers.end(),
            headers_from_ast.begin(), headers_from_ast.end());
    }

    auto client = getClient(url, *s3_settings, context, /* for_disk_s3 */false);

    auto client_refresher = [refresh_credentials_callback, this, context_ = Context::createCopy(context)] () -> std::unique_ptr<S3::Client>
    {
        if (!refresh_credentials_callback)
            return nullptr;
        auto new_client = getClient(url, *s3_settings, context_, /* for_disk_s3 */false, /*opt_disk_name*/ {}, refresh_credentials_callback);
        return new_client;
    };
    return std::make_shared<S3ObjectStorage>(
        std::move(client),
        std::make_unique<S3Settings>(*s3_settings),
        url,
        *s3_capabilities,
        /*key_generator=*/nullptr,
        "StorageS3",
        false,
        client_refresher);
}

void S3StorageParsedArguments::fromNamedCollection(const NamedCollection & collection, ContextPtr context)
{
    const auto & settings = context->getSettingsRef();
    validateNamedCollection(collection, required_configuration_keys, optional_configuration_keys);

    auto filename = collection.getOrDefault<String>("filename", "");
    if (!filename.empty())