Firebase存储 - 限制用户上传到Firebase存储的图片大小 [英] Firebase storage - Limit size of image that users upload to firebase storage

查看:209
本文介绍了Firebase存储 - 限制用户上传到Firebase存储的图片大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的firebase开发者。我正在开发一个android应用程序,让用户将图片上传到firebase存储。因为我正在使用Spark模式,所以只有5Gb的存储空间。
我想限制用户上传到Firebase存储的图片大小。
下面是我用来上传图片到firebase存储的代码。



pre $ U $ imageUri =的ImagePath);
//需要授权android.permission.MANAGE_DOCUMENTS来上传文件。
final StorageReference photoRef = mStorageRef.child(Constants.FIREBASE_LOCATION_IMAGE_EXAM).child(imageUri.getLastPathSegment());
taskStarted();
photoRef.putFile(imageUri).addOnSuccessListener(new OnSuccessListener< UploadTask.TaskSnapshot>(){
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot){
Log.d (TAG,upload success);
mUploadExamTaskCount--;
examURLArray.add(new ExamPage(taskSnapshot.getDownloadUrl()。toString()));
if(mUploadExamTaskCount == 0 ){//上传文件成功

更新图像链接到数据库
$ $ b $ DatabaseReference firebaseExamPage = FirebaseDatabase.getInstance()。getReference(Constants.FIREBASE_LOCATION_EXAM_PAGE );
firebaseExamPage.child(examKey).setValue(examURLArray);
Log.d(标签,发送上传考试成功br);
Intent taskCompletedIntent = new Intent(ACTION_UPLOAD_EXAM_COMPLETED);
LocalBroadcastManager.getInstance(getApplicationContext())。sendBroadcast(taskCompletedIntent);
}
taskCompleted();







$你有任何想法如何解决这个问题?

解决方案

通过

  service firebase.storage {
match / b /< bucket> / o {
match / files / {fileName} {
allow read;
允许写入:if request.resource.size< 10 * 1024 * 1024; // 10MB限制例如



code $ pre

I am a new firebase developer. I am developing an android app that lets users upload images to firebase storage. Because I am using Spark mode I only have 5Gb of storage. I want to limit the size of images that users upload to firebase storage. Below is the code that I use to upload an image to firebase storage.

Uri imageUri = Uri.parse(imagePath);
                    //need permistion android.permission.MANAGE_DOCUMENTS to upload file.
                    final StorageReference photoRef = mStorageRef.child(Constants.FIREBASE_LOCATION_IMAGE_EXAM).child(imageUri.getLastPathSegment());
                    taskStarted();
                    photoRef.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            Log.d(TAG, "upload success");
                            mUploadExamTaskCount--;
                            examURLArray.add(new ExamPage(taskSnapshot.getDownloadUrl().toString()));
                            if (mUploadExamTaskCount == 0) {//upload files success.
                                /**
                                 * update image link to database.
                                 */
                                DatabaseReference firebaseExamPage = FirebaseDatabase.getInstance().getReference(Constants.FIREBASE_LOCATION_EXAM_PAGE);
                                firebaseExamPage.child(examKey).setValue(examURLArray);
                                Log.d(TAG, "send upload  exam success br");
                                Intent taskCompletedIntent = new Intent(ACTION_UPLOAD_EXAM_COMPLETED);
                                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(taskCompletedIntent);
                            }
                            taskCompleted();
                        }
                    })

Do you have any idea how to resolve this problem ?

解决方案

Per the Firebase Storage Security Rules docs, you can write rules that check the size of an uploaded file:

service firebase.storage {
  match /b/<bucket>/o {
    match /files/{fileName} {
      allow read;
      allow write: if request.resource.size < 10 * 1024 * 1024; // 10MB limit for instance
    }
  }
}

这篇关于Firebase存储 - 限制用户上传到Firebase存储的图片大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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