将文件上传到Android Firebase存储中的用户特定目录?

时间:2018-08-27 16:35:23

标签: android firebase firebase-storage firebase-security-rules

我正在尝试使用Android中的Firebase存储上传文件。但是我收到以下异常:

E/StorageException: StorageException has occurred.
                    User does not have permission to access this object.
                     Code: -13021 HttpResult: 403
E/StorageException: The server has terminated the upload session
                    java.io.IOException: The server has terminated the upload session
                        at com.google.firebase.storage.UploadTask.zzs(Unknown Source)
                        at com.google.firebase.storage.UploadTask.zzr(Unknown Source)
                        at com.google.firebase.storage.UploadTask.run(Unknown Source)
                        at com.google.firebase.storage.StorageTask.zzl(Unknown Source)
                        at com.google.firebase.storage.zzx.run(Unknown Source)
                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                        at java.lang.Thread.run(Thread.java:761)
                     Caused by: java.io.IOException: {  "error": {    "code": 403,    "message": "Permission denied. Could not perform this operation"  }}
                        at bft.a(:com.google.android.gms.dynamite_dynamitemodulesc@12874022@12.8.74 (040308-204998136):147)
                        at bft.a(:com.google.android.gms.dynamite_dynamitemodulesc@12874022@12.8.74 (040308-204998136):119)
                        at bfn.a(:com.google.android.gms.dynamite_dynamitemodulesc@12874022@12.8.74 (040308-204998136):5)
                        at nd.onTransact(:com.google.android.gms.dynamite_dynamitemodulesc@12874022@12.8.74 (040308-204998136):12)
                        at android.os.Binder.transact(Binder.java:499)
                        at com.google.android.gms.internal.firebase_storage.zza.transactAndReadExceptionReturnVoid(Unknown Source)
                        at com.google.android.gms.internal.firebase_storage.zzm.zze(Unknown Source)
                        at com.google.android.gms.internal.firebase_storage.zzq.zza(Unknown Source)
                        at com.google.android.gms.internal.firebase_storage.zzf.zza(Unknown Source)
                        at com.google.firebase.storage.UploadTask.zzc(Unknown Source)
                        at com.google.firebase.storage.UploadTask.run(Unknown Source) 
                        at com.google.firebase.storage.StorageTask.zzl(Unknown Source) 
                        at com.google.firebase.storage.zzx.run(Unknown Source) 
                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                        at java.lang.Thread.run(Thread.java:761) 
E/StorageException: StorageException has occurred.
                    User does not have permission to access this object.
                     Code: -13021 HttpResult: 403
E/StorageException: The server has terminated the upload session
                    java.io.IOException: The server has terminated the upload session
                        at com.google.firebase.storage.UploadTask.zzs(Unknown Source)
                        at com.google.firebase.storage.UploadTask.zzr(Unknown Source)
                        at com.google.firebase.storage.UploadTask.run(Unknown Source)
                        at com.google.firebase.storage.StorageTask.zzl(Unknown Source)
                        at com.google.firebase.storage.zzx.run(Unknown Source)
                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                        at java.lang.Thread.run(Thread.java:761)
                     Caused by: java.io.IOException: {  "error": {    "code": 403,    "message": "Permission denied. Could not perform this operation"  }}
                        at bft.a(:com.google.android.gms.dynamite_dynamitemodulesc@12874022@12.8.74 (040308-204998136):147)
                        at bft.a(:com.google.android.gms.dynamite_dynamitemodulesc@12874022@12.8.74 (040308-204998136):119)
                        at bfn.a(:com.google.android.gms.dynamite_dynamitemodulesc@12874022@12.8.74 (040308-204998136):5)
                        at nd.onTransact(:com.google.android.gms.dynamite_dynamitemodulesc@12874022@12.8.74 (040308-204998136):12)
                        at android.os.Binder.transact(Binder.java:499)
                        at com.google.android.gms.internal.firebase_storage.zza.transactAndReadExceptionReturnVoid(Unknown Source)
                        at com.google.android.gms.internal.firebase_storage.zzm.zze(Unknown Source)
                        at com.google.android.gms.internal.firebase_storage.zzq.zza(Unknown Source)
                        at com.google.android.gms.internal.firebase_storage.zzf.zza(Unknown Source)
                        at com.google.firebase.storage.UploadTask.zzc(Unknown Source)
                        at com.google.firebase.storage.UploadTask.run(Unknown Source) 
                        at com.google.firebase.storage.StorageTask.zzl(Unknown Source) 
                        at com.google.firebase.storage.zzx.run(Unknown Source) 
                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                        at java.lang.Thread.run(Thread.java:761) 

我的Java代码:

File f= new File(filePath.getPath());
mStorageRef = FirebaseStorage.getInstance().getReference();
StorageReference userReference = mStorageRef.child(mAuth.getUid());
StorageReference fileReference = userReference.child(f.getName());               
fileReference.putFile(filePath)

我在Firebase中的安全规则:

  service firebase.storage {
  match /b/{bucket}/o {
    match /{userId} {      
      allow read,write: if request.auth.uid==userId;
     }
   }
  }

1 个答案:

答案 0 :(得分:1)

当我将安全规则更改为时,该问题已解决:

import pandas as pd

df = pd.DataFrame({'A': [2, 3, 4], 'B': [5, 7, 9], 'C':[10, 11, 12]})

df['A'] = df ['A']*3
df['B'] = df ['B']*2

df.to_excel('output.xlsx')

}