Upload Files

Upload files directly from the device to Cloudflare R2. The SDK handles the presigned URL, upload, and confirmation in one call.

Basic upload

Give the bucket, a path within it, and the file. The SDK handles the presigned URL exchange, the upload and the confirmation.

lib/storage.dart
Dart
import 'dart:io';

final result = await Koolbase.storage.upload(
  bucket: 'avatars',
  path: 'user_${userId}.jpg',
  file: File('/path/to/image.jpg'),
);

print(result.object.path);        // user_abc123.jpg
print(result.object.size);        // file size in bytes
print(result.object.contentType); // image/jpeg
print(result.downloadUrl);        // a signed link — it expires, don't store it

Upload from bytes

In Flutter, when you have the file's bytes rather than a path on disk — from an image picker, or on the web — use uploadBytes(). In the browser, pass the picked File itself:

lib/storage.dart
Dart
import 'package:image_picker/image_picker.dart';

final picked = await ImagePicker().pickImage(source: ImageSource.gallery);

if (picked != null) {
  final result = await Koolbase.storage.uploadBytes(
    bucket: 'avatars',
    path: 'user_${userId}.jpg',
    bytes: await picked.readAsBytes(),
    contentType: 'image/jpeg',
  );
  print(result.object.path); // user_abc123.jpg
}

Upload with progress

Track upload progress for large files using the onProgress callback:

lib/storage.dart
Dart
await Koolbase.storage.upload(
  bucket: 'documents',
  path: 'reports/$filename',
  file: file,
  onProgress: (p) {
    // pending, uploading, verifying, then completed
    setState(() => uploadProgress = p.bytesSent / p.bytesTotal);
  },
);

Content type detection

Koolbase automatically detects the content type from the file extension. You can override it explicitly using the contentType parameter:

lib/storage.dart
Dart
// Auto-detected from the .pdf extension
await Koolbase.storage.upload(
  bucket: 'documents',
  path: 'report.pdf',
  file: file,
);

// Set explicitly
await Koolbase.storage.upload(
  bucket: 'exports',
  path: 'data.json',
  file: file,
  contentType: 'application/json',
);

Supported file types

Koolbase Storage accepts any file type by default. If the destination bucket has an allowed_mime_types allowlist configured, the upload is rejected up-front with mime_not_allowed — see Buckets for how to configure that allowlist.

Overwriting existing files

Uploads are safe-by-default: if a file already exists at the requested bucket+path, the SDK throws a conflict error rather than silently replacing the data. To explicitly overwrite, pass overwrite: true:

lib/storage.dart
Dart
// Default — fails if a file already exists at this path
await Koolbase.storage.upload(
  bucket: 'avatars',
  path: 'user_${userId}.jpg',
  file: file,
);

// Explicit overwrite — replaces the existing file
await Koolbase.storage.upload(
  bucket: 'avatars',
  path: 'user_${userId}.jpg',
  file: file,
  overwrite: true,
);

Safe-by-default uploads

Without overwrite: true, an upload to a path that already holds a file is rejected with KoolbaseStorageConflictException (Flutter) or KoolbaseStorageConflictError (React Native and web). Your data is never silently replaced. Catch the conflict and prompt the user, or retry with overwrite: true.

To keep earlier copies of a file that gets replaced, turn on versioning for the bucket.

Error handling

Catch typed exceptions for the cases you want to handle explicitly, and the base KoolbaseStorageException for everything else:

lib/storage.dart
Dart
try {
  await Koolbase.storage.upload(
    bucket: 'avatars',
    path: 'user_${userId}.jpg',
    file: file,
  );
} on KoolbaseStorageConflictException {
  showError('A file already exists there. Pass overwrite: true to replace it.');
} on KoolbaseStorageQuotaExceededException {
  showError('Storage is full. Delete some files first.');
} on KoolbaseStorageFileTooLargeException {
  showError('This file is too large for the bucket.');
} on KoolbaseStorageMimeTypeException {
  showError('This file type is not allowed in this bucket.');
} on KoolbaseUploadExpiredException {
  showError('Upload took too long. Please try again.');
} on KoolbaseStorageException catch (e) {
  showError('Upload failed: ${e.message}');
}

The Errors reference lists every storage code the server may return. Branch on the typed class; e.code holds the server's code for anything newer than your SDK.