1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//! S3 source configuration.
use faucet_core::DEFAULT_BATCH_SIZE;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
/// Format of files stored in S3.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum S3FileFormat {
/// Each line in the file is a separate JSON record.
#[default]
JsonLines,
/// The entire file is a JSON array of records.
JsonArray,
/// Each file becomes a single record with `"key"` and `"content"` fields.
RawText,
}
/// Configuration for the S3 source connector.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct S3SourceConfig {
/// S3 bucket name.
pub bucket: String,
/// Object key prefix filter.
pub prefix: Option<String>,
/// AWS region. `None` uses the SDK default.
pub region: Option<String>,
/// Custom endpoint URL for S3-compatible services (e.g. MinIO).
pub endpoint_url: Option<String>,
/// Format of the files to read.
pub file_format: S3FileFormat,
/// Maximum number of objects to read.
pub max_objects: Option<usize>,
/// Maximum number of concurrent object reads (default: 10).
pub concurrency: usize,
/// Records per emitted [`StreamPage`](faucet_core::StreamPage). For
/// `JsonLines` and `RawText` formats, the object body is decoded
/// line-by-line via [`tokio::io::AsyncBufReadExt`] and a page is yielded
/// whenever the buffer reaches this size; multi-object scans flatten so
/// a single page may contain lines from any object. For `JsonArray`,
/// each object is buffered fully before its records are chunked into
/// pages of this size (see the README "Streaming and batching" section
/// for the caveat). Defaults to [`DEFAULT_BATCH_SIZE`].
///
/// `batch_size = 0` is the "no batching" sentinel: every page is one
/// complete object — no within-object chunking. Useful for small
/// lookup files, or for sinks (e.g. SQL `COPY`, BigQuery load jobs)
/// that prefer one large request per file to many small ones.
#[serde(default = "default_batch_size")]
pub batch_size: usize,
/// Verify each object's byte length against the `Content-Length` the
/// store advertises, failing the read with [`FaucetError::Source`](faucet_core::FaucetError::Source)
/// on a short (truncated) or over-long transfer (#161). The check is
/// cheap (a byte counter over the body that is read anyway) and defaults
/// to `true`. Disable it only for an S3-compatible store that does not
/// return a reliable `Content-Length`. When the store reports no length,
/// the check is skipped (a debug log notes it) rather than failing.
#[serde(default = "default_true")]
pub verify_length: bool,
/// Verify each object's body against the checksum the store advertises —
/// an `x-amz-checksum-{crc32,crc32c,sha1,sha256}` header when present, or
/// the ETag as MD5 for a non-multipart upload (#161). Stronger than the
/// length check but costs a hash over the full body, so it defaults to
/// `false`. Enabling it sets `ChecksumMode::Enabled` on each `GetObject`
/// so the store returns its stored checksum. When the store advertises no
/// usable checksum for an object, verification is skipped for that object
/// (a debug log notes it); the length check still applies.
#[serde(default)]
pub verify_checksum: bool,
/// Compression codec applied to each downloaded object. Defaults to
/// [`CompressionConfig::Auto`](faucet_core::CompressionConfig::Auto) —
/// the codec is resolved per-object-key, so a single source can read a
/// mix of compressed and uncompressed objects. Requires the
/// crate-local `compression` feature.
#[cfg(feature = "compression")]
#[serde(default)]
pub compression: faucet_core::CompressionConfig,
}
fn default_batch_size() -> usize {
DEFAULT_BATCH_SIZE
}
fn default_true() -> bool {
true
}
impl S3SourceConfig {
/// Create a new config with the required bucket name and sensible defaults.
pub fn new(bucket: impl Into<String>) -> Self {
Self {
bucket: bucket.into(),
prefix: None,
region: None,
endpoint_url: None,
file_format: S3FileFormat::default(),
max_objects: None,
concurrency: 10,
batch_size: DEFAULT_BATCH_SIZE,
verify_length: true,
verify_checksum: false,
#[cfg(feature = "compression")]
compression: faucet_core::CompressionConfig::default(),
}
}
/// Set the object key prefix filter.
pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
self.prefix = Some(prefix.into());
self
}
/// Set the AWS region.
pub fn region(mut self, region: impl Into<String>) -> Self {
self.region = Some(region.into());
self
}
/// Set a custom endpoint URL for S3-compatible services.
pub fn endpoint_url(mut self, url: impl Into<String>) -> Self {
self.endpoint_url = Some(url.into());
self
}
/// Set the file format.
pub fn file_format(mut self, format: S3FileFormat) -> Self {
self.file_format = format;
self
}
/// Set the maximum number of objects to read.
pub fn max_objects(mut self, max: usize) -> Self {
self.max_objects = Some(max);
self
}
/// Set the maximum number of concurrent object reads.
pub fn concurrency(mut self, concurrency: usize) -> Self {
self.concurrency = concurrency;
self
}
/// Set the per-page record count for [`Source::stream_pages`](faucet_core::Source::stream_pages).
///
/// Pass `0` to opt out of within-object chunking — every emitted
/// [`StreamPage`](faucet_core::StreamPage) corresponds to exactly one
/// S3 object.
pub fn with_batch_size(mut self, batch_size: usize) -> Self {
self.batch_size = batch_size;
self
}
/// Enable or disable the per-object `Content-Length` verification
/// (default `true`). See [`verify_length`](Self::verify_length).
pub fn verify_length(mut self, verify: bool) -> Self {
self.verify_length = verify;
self
}
/// Enable or disable per-object checksum verification (default `false`).
/// See [`verify_checksum`](Self::verify_checksum).
pub fn verify_checksum(mut self, verify: bool) -> Self {
self.verify_checksum = verify;
self
}
/// Set the compression codec. Available only with the `compression` feature.
#[cfg(feature = "compression")]
pub fn compression(mut self, c: faucet_core::CompressionConfig) -> Self {
self.compression = c;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_config() {
let config = S3SourceConfig::new("my-bucket");
assert_eq!(config.bucket, "my-bucket");
assert!(config.prefix.is_none());
assert!(config.region.is_none());
assert!(config.endpoint_url.is_none());
assert!(matches!(config.file_format, S3FileFormat::JsonLines));
assert!(config.max_objects.is_none());
}
#[test]
fn builder_methods() {
let config = S3SourceConfig::new("my-bucket")
.prefix("data/")
.region("us-west-2")
.endpoint_url("http://localhost:9000")
.file_format(S3FileFormat::JsonArray)
.max_objects(10);
assert_eq!(config.bucket, "my-bucket");
assert_eq!(config.prefix.as_deref(), Some("data/"));
assert_eq!(config.region.as_deref(), Some("us-west-2"));
assert_eq!(
config.endpoint_url.as_deref(),
Some("http://localhost:9000")
);
assert!(matches!(config.file_format, S3FileFormat::JsonArray));
assert_eq!(config.max_objects, Some(10));
}
#[test]
fn file_format_default_is_json_lines() {
let format = S3FileFormat::default();
assert!(matches!(format, S3FileFormat::JsonLines));
}
#[test]
fn batch_size_defaults_to_default_batch_size() {
let config = S3SourceConfig::new("my-bucket");
assert_eq!(config.batch_size, faucet_core::DEFAULT_BATCH_SIZE);
}
#[test]
fn with_batch_size_overrides_default() {
let config = S3SourceConfig::new("my-bucket").with_batch_size(500);
assert_eq!(config.batch_size, 500);
}
#[test]
fn batch_size_zero_is_accepted_as_no_batching_sentinel() {
let config = S3SourceConfig::new("my-bucket").with_batch_size(0);
assert_eq!(config.batch_size, 0);
assert!(faucet_core::validate_batch_size(config.batch_size).is_ok());
}
#[test]
fn batch_size_above_max_is_rejected_by_validate_batch_size() {
let config =
S3SourceConfig::new("my-bucket").with_batch_size(faucet_core::MAX_BATCH_SIZE + 1);
assert!(faucet_core::validate_batch_size(config.batch_size).is_err());
}
#[test]
fn batch_size_deserializes_from_json() {
let json = r#"{
"bucket": "my-bucket",
"prefix": null,
"region": null,
"endpoint_url": null,
"file_format": "json_lines",
"max_objects": null,
"concurrency": 10,
"batch_size": 250
}"#;
let config: S3SourceConfig = serde_json::from_str(json).unwrap();
assert_eq!(config.batch_size, 250);
}
#[test]
fn verify_defaults_length_on_checksum_off() {
let cfg = S3SourceConfig::new("b");
assert!(cfg.verify_length, "length verification defaults on");
assert!(!cfg.verify_checksum, "checksum verification defaults off");
}
#[test]
fn verify_fields_default_when_absent_from_json() {
// An existing config that predates these fields must still parse, with
// length verification on and checksum off.
let json = r#"{
"bucket": "my-bucket",
"prefix": null,
"region": null,
"endpoint_url": null,
"file_format": "json_lines",
"max_objects": null,
"concurrency": 10,
"batch_size": 250
}"#;
let config: S3SourceConfig = serde_json::from_str(json).unwrap();
assert!(config.verify_length);
assert!(!config.verify_checksum);
}
#[test]
fn verify_builders_override() {
let cfg = S3SourceConfig::new("b")
.verify_length(false)
.verify_checksum(true);
assert!(!cfg.verify_length);
assert!(cfg.verify_checksum);
}
#[cfg(feature = "compression")]
#[test]
fn compression_default_is_auto() {
let cfg = S3SourceConfig::new("bucket");
assert_eq!(cfg.compression, faucet_core::CompressionConfig::Auto);
}
}