streamToS3 method
Uploads the file to S3 and returns the stored object key.
The generated key includes the current application ID and stores uploads under the framework upload prefix.
When autoName is true, a UUID-based filename is generated while
preserving the extension when possible.
Returns the S3 object key on success, or null on failure.
Example:
final key = await file.streamToS3(acl: S3Acl.private);
print(key);
Implementation
Future<String?> streamToS3({S3Acl acl = .private, bool autoName = true}) async {
final s3Client = App().make<S3Client>();
final config = App().make<AppConfig>();
final uuid = App().make<Uuid>();
final String fileName;
if (autoName) {
final ext = extension.isNotEmpty ? '.$extension' : '';
final uuid = Uuid().v4();
fileName = '$uuid$ext';
} else {
fileName = filename;
}
final String key = "archery/web/app-${config.get('app.id', uuid.v4())}/storage/uploads/$fileName";
if (await s3Client.putObject(
key: key,
data: await _cachedBytes, // ← Stream directly to S3
acl: acl,
contentType: contentType,
)) {
return key;
}
return null;
}