整合 google firebase

firebase 简介

firebase 是 Google 提供的一个移动和网络应用开发平台,它提供了一系列工具和服务来帮助开发者构建高质量的应用,改善用户体验,并快速成长其业务。Firebase 提供的服务覆盖了应用开发的许多方面,包括但不限于数据库、认证、分析、配置管理、文件存储、功能测试和云函数等。

Firebase 主要特性

  • Firestore Database: 高效、灵活的 NoSQL 数据库,支持实时数据同步。
  • Firebase Authentication: 提供简单、安全的认证功能,支持多种登录方式,如邮箱密码、手机号短信、社交登陆(Google、Facebook 等)。
  • Firebase Hosting: 提供静态和动态内容托管服务,支持 SSL,可以非常适合托管单页应用(SPA)。 -Firebase Storage: 为文件存储提供安全的云端解决方案,与 Firebase Auth 集成,易于控制用户对文件的访问。
  • Firebase Cloud Functions: 允许你运行服务器端代码响应事件触发,无需管理服务器。
  • Firebase Analytics: 免费、无限量的分析解决方案,可以帮助你理解用户如何与你的应用互动。
  • Firebase Cloud Messaging (FCM): 提供可靠的消息推送解决方案,支持通知和数据消息。

在这篇技术博客中,我们将深入探索如何使用 Java 上传文件到 Firebase Storage,同时也会介绍如何在 Firebase 添加应用、获取accountService.json文件,以及如何添加一个存储桶(Bucket)。

开始之前

在 Firebase 添加应用和获取 accountService.json 文件

在开始编码之前,您需要在 Firebase 控制台中设置您的项目和应用,并获取相应的配置文件。

  1. 创建 Firebase 项目:访问Firebase 控制台open in new window,点击“添加项目”并遵循指示完成项目的创建。
  2. 添加您的应用:项目创建后,您可以为特定的平台(如 iOS、Android 或 Web)添加应用。对于 Java 应用,选择添加 Web 应用。
  3. 获取accountService.json文件
    • 在 Firebase 控制台中,进入您的项目设置。
    • 找到“服务账户”选项卡。
    • 点击“生成新的私钥”,然后确认。这将下载包含您服务账户凭证的accountService.json文件。

accountService.json 的示例内容如下

{
  "type": "service_account",
  "project_id": "imaginix-eda2e",
  "private_key_id": "",
  "private_key": "",
  "client_email": "",
  "client_id": "104679440774414829061",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "",
  "universe_domain": "googleapis.com"
}

整合 firebase

添加 Firebase Admin SDK

Firebase Storage 提供了一个强大的、简单的对象存储解决方案,让您可以轻松地存储和服务用户生成的内容,如图片或视频等。为了在您的 Java 应用中实现这一功能,首先需要在项目中添加 Firebase Admin SDK 依赖。在您的pom.xml文件中添加如下依赖:

<dependency>
  <groupId>com.google.firebase</groupId>
  <artifactId>firebase-admin</artifactId>
  <version>7.0.1</version>
</dependency>

配置 firebase

package com.litongjava.open.chat.config;

import java.io.IOException;
import java.io.InputStream;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.litongjava.tio.boot.server.TioBootServer;
import com.litongjava.tio.utils.hutool.ResourceUtil;

/**
 * Created by Tong Li <https://github.com/litongjava>
 */
public class FirebaseAppConfiguration {

  public void config() throws IOException {
    InputStream serviceAccount = ResourceUtil.getResourceAsStream("rumi-bdb43-firebase-adminsdk-72auz-8428c295e4.json");

    // builder
    @SuppressWarnings("deprecation")
    FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
    // config
    builder.setCredentials(GoogleCredentials.fromStream(serviceAccount));

    // build
    FirebaseOptions options = builder.build();
    // init
    FirebaseApp.initializeApp(options);
    // destory
    TioBootServer.me().addDestroyMethod(() -> {
      FirebaseApp.getInstance().delete();
    });
  }
}

测试配置是否成功

package com.litongjava.open.chat.config;

import java.io.IOException;

import org.junit.Test;

import com.google.firebase.FirebaseApp;

public class FirebaseAppConfigurationTest {

  @Test
  public void test() {
    FirebaseAppConfiguration firebaseAppConfiguration = new FirebaseAppConfiguration();
    try {
      firebaseAppConfiguration.config();

      String name = FirebaseApp.getInstance().getName();
      System.out.println(name);
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println();
  }

}