将本地图像上传到Firebase存储(Unity3D)

时间:2017-03-20 03:23:59

标签: android unity3d firebase firebase-storage

我需要从本地路径将照片上传到我的firebase存储空间,但我一直收到错误

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)

这是我的代码:

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);
        }

我尝试使用PutBytesAsync而不是putFileAsync并且没有收到错误但是上传的文件类型是application / octet-stream

我非常需要帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

这里似乎有两个问题:

  1. 初始错误来自Could not find file "C:\Users\jorren\AppData\LocalLow\DefaultCompany\JKL\636256043481512729.jpg"。你检查过这个文件是否存在于磁盘上?似乎无法找到:(
  2. 内容类型由文件元数据设置,如果找不到,则默认为application/octet-stream。对于磁盘上的文件,我们经常可以从文件中选择内容类型,但对于内存数据,我们不能。只需在元数据中设置内容类型(每the docs)。
  3. 示例:

    // 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, ...)