masamune_storage_functions 3.2.6
masamune_storage_functions: ^3.2.6 copied to clipboard
Masamune package for uploading files to storage using Functions.
Masamune Storage Functions
[GitHub] | [YouTube] | [Packages] | [X] | [LinkedIn] | [mathru.net]
Masamune Storage Functions #
Usage #
Installation #
- Add the package to your project.
flutter pub add masamune_storage_functions
Setup #
- Configure the storage adapter backed by Cloud Functions.
// lib/main.dart
import 'package:masamune_storage_functions/masamune_storage_functions.dart';
import 'package:katana_functions_firebase/katana_functions_firebase.dart';
final functionsAdapter = FirebaseFunctionsAdapter(
options: DefaultFirebaseOptions.currentPlatform,
region: FirebaseRegion.asiaNortheast1,
);
final storageAdapter = FunctionsStorageAdapter(
functionsAdapter: functionsAdapter,
bucketName: 'your-project.appspot.com', // Your Cloud Storage bucket
);
void main() {
runMasamuneApp(
appRef: appRef,
storageAdapter: storageAdapter,
(appRef, _) => MasamuneApp(
appRef: appRef,
home: HomePage(),
),
);
}
Upload Files #
- Upload files through your Cloud Functions backend:
class FileUploadPage extends PageScopedWidget {
@override
Widget build(BuildContext context, PageRef ref) {
final storage = ref.app.storage();
return ElevatedButton(
onPressed: () async {
// Upload from bytes
final bytes = await File('path/to/image.png').readAsBytes();
final uploadedFile = await storage.uploadWithBytes(
bytes,
'uploads/user_${userId}/profile.png',
mimeType: 'image/png',
);
print("Uploaded to: ${uploadedFile.publicUrl}");
},
child: const Text("Upload File"),
);
}
}
Upload from URI:
// Upload from ModelImageUri or ModelVideoUri
final modelImageUri = ModelImageUri.fromPath('local/path/image.jpg');
final result = await storage.uploadWithUri(
modelImageUri.uri,
'uploads/image.jpg',
);
print("Download URL: ${result.downloadUrl}");
Download Files #
- Download files from Cloud Storage:
// Download file bytes
final bytes = await storage.download('uploads/image.png');
// Save to local file
await File('local/path/downloaded.png').writeAsBytes(bytes);
Delete Files #
// Delete a file
await storage.delete('uploads/old_file.png');
Backend Implementation #
Your Cloud Functions must handle storage operations:
// Cloud Functions
export const storage = functions.https.onCall(async (data, context) => {
const bucket = admin.storage().bucket();
switch (data.action) {
case "upload":
// Handle file upload
const file = bucket.file(data.path);
await file.save(Buffer.from(data.bytes, 'base64'), {
contentType: data.mimeType,
});
const [url] = await file.getSignedUrl({
action: 'read',
expires: Date.now() + 3600000,
});
return {
publicUrl: url,
downloadUrl: url,
};
case "download":
// Handle file download
const downloadFile = bucket.file(data.path);
const [buffer] = await downloadFile.download();
return { bytes: buffer.toString('base64') };
case "delete":
// Handle file deletion
await bucket.file(data.path).delete();
return { success: true };
}
});
GitHub Sponsors #
Sponsors are always welcome. Thank you for your support!