Firebase:如何在上传文件之前检查文件是否已存在

时间:2018-10-04 11:00:08

标签: android firebase

我将Firebase存储用于云存储

在上传文件时,如果存储参考中已经存在该文件,我就不想上传。我只需要跳过上传并继续下一个文件。

当前,我是这样上传的

    class ProductsAdminPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // Scaffold is inside DefaultTabController because DefaultTabController needs access to Scaffold
    return DefaultTabController(
      // Number of tabs
      length: 2,
      child: Scaffold(
        drawer: Drawer(
          child: Column(
            children: <Widget>[
              AppBar(
                automaticallyImplyLeading: false,
                title: Text('Choose'),
              ),
              ListTile(
                title: Text('All Products'),
                onTap: () {
                  Navigator.pushReplacement(context, MaterialPageRoute(builder: (BuildContext context) => ProductsPage()));
                },
              )
            ],
          ),
        ),
        appBar: AppBar(
          title: Text('Manage Products'),
          // TabBar is added as the bottom widget of the appBar
          bottom: TabBar(
            tabs: <Widget>[
              // No of Tab() Widgets Should be equal to length
              Tab(
                icon: Icon(Icons.create),
                text: 'Create Product',
              ),
              Tab(
                icon: Icon(Icons.list),
                text: 'My Products',
              ),
            ],
          ),
        ),
        body: TabBarView(
          // No of pages should be equal to length
          children: <Widget>[ProductCreatePage(), ProductListPage()],
        ),
      ),
    );
  }
}

显然,再次上传,如果文件已经存在,请替换。我只是想通过检查存储桶中是否已存在文件名来跳过不需要的上传。 那可能吗?

2 个答案:

答案 0 :(得分:5)

尝试获取元数据。如果失败,则文件不存在

uploadRef.getMetadata()
    .addOnSuccessListener({ /*File exists*/ })
    .addOnFailureListener({
        //File do not exist
        UploadTask uploadTask = uploadRef.putFile(file);
    })

如果您要完全禁止文件覆盖,而不仅仅是检查文件是否存在,您可以像这样指定存储的安全规则

allow write: if resource.metadata == null; // 

但是我从未测试过它,所以我不确定,也许resource == null也会起作用

答案 1 :(得分:1)

firebase中没有像现在这样的功能,但是您可以通过将文件名保存在数据库中来手动完成。您可以在上传前检查文件名是否存在。您可以使用firestore进行此操作。

这是检查图片是否存在

ERROR: signalclear({30,10,40,40,20,30,30,30,-1},{10,0,20,-1},{...}) // this sequence of numbers is only for testing.
Expected Result: {...} = {20,10,20,30,20,10,20,30,-1}
My Result: {...} = {20,10,20,30,-1}

这是用于将文件名保存到数据库中

    DocumentReference docRef = db.collection("images").document("file1");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                  // your code for uploading next image
                Log.d(TAG, "DocumentSnapshot data: " + document.getData());
            } else {
               // your code for uploading this image
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});
相关问题