英文:
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)
, 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<String, String> map = new HashMap<>();
map.put("firebaseStorageDownloadTokens", 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("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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论