CRUD folder in Supabase bucket using Flutter - flutter

can I CRUD a folder to the Supabase bucket from flutter app using supabase_flutter package?
for example create folder inside 'my_bucket' bucket on Supabase storage and upload a file to it using the code bellow:
final bytes = File('path').readAsBytesSync();
String link = '';
final fileExt = path.split('.').last;
final supabase = Supabase.instance.client;
try {
await supabase.storage
.from('my_bucket') // create folder from here
.uploadBinary(
'file.$fileExt',
bytes,
);
link = await supabase.storage
.from('my_bucket') // get alink for the file
.createSignedUrl('file.$fileExt');
} on PostgrestException catch (e) {
print('ErrorSupa:${e.message}');
}

You can just pass the path to the file like the following, and necessary folders will be created automatically.
final bytes = File('path').readAsBytesSync();
String link = '';
final fileExt = path.split('.').last;
final supabase = Supabase.instance.client;
await supabase.storage
.from('my_bucket') // create folder from here
.uploadBinary(
'path/to/file/file.$fileExt',
bytes,
);
link = await supabase.storage
.from('my_bucket') // get alink for the file
.createSignedUrl('path/to/file/file.$fileExt');

Related

how to give title in flutter to pdf file before save to download folder?

I want exactly this page in flutter
I'm not sure where that screen is coming from. But you can try the file_picker_writable plugin. (shameless plug).
Future<void> _openFilePickerForCreate() async {
final rand = Random().nextInt(10000000);
final fileInfo = await FilePickerWritable().openFileForCreate(
fileName: 'newfile.$rand.codeux',
writer: (file) async {
final content = 'File created at ${DateTime.now()}\n\n';
await file.writeAsString(content);
},
);
if (fileInfo == null) {
_logger.info('User canceled.');
return;
}
final data = await _appDataBloc.store.load();
await _appDataBloc.store
.save(data.copyWith(files: data.files + [fileInfo]));
}
}
this will open a folder chooser dialog for the user and a default file name (given by fileName).
This code example will open the following dialog:

Is there a way to download folder from firebase storage with flutter?

I want to download .epub files from firebase storage. I can download image file cause I know imageUrl but not .epub file url. How should I do? I store fileName, imageUrl in Firestore but I don't know epub file's url . So I can't store it.
downloadFile(fileName,imageUrl) async{
Dio dio=Dio();
final storageRef=FirebaseStorage.instance.ref();
final imageUrls =await storageRef.child("Featured").child('a clock orange/Anthony-Burgess-A-Clockwork-Orange-W.-W.-Norton-_-Company-_1986_.epub').getDownloadURL();
String savePath= await getPath(fileName);
dio.download(imageUrls, savePath,
onReceiveProgress: (rcv,total){
setState((){
progress=((rcv/total) *100).toStringAsFixed(0);
});
if (progress == '100') {
setState(() {
isDownloaded = true;
});
}
}).then((_){
if (progress=="100"){
setState(() {
isDownloaded=true;
});
}
});}
I tried this. But it didn't work.
.
Use Firebase's writeToFile instead of dio's download.
final fileRef = storageRef.child("<path here>");
final appDocDir = await getApplicationDocumentsDirectory();
final filePath = "${appDocDir.absolute}/<path here>";
final file = File(filePath);
final downloadTask = fileRef.writeToFile(file);
downloadTask.snapshotEvents.listen((taskSnapshot) {
...
}
See Download to a local file for details.

How to get the google docs/spreadsheet id using google drive api in flutter?

I have created a function that is implemented in my flutter app, I set up all the configurations in the google cloud console for uploading files to google drive API. My function looks like this
Future<void> uploadFileOnDrive() async {
try {
//auth credentials
final googleSignIn =
signIn.GoogleSignIn.standard(scopes: [drive.DriveApi.driveScope]);
final signIn.GoogleSignInAccount? account = await googleSignIn.signIn();
// print("User account $account");
final authHeaders = await account!.authHeaders;
final authenticateClient = GoogleAuthClient(authHeaders);
final driveApi = drive.DriveApi(authenticateClient);
//Uploading the file with "hi" text
final Stream<List<int>> mediaStream =
Future.value([104, 105]).asStream().asBroadcastStream();
var media = new drive.Media(mediaStream, 2);
var driveFile = new drive.File();
driveFile.name = "hello_world.txt";
final result = await driveApi.files.create(driveFile, uploadMedia: media);
//printing the values
print(result.driveId);
print(result.name);
print(result.id);
print(result.createdTime);
print("Upload result: $result");
Fluttertoast.showToast(msg: "Uploaded succesfully");
} catch (e) {
Fluttertoast.showToast(msg: e.toString());
}
}
This function is uploading files successfully on my drive but I need to edit these files for which I need their ID.
The result contains the Instance of 'file' but when I try to print the result.driveId, I get null value but result.name is printing "hello_world.txt" correctly and result.id is also printing some id which is not the actual id of the "hello_world.txt" file (I checked the URL, it does not match).
Am I doing anything wrong here? Please correct me. Thanks

How can i implement Flutter File Picker in Website

How can we choose file from local system drives in Flutter web development . Not in App
Please any one suggest me .
I am using like this
I can able to choose file
But how can i get the file path
You need to get the data and create a MediaInfo class for it
class MediaInfo{
String fileName;
String filePath;
}
Get File Path
Future<MediaInfo> convertFileGetNamePath({#required html.File file}) async {
final Map<String, dynamic> infoData = {};
final reader = html.FileReader();
reader.readAsDataUrl(file);
await reader.onLoad.first;
final fileName = file.name;
infoData.addAll({
'name': fileName,
'path': filePath,
});
MediaInfo webImageInfo = MediaInfo();
webImageInfo.fileName = infoData['name'];
webImageInfo.filePath = infoData['path'];
return webImageInfo;
}
Implement it in your code
MediaInfo getInfo = await convertFileGetNamePath(file: imageFile);
fileName = getInfo.fileName;

Problem with create new directory in Flutter App

I have a problem with creating a new directory in Flutter app (testing on iOS simulator). The path I get after function create() is just a root path, not a new directory path. Does anyone know this problem? Please help me.
My code is below:
static Future<String> createDirectory(String folderName) async {
final Directory _localStorage = await getApplicationSupportDirectory();
final Directory _newDir = Directory('${_localStorage.path}/$folderName/');
if (await _newDir.exists()) {
return _newDir.path;
}
else {
final Directory _newAppDir = await _localStorage.create(recursive: true)
return _newAppDir.path;
}
}
You are trying to create existing directory (root directory returned by getApplicationSupportDirectory method).
Instead of:
final Directory _newAppDir = await _localStorage.create(recursive: true);
You need to use:
final Directory _newAppDir = await _newDir.create(recursive: true)