【问题标题】:Firebase : Uploading files anonymously without authenticationFirebase:匿名上传文件而无需身份验证
【发布时间】:2017-11-02 01:14:13
【问题描述】:

使用下面的代码,我可以看到上传到我的 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 之外调用上传函数:

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

【问题讨论】:

    标签: android firebase firebase-authentication firebase-storage


    【解决方案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;
        }
      }
    }
    

    【讨论】:

    • 我已经将安全规则设置为允许未经身份验证的访问,如上,没有启用匿名身份验证我能够上传一些文件。但有时我会收到异常:StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.android.gms.internal.zzbqn: Please sign in before trying to get a token.
    【解决方案2】:

    如果您真的想以第三方成员身份访问您的存储而无需身份验证

    你可以这样做

    1.转到控制台 2.然后去存储部分 3.然后切换到存储中的规则选项卡,您会在其中找到一些读写规则 4. 让它read = true, write = true 5保存它..它会给你一个警告,因为这是一个不好的做法 6. 现在任何人都可以在没有身份验证的情况下访问您的存储

    【讨论】:

    • 问题是关于 Firebase 存储,而不是 Firebase 数据库。它们是具有不同配置的不同服务。
    猜你喜欢
    • 1970-01-01
    • 2016-06-08
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 2021-05-23
    • 2021-11-18
    • 2019-01-17
    相关资源
    最近更新 更多