简体   繁体   中英

Upload a local image to Firebase storage (Unity3D)

I need to upload my photo to my firebase storage from local path but I keep on getting an error

System.AggregateException: Exception of type 'System.AggregateException' was thrown.

Firebase.Storage.StorageException: An unknown error occurred, please check the HTTP result code and inner exception for server response. ---> System.IO.FileNotFoundException: Could not find file "C:\Users\jorren\AppData\LocalLow\DefaultCompany\JKL\636256043481512729.jpg".
File name: 'C:\Users\jorren\AppData\LocalLow\DefaultCompany\JKL\636256043481512729.jpg'
  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00209] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:305 
  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
  at System.IO.File.OpenRead (System.String path) [0x00000] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:363 
  at Firebase.Storage.UploadTask..ctor (Firebase.Storage.StorageReference targetRef, Firebase.Storage.StorageMetadata metadata, System.String file, System.Uri existingUploadUri, System.Threading.Tasks.TaskCompletionSource`1 userTask, IProgress`1 progress, CancellationToken token, System.Threading.SynchronizationContext ctx) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---

UnityEngine.Debug:Log(Object)
Kudan.AR.Samples.<uploadToStorage>c__Iterator1:MoveNext() (at Assets/KudanAR/Samples/Scripts/SampleApp.cs:122)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

This is my code:

Firebase.Storage.FirebaseStorage storage = Firebase.Storage.FirebaseStorage.DefaultInstance;
        Firebase.Storage.StorageMetadata metadata;
        // Create a storage reference from our storage service
        Firebase.Storage.StorageReference storage_ref = storage.GetReferenceFromUrl("gs://sylpauljoyce-d9115.appspot.com");
        Firebase.Storage.StorageReference rivers_ref = storage_ref.Child("images/" + filename);
        string fileContents = Application.persistentDataPath + "/" + filename;
        var task = rivers_ref.PutFileAsync(fileContents);

        yield return new WaitUntil(() => task.IsCompleted);
         if (task.IsFaulted) {
             Debug.Log(task.Exception.ToString());
        } else {
            fileContents = "";
            Debug.Log("Finished uploading... Download Url: " + task.Result.DownloadUrl.ToString());
            url = task.Result.DownloadUrl.ToString ();

            Debug.Log (url);
        }

I tried using PutBytesAsync instead of putFileAsync and didn't get an error but the type of the file uploaded is application/octet-stream

I badly need help. Thanks!

Seems like there are two issues here:

  1. The initial error is from Could not find file "C:\\Users\\jorren\\AppData\\LocalLow\\DefaultCompany\\JKL\\636256043481512729.jpg" . Have you checked that this file exists on disk? Seems like it can't be found :(
  2. The content type is set by the file metadata, and it defaults to application/octet-stream if we can't find it. For files on disk we often can pick the content type off the file, but for in memory data we can't. Simply set the content type in your metadata (per the docs ).

Example:

// Create file metadata including the content type
var new_metadata = new Firebase.Storage.MetadataChange();
new_metadata.ContentType = "image/jpeg";

// Upload data and metadata
mountains_ref.PutBytesAsync(custom_bytes, new_metadata, ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM