Android Firebase:无需身份验证即可匿名上载文件

Android Firebase:无需身份验证即可匿名上载文件,android,firebase,firebase-authentication,firebase-storage,Android,Firebase,Firebase Authentication,Firebase Storage,使用下面的代码,我可以看到未经身份验证就上载到firebase存储帐户的文件,但其他文件无法上载 UploadFileAsync upload_task; for(int i=0; i<Files.size(); i++) { upload_task=new UploadFileAsync(getApplicationContext());

使用下面的代码,我可以看到未经身份验证就上载到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) {
    }
}
UploadFileAsync upload\u任务;

对于(int i=0;i要将云存储用于Firebase,您需要:

  • 启用Firebase身份验证
  • 设置安全规则以允许未经身份验证的访问
若要启用身份验证,则可以在开发过程中遵循并将规则设置为公共访问:

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

为了将云存储用于Firebase,您需要:

  • 启用Firebase身份验证
  • 设置安全规则以允许未经身份验证的访问
若要启用身份验证,则可以在开发过程中遵循并将规则设置为公共访问:

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

如果您确实希望以第三方成员身份访问存储而不进行身份验证

你可以这样做

1.转到控制台 2.然后转到存储部分 3.然后切换到存储中的“规则”选项卡,在那里可以找到一些读写规则 4.使其
read=true,write=true
5.省省吧。这会给你一个警告,因为这是一个坏习惯
6.现在,如果您确实想以第三方成员身份无需身份验证即可访问您的存储,则任何人都可以无需身份验证即可访问您的存储

你可以这样做

1.转到控制台 2.然后转到存储部分 3.然后切换到存储中的“规则”选项卡,在那里可以找到一些读写规则 4.使其
read=true,write=true
5.省省吧。这会给你一个警告,因为这是一个坏习惯
6.现在,任何人都可以无需身份验证即可访问您的存储

我已经设置了安全规则,如上所述,允许未经身份验证的访问,而无需启用匿名身份验证,我可以上载一些文件。但有时我会遇到异常:StorageUtil:error getting token java.util.concurrent.ExecutionException:com.google.android.gms.internal.zzbqn:请在尝试获取令牌之前登录。我已经设置了安全规则以允许未经验证的访问,如上所述,在不启用匿名身份验证的情况下,我可以上载一些文件。但有时会出现异常:StorageUtil:获取令牌时出错java.util.concurrent.ExecutionException:com.google.android.gms.internal.zzbqn:请在尝试获取令牌之前登录。问题是关于Firebase存储,而不是Firebase数据库。它们是具有单独配置的不同服务。问题是关于Firebase存储,而不是Firebase数据库。它们是具有单独配置的不同服务。