个性化阅读
专注于IT技术分析

Firebase云存储设置和配置

点击下载

在上一节中, 我们了解了云存储, 其关键功能, 特性及其工作方式。现在, 我们将讨论如何使用Firebase设置和配置Android应用程序以在应用程序中使用Cloud Storage。就像Firebase实时数据库和Firebase Firestore一样, Cloud Storage的开始步骤相同, 并且将使用Kotlin。因此, 让我们从开始步骤开始并详细说明每个步骤, 执行这些步骤是为了设置和配置应用程序以在Firebase中使用Cloud Storage。

步骤1:

第一步, 我们将使用空活动和Kotlin语言创建一个新的Android Studio项目, 并将其命名为CloudStorageExample。

云存储设置和配置

第2步:

我们将通过Firebase助手或使用控制台手动将Android应用程序与Firebase连接。之后, 我们将所有必需的库和插件添加到我们的app.gradle文件中。我们还将添加mavenLocal()作为我们的存储库和所有项目。

云存储设置和配置
云存储设置和配置
云存储设置和配置

第三步:

我们将转到Firebase控制台, 然后在“开发人员”->“存储”中查看Firebase云存储。

云存储设置和配置

步骤4:

我们将通过单击“入门”来创建数据库。单击“入门”后, 将打开一个弹出框, 我们在其中设置了具有特定规则的存储, 然后单击“下一步”。

云存储设置和配置
云存储设置和配置

单击下一步后, 将打开一个弹出框。在这里, 我们可以根据要定位的位置选择我们的Cloud Storage位置, 最后单击“完成”。

云存储设置和配置

步骤5:

单击“完成”后, 将创建一个云存储, 其外观与实时数据库和Cloud Firestore不同。在这里, 我们分别具有文件, 规则和用于存储文件的用法, 安全性规则和用法。

云存储设置和配置
云存储设置和配置

步骤6:

无需更改我们的安全规则, 因为默认情况下, 经过身份验证的用户只能读写存储。这些规则定义为:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

如果我们要公开我们的云存储, 则按照以下安全规则进行修改:

//Everyone can read or write to the bucket, even non-user of our app.
//Because it is shared with Google App engine, this will also make files uploaded via the GAE public.

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}

如果我们想使用这些规则进行数据验证, 则有可能。我们可以验证文件名和路径。这些规则还用于验证元数据属性, 例如contentType和size。

service firebase.storage {
  match /b/{bucket}/o {
    match /images/{imageId} {// Upload onlt those image file that's less than 5MB
      allow write: if request.resource.size < 5 * 1024 * 1024
                   && request.resource.contentType.matches('image/.*');
    }
  }
}

设置和配置已在此处完成, 现在我们可以实现代码以使用此存储。


赞(0)
未经允许不得转载:srcmini » Firebase云存储设置和配置

评论 抢沙发

评论前必须登录!