【问题标题】:Uploading images to Firebase with Spring boot使用 Spring Boot 将图像上传到 Firebase
【发布时间】:2021-04-27 14:35:49
【问题描述】:

大家好,我目前正在从我的 spring boot api 提交图像到 firebase 存储,目前一切正常我可以提交我的图像但是图像的公共 url 不可用,我想知道如何从我的 api 获取我的图像的公共 url

public void uploadFile(MultipartFile multipartFile, String path) throws IOException {
    String objectName = generateFileName(multipartFile);
    StorageOptions storageOptions = StorageOptions.newBuilder()
            .setProjectId("my-project")
            .setCredentials(GoogleCredentials
                    .fromStream(new ClassPathResource("firebase_admin.json").getInputStream()))
            .build();
    Storage storage = storageOptions.getService();
    BlobId blobId = BlobId.of("bucket-name", objectName);

    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(multipartFile.getContentType()).build();

    Blob blob = storage.create(blobInfo, multipartFile.getBytes());

    System.out.println("UPLOAD FILE" + multipartFile.getName() + " " + multipartFile.getSize() + " octet");


}

【问题讨论】:

    标签: firebase spring-boot google-cloud-storage firebase-storage


    【解决方案1】:

    如果图片是公开的,这里是 URL 模式

    https://storage.cloud.google.com/<bucket-name>/<objectName>
    

    【讨论】:

      【解决方案2】:

      使用url中的alt参数获取下载地址如下:

      https://firebasestorage.googleapis.com/v0/b/<bucket-name>/o/<objet-name>?alt=media
      

      在这篇文章中获取更多详细信息:https://medium.com/teamarimac/file-upload-and-download-with-spring-boot-firebase-af068bc62614

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-29
        • 1970-01-01
        • 2019-09-23
        • 2017-04-15
        • 2022-08-13
        • 2020-12-22
        • 2018-07-12
        相关资源
        最近更新 更多