Firebase:匿名上传文件而无需身份验证 [英] Firebase : Uploading files anonymously without authentication

查看:123
本文介绍了Firebase:匿名上传文件而无需身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码,我可以看到未经身份验证上传到Firebase存储帐户的文件,但是其他文件无法上传.

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

即使在AsyncTask外部调用了上载函数,我也会收到此错误:

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

StorageUtil:获取令牌java.util.concurrent.ExecutionException时出错:com.google.android.gms.internal.zzbqn:请先登录,然后再尝试获取令牌.

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

推荐答案

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

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

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

要启用身份验证,请按照文档中的说明进行操作;否则,您可以遵循安全规则文档,并将您的规则设置为公开访问在开发过程中:

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

这篇关于Firebase:匿名上传文件而无需身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆