无法在使用Spring Boot时预览存储在Firebase存储中的上传图像。

huangapple go评论80阅读模式
英文:

Unable to preview uploaded image on firebase storage using spring boot

问题

以下是您提供的代码的中文翻译:

public FileRequest uploadImage(FileRequest fileRequest, MultipartFile file) throws IOException {
    if(file.isEmpty()){
        throw new NullPointerException("未找到文件..");
    }
    byte[] fileByteArray = file.getBytes();
    ClassPathResource resource = new ClassPathResource("firebase.json");
    Storage storage = StorageOptions
            .newBuilder()
            .setCredentials(ServiceAccountCredentials
                    .fromStream(resource.getInputStream()))
            .build()
            .getService();
    BlobId blobId = BlobId.of(FileConstant.bucketName, fileRequest.getUploadContext() + "/" + fileRequest.getFileId());
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(fileRequest.getMimeType()).build();
    storage.create(blobInfo, fileByteArray);
    return fileDAO.uploadFile(fileRequest);
}

如果您有任何其他问题或需要进一步帮助,请随时提问。

英文:

I am trying to upload file in firebase storage using spring boot, below is my piece of code, my file is getting uploaded but i try to preview it from firebase UI is preview is not loading(pls refer the image) 无法在使用Spring Boot时预览存储在Firebase存储中的上传图像。

, while when i am uploading same file from Upload file option from firebase UI then it is previewing fine. Pls assist me with this problem.

public FileRequest uploadImage(FileRequest fileRequest, MultipartFile file) throws IOException {
        if(file.isEmpty()){
            throw new NullPointerException("No File Found..");
        }
        byte[] fileByteArray = file.getBytes();
        ClassPathResource resource = new ClassPathResource("firebase.json");
        Storage storage = StorageOptions
                .newBuilder()
                .setCredentials(ServiceAccountCredentials
                        .fromStream(resource.getInputStream()))
                .build()
                .getService();
        BlobId blobId = BlobId.of(FileConstant.bucketName,fileRequest.getUploadContext() + "/" + fileRequest.getFileId());
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(fileRequest.getMimeType()).build();
        storage.create(blobInfo,fileByteArray);
        return fileDAO.uploadFile(fileRequest);
    }

答案1

得分: 1

当您通过 Firebase UI 上传文件时,将自动生成访问令牌,但通过 Java 上传的文件不会自动生成。

您需要创建一个 Map 来定义一些元数据。

Map<String, String> map = new HashMap<>();
map.put("firebaseStorageDownloadTokens", imageName);

然后将其传递到您的 blobInfo 中:

BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
                .setMetadata(map)
                .setContentType(file.getContentType())
                .build();

您的代码应该如下所示:

public FileRequest uploadImage(FileRequest fileRequest, MultipartFile file) throws IOException {
        if(file.isEmpty()){
            throw new NullPointerException("No File Found..");
        }
        byte[] fileByteArray = file.getBytes();
        ClassPathResource resource = new ClassPathResource("firebase.json");
        Storage storage = StorageOptions
                .newBuilder()
                .setCredentials(ServiceAccountCredentials
                        .fromStream(resource.getInputStream()))
                .build()
                .getService();
        String imageName = fileRequest.getUploadContext() + "/" + fileRequest.getFileId();
        Map<String, String> map = new HashMap<>();
        map.put("firebaseStorageDownloadTokens", imageName);
        BlobId blobId = BlobId.of(FileConstant.bucketName, imageName);
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setMetadata(map).setContentType(fileRequest.getMimeType()).build();
        storage.create(blobInfo,fileByteArray);
        return fileDAO.uploadFile(fileRequest);
    }
英文:

When you upload files via firebase UI will have an access token automatically generated but no for files uploaded via Java.

You need to create a Map to define some metadata.

Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
map.put(&quot;firebaseStorageDownloadTokens&quot;, imageName);

And pass it into your blobInfo:

BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
                .setMetadata(map)
                .setContentType(file.getContentType())
                .build();

Your code should look like this:

public FileRequest uploadImage(FileRequest fileRequest, MultipartFile file) throws IOException {
        if(file.isEmpty()){
            throw new NullPointerException(&quot;No File Found..&quot;);
        }
        byte[] fileByteArray = file.getBytes();
        ClassPathResource resource = new ClassPathResource(&quot;firebase.json&quot;);
        Storage storage = StorageOptions
                .newBuilder()
                .setCredentials(ServiceAccountCredentials
                        .fromStream(resource.getInputStream()))
                .build()
                .getService();
        String imageName = fileRequest.getUploadContext() + &quot;/&quot; + fileRequest.getFileId();
        Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
        map.put(&quot;firebaseStorageDownloadTokens&quot;, imageName);
        BlobId blobId = BlobId.of(FileConstant.bucketName, imageName);
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setMetadata(map).setContentType(fileRequest.getMimeType()).build();
        storage.create(blobInfo,fileByteArray);
        return fileDAO.uploadFile(fileRequest);
    }

huangapple
  • 本文由 发表于 2020年9月3日 16:02:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63719320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定