Firebase : Uploading files anonymously without authentication

人走茶凉 提交于 2019-12-12 05:36:57

问题


Using the code below I can see files uploaded to my firebase storage account without authentication, but others fail to upload.

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

I get this error, even if I call upload function outside the AsyncTask :

StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.android.gms.internal.zzbqn: Please sign in before trying to get a token.


回答1:


In order to use Cloud Storage for Firebase, you need to either:

  • Enable Firebase Authentication
  • Set your Security Rules to allow unauthenticated access

To enable auth, follow the instructions in the docs; otherwise, you can follow the security rules docs and set your rules to public access during development:

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



回答2:


If you really want to acces your database as a third party member without authentication

you can do this

1.goto the console 2.then goto the realtime database section 3. then switch over to rules tab in realtine database where you will find some read and write rules 4. make it read = true, write = true 5 save it.. it'll give you a warning because it's a bad practice 6. now anyone can acces ur database without authentication



来源:https://stackoverflow.com/questions/44298205/firebase-uploading-files-anonymously-without-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!