使用 Spring Boot 将图像上传到 Firebase

问题描述

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

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");


}

解决方法

如果图片是公开的,这里是网址格式

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