Firebase:匿名上传文件而无需身份验证

时间:2017-06-01 03:48:38

标签: android firebase firebase-authentication firebase-storage

使用下面的代码,我可以看到上传到我的firebase存储帐户的文件没有经过身份验证,但其他人无法上传。

UploadFileAsync upload_task;
for(int i=0; i<Files.size(); i++)
                        {


                                upload_task=new UploadFileAsync(getApplicationContext());
                                upload_task.filePath=Files.get(i);
                                upload_task.execute();
                                count++;



}


public class UploadFileAsync extends AsyncTask<String, Void, Boolean> {


    public String filePath;

    public boolean isUploaded=false;

    double progress;

    public Context context;

    private StorageReference storageReference;

    boolean res = false;


    //this method will upload the file
    private boolean uploadFile(final Context context, final String filePath) {


        //if there is a file to upload
        if (filePath != null) {


            //getting firebase storage reference
            storageReference = FirebaseStorage.getInstance().getReference();


            StorageReference riversRef = storageReference.child(filePath);
            riversRef.putFile(Uri.fromFile(new File(fileName)))
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            //if the upload is successfull

                            isUploaded=true;


                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception exception) {
                            //if the upload is not successfull

                            isUploaded=false;


                        }
                    })


                    .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                            //calculating progress percentage
                            progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();

                            Log.e("Progress"+fileName,""+progress);



                    });
        }
        //if there is not any file
        else {
            //you can display an error toast
        }





        return  isUploaded;
    }


    public UploadFileAsync (Context context){


        this.context = context;
        this.isUploaded=false;
    }


    @Override
    protected Boolean doInBackground(String... params) {



        isUploaded = uploadFile(this.context, filePath);




        return isUploaded;
    }

    @Override
    protected void onPostExecute(Boolean  result) {

    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}

即使我在AsyncTask之外调用upload函数,我也会收到此错误:

StorageUtil:获取令牌的错误java.util.concurrent.ExecutionException:com.google.android.gms.internal.zzbqn:请在尝试获取令牌之前登录。

2 个答案:

答案 0 :(得分:1)

要使用Cloud Storage for Firebase,您需要:

  • 启用Firebase身份验证
  • 将您的安全规则设置为允许未经身份验证的访问

要启用身份验证,follow the instructions in the docs;否则,您可以按照security rules docs并在开发过程中将规则设置为公共访问权限:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allObjects=**} {
      // public read, write access!
      // don't use this in production!!!
      allow read, write;
    }
  }
}

答案 1 :(得分:0)

如果您确实想要在没有身份验证的情况下以第三方成员身份访问您的数据库

你可以这样做

1.转到控制台 2.然后转到实时数据库部分 3.然后切换到realtine数据库中的rules选项卡,在那里您将找到一些读写规则 4.使它成为read = true, write = true 5保存..它会给你一个警告,因为这是一个不好的做法 6.现在任何人都可以访问您的数据库而无需身份验证

相关问题