首页 > 解决方案 > 如何使用 Spring Boot 将图像上传到 Firebase 上的特定参考或文件夹

问题描述

我正在尝试使用 Spring Boot 将图像上传到 Firebase 存储中的特定参考或文件夹。

我不知道如何设置图像或文件的上传目录。

下面是我直接在存储桶名称中上传文件的代码。我想将文件上传到/tests文件夹。

public String uploadFile(File file, String fileName, String contentType) throws IOException {
    BlobId blobId = BlobId.of(BUCKET_NAME, fileName);

    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(contentType).build();

    Credentials credentials = GoogleCredentials.fromStream(new FileInputStream(ResourceUtils.getFile(GOOGLE_CREDENTIALS_CONFIG_FILE)));

    Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

    storage.create(blobInfo, Files.readAllBytes(file.toPath()));

    return String.format(DOWNLOAD_URL, URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString()));
}

那是我的firebase结构目录

在 react-native 方面,我设置了这样的引用,但我不知道如何在 spring-boot 中添加它。

storage().ref('tests/'+this.state.photo1.name)
           .putFile(this.state.photo1.uri)
           .then(response => {
               console.log("Firebase response ", response);
           })
           .catch((e) => console.log('uploading image to firebase error => ', e));

标签: javaspringspring-bootfirebase-storage

解决方案


推荐阅读