Download & Delete

Get public or time-limited URLs for files, download file bytes directly, and permanently remove files from storage.

Get a download URL

Returns a URL for the file — for an image, a browser, or a video player.

lib/storage.dart
Dart
final url = await Koolbase.storage.getDownloadUrl(
  bucket: 'avatars',
  path: 'user_${userId}.jpg',
);

Image.network(url)

Public URL from R2

For a file in a public bucket the URL is its permanent CDN address. For a private bucket it is a signed link that expires after an hour — ask for a new one when you need it, and store the file's path, never the link.

Download file bytes

Download the raw bytes of a file directly into your app:

lib/storage.dart
Dart
final bytes = await Koolbase.storage.downloadBytes(
  bucket: 'documents',
  path: 'reports/q3-report.pdf',
  onProgress: (received, total) => print('$received of $total'),
);

await File('${tempDir.path}/q3-report.pdf').writeAsBytes(bytes);

List files in a bucket

List the files in a bucket a page at a time, optionally under a path prefix. Only files the caller may view are listed.

lib/storage.dart
Dart
final page = await Koolbase.storage.listFiles(
  bucket: 'attachments',
  prefix: 'posts/$postId/',
  limit: 50,
);

for (final file in page.files) {
  print(file.path);        // posts/abc/photo.jpg
  print(file.size);        // size in bytes
  print(file.contentType); // image/jpeg
}

// The next page, while there is one
if (page.nextCursor != null) {
  final next = await Koolbase.storage.listFiles(
    bucket: 'attachments',
    prefix: 'posts/$postId/',
    cursor: page.nextCursor,
  );
}

Delete a file

Permanently removes a file from R2 and its metadata from the database. This cannot be undone.

lib/storage.dart
Dart
await Koolbase.storage.delete(
  bucket: 'avatars',
  path: 'user_${userId}.jpg',
);

Deletion is permanent

Without versioning, a deleted file cannot be recovered — confirm with the person before deleting. With versioning on, earlier versions are kept and can be restored.

Common pattern — profile avatar

A complete example for uploading and displaying a user avatar:

lib/profile_screen.dart
Dart
Future<void> updateAvatar(Uint8List imageBytes, String userId) async {
  final path = 'user_$userId.jpg';

  // Upload the new avatar, replacing the old one
  await Koolbase.storage.uploadBytes(
    bucket: 'avatars',
    path: path,
    bytes: imageBytes,
    contentType: 'image/jpeg',
    overwrite: true,
  );

  // Store the path, not a download link: links expire
  await Koolbase.db.doc(profileId).update({'avatar_path': path});
}