Home > OS >  Uploading multiple files to firbase storage | Unity
Uploading multiple files to firbase storage | Unity

Time:12-15

Hey I'm trying to upload all files located in a folder to firestore storage. However im quite new to c# and unity.

I have the following code to upload a file located in the folder.

       if(Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead)){
                Debug.Log("Permissions Found");
                var Directory_path = ("SparseSpatialMap/Copia.meta");
                var path = (Application.persistentDataPath   "/"   Directory_path);             
        //Firestore Reference
        storage = FirebaseStorage.DefaultInstance;
        storageReference = storage.GetReferenceFromUrl("gs://houdini-ac884.appspot.com");
   
        StreamReader stream = new StreamReader(path);
        // Create a reference to the file you want to upload
        StorageReference riversRef = storageReference.Child("uploads/newFile.meta");

        // Upload the file to the path "uploads/newFile.meta"
        riversRef.PutStreamAsync(stream.BaseStream)
            .ContinueWith((task) => {
                if (task.IsFaulted || task.IsCanceled) {
                    Debug.Log(task.Exception.ToString());
                    // Uh-oh, an error occurred!
                }
                else {
                    // Metadata contains file metadata such as size, content-type, and download URL.
                    StorageMetadata metadata = task.Result;
                    string md5Hash = metadata.Md5Hash;
                    Debug.Log("Finished uploading...");
                    Debug.Log("md5 hash = "   md5Hash);
                }
            });
                    } else {
                            Debug.Log("No Permissions");
                            Permission.RequestUserPermission(Permission.ExternalStorageRead);
                            return;
                    }

The file i uploaded with success is located here " /storage/emulated/0/Android/data/estg.easy.ar/files/SparseSpatialMap/Copia.meta "

I want to upload all files in the /SparseSpatialMap direcory

CodePudding user response:

What you’re looking for is a way to get all file names, or paths. Try importing System.IO if you have already then you can use Directory.GetFiles(path) replacing path with the folder directory. Then you should have an array of strings representing each file.

CodePudding user response:

For anyone looking for the solution this was how i did it. got all files via their folder with DirectoryInfo and uploaded each individually using it's the respective name

         if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead)){ //Cria ficheiros em branco (Ou por nao ter acesso a um ficheiro que está a ser usado ou por ser Asyncrono)
                Debug.Log("Permissions Found");
                DirectoryInfo d = new DirectoryInfo(Application.persistentDataPath   "/SparseSpatialMap");
                FileInfo[] Files = d.GetFiles("*.meta"); //Getting meta files
                string str = "";
                foreach(FileInfo file in Files )
                    {
                str = str   ", "   file.Name;
                var Directory_path = ("SparseSpatialMap/"   file.Name);
                var path = (Application.persistentDataPath   "/"   Directory_path);             
                //Firestore Reference
                storage = FirebaseStorage.DefaultInstance;
                storageReference = storage.GetReferenceFromUrl("gs://houdini-ac884.appspot.com");
                // File located on disk
                string localFile = path.ToString();
                StreamReader stream = new StreamReader(path);
                // Create a reference to the file you want to upload
                StorageReference riversRef = storageReference.Child("uploads/"  file.Name);
                // Upload the file to the path "uploads/newFile.meta"
                riversRef.PutStreamAsync(stream.BaseStream)
                    .ContinueWith((task) => {
                        if (task.IsFaulted || task.IsCanceled) {
                            Debug.Log(task.Exception.ToString());
                            // Uh-oh, an error occurred!
                        }
                        else {
                            // Metadata contains file metadata such as size, content-type, and download URL.
                            StorageMetadata metadata = task.Result;
                            string md5Hash = metadata.Md5Hash;
                            Debug.Log("Finished uploading...");
                            Debug.Log("md5 hash = "   md5Hash);
                        }
                    });
                    }

                    Debug.Log(str);

                    } else {
                            Debug.Log("No Permissions");
                            Permission.RequestUserPermission(Permission.ExternalStorageRead);
                            return;
                    }
  • Related