繁体   English   中英

从谷歌云存储读取 blob object(图像)正在返回 null - spring 启动应用程序

[英]Reading a blob object (image) from google cloud storage is returning null in camel - spring boot application

我正在使用 spring 启动应用程序中的 apache 骆驼从谷歌云存储读取文件。 text/plain 类型文件的内容成功返回,但图像文件不返回任何内容,并且它们的内容类型在打印时返回 null。

java 代码(处理器):

package processors;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

import com.google.api.gax.paging.Page;
import com.google.api.services.storage.Storage.Buckets;
import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;




public class ListObjFromGCPProcessor implements Processor{

        public void process(Exchange xchg) throws Exception {
            
            //Storage storage = StorageOptions.getDefaultInstance().getService();
            Credentials credentials = GoogleCredentials.fromStream(new FileInputStream("D:/Users/User/MyFiles/Apache Camel github/SagesseMobileAppAPIs/camel-SagesseMobApp/src/main/resources/gcp-credentials.json"));
            Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
            
            
            Page<Blob> blobs = storage.list("sagessapp_test");
            String name = "kitten.png";
            
            for (Blob blob: blobs.getValues()) {
                if (name.equals(blob.getName())) {
                    byte[] content= blob.getContent();
                    xchg.getIn().setBody(content);
                    System.out.println(Base64.getEncoder().encodeToString(content));
                    System.out.println(blob.getContentType());
            
                }
            }
            
            
        }
        
        
    
}

gcp: 在此处输入图像描述 知道如何解决此问题并将图像内容返回为 base64? 谢谢

文件 (kitten.png) 以 0 字节大小出现在图像上。
验证文件存在且内容不是 null 后,您需要将其从二进制格式化为 base64。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM