-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathstorage_example.dart
More file actions
93 lines (75 loc) · 2.84 KB
/
storage_example.dart
File metadata and controls
93 lines (75 loc) · 2.84 KB
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
import 'dart:convert';
import 'package:dart_firebase_admin/dart_firebase_admin.dart';
import 'package:google_cloud_storage/google_cloud_storage.dart' hide Storage;
Future<void> storageExample(FirebaseApp admin) async {
print('\n### Storage Example ###\n');
// await basicExample(admin);
await signedUrlExample(admin);
}
Future<void> basicExample(FirebaseApp admin) async {
print('> Basic Storage usage...\n');
try {
final storage = admin.storage();
final bucket = storage.bucket('dart-firebase-admin.firebasestorage.app');
print('> Using bucket: ${bucket.id}\n');
final file = bucket.file('foo.txt');
print('> File: ${file.name}\n');
const fileContent = 'Hello from basicExample() in storage_example.dart';
print('> Uploading file "${file.name}" to Storage...\n');
await file.save(fileContent);
print('> ✓ File uploaded successfully!\n');
print('> Deleting file "${file.name}"...\n');
await file.delete();
print('> ✓ File deleted successfully!\n');
} catch (e, stackTrace) {
print('> ✗ Error: $e\n');
print('> Stack trace: $stackTrace\n');
}
}
Future<void> signedUrlExample(FirebaseApp admin) async {
print('> Signed URL Storage usage...\n');
String? url;
try {
final storage = admin.storage();
final bucket = storage.bucket('dart-firebase-admin.firebasestorage.app');
print('> Using bucket: ${bucket.id}\n');
final file = bucket.file('signed-url-example.txt');
const fileContent = 'Hello from signed url example!';
print('> Uploading file "${file.name}" to Storage...\n');
await file.save(utf8.encode(fileContent));
print('> ✓ File uploaded successfully!\n');
// Verify the file exists by getting its metadata with retry logic
// (handles eventual consistency - file might not be immediately queryable)
FileMetadata? metadata;
for (var i = 0; i < 3; i++) {
try {
metadata = await file.getMetadata();
break;
} catch (e) {
if (i < 2) {
print('> Retrying metadata fetch (${i + 1}/3)...\n');
await Future<void>.delayed(const Duration(milliseconds: 300));
} else {
rethrow;
}
}
}
if (metadata != null) {
print('> ✓ File verified in bucket: ${metadata.bucket}\n');
print('> File size: ${metadata.size} bytes\n');
print('> File created: ${metadata.timeCreated}\n');
}
final expires = DateTime.now().add(const Duration(minutes: 30));
url = await file.getSignedUrl(
GetFileSignedUrlOptions(action: 'read', expires: expires),
);
print('> ✓ Signed URL generated for ${file.name}\n');
} catch (e, stackTrace) {
print('> ✗ Error: $e\n');
print('> Stack trace: $stackTrace\n');
}
print('Signed URL: $url\n');
if (url != null) {
print('You can access the file at the URL above for 30 minutes.\n');
}
}