-
-
Save lcbasu/aa99e847fef2534471ec12866a02ae8c to your computer and use it in GitHub Desktop.
Spring Boot - File Upload to Firebase (Google Cloud)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| * | |
| * | |
| * | |
| firebase.bucket-name=MY_FIREBASE_PROJECT_BUCKET_NAME | |
| firebase.image-url=https://storage.googleapis.com/${firebase.bucket-name}/%s | |
| * | |
| * | |
| * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.google.auth.oauth2.GoogleCredentials; | |
| import com.google.cloud.storage.Blob; | |
| import com.google.cloud.storage.Bucket; | |
| import com.google.firebase.FirebaseApp; | |
| import com.google.firebase.FirebaseOptions; | |
| import com.google.firebase.cloud.StorageClient; | |
| import lombok.Data; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.boot.context.event.ApplicationReadyEvent; | |
| import org.springframework.boot.context.properties.ConfigurationProperties; | |
| import org.springframework.context.annotation.Configuration; | |
| import org.springframework.context.event.EventListener; | |
| import org.springframework.core.io.ClassPathResource; | |
| import org.springframework.stereotype.Service; | |
| import org.springframework.util.StringUtils; | |
| import org.springframework.web.multipart.MultipartFile; | |
| import java.awt.image.BufferedImage; | |
| import java.io.IOException; | |
| @Service | |
| public class FirebaseImageService implements IImageService { | |
| @Autowired | |
| Properties properties; | |
| @EventListener | |
| public void init(ApplicationReadyEvent event) { | |
| // initialize Firebase | |
| try { | |
| ClassPathResource serviceAccount = new ClassPathResource("firebase.json"); | |
| FirebaseOptions options = new FirebaseOptions.Builder() | |
| .setCredentials(GoogleCredentials.fromStream(serviceAccount.getInputStream())) | |
| .setStorageBucket(properties.getBucketName()) | |
| .build(); | |
| FirebaseApp.initializeApp(options); | |
| } catch (Exception ex) { | |
| ex.printStackTrace(); | |
| } | |
| } | |
| @Override | |
| public String getImageUrl(String name) { | |
| return String.format(properties.imageUrl, name); | |
| } | |
| @Override | |
| public String save(MultipartFile file) throws IOException { | |
| Bucket bucket = StorageClient.getInstance().bucket(); | |
| String name = generateFileName(file.getOriginalFilename()); | |
| bucket.create(name, file.getBytes(), file.getContentType()); | |
| return name; | |
| } | |
| @Override | |
| public String save(BufferedImage bufferedImage, String originalFileName) throws IOException { | |
| byte[] bytes = getByteArrays(bufferedImage, getExtension(originalFileName)); | |
| Bucket bucket = StorageClient.getInstance().bucket(); | |
| String name = generateFileName(originalFileName); | |
| bucket.create(name, bytes); | |
| return name; | |
| } | |
| @Override | |
| public void delete(String name) throws IOException { | |
| Bucket bucket = StorageClient.getInstance().bucket(); | |
| if (StringUtils.isEmpty(name)) { | |
| throw new IOException("invalid file name"); | |
| } | |
| Blob blob = bucket.get(name); | |
| if (blob == null) { | |
| throw new IOException("file not found"); | |
| } | |
| blob.delete(); | |
| } | |
| @Data | |
| @Configuration | |
| @ConfigurationProperties(prefix = "firebase") | |
| public class Properties { | |
| private String bucketName; | |
| private String imageUrl; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.springframework.util.StringUtils; | |
| import org.springframework.web.multipart.MultipartFile; | |
| import javax.imageio.ImageIO; | |
| import java.awt.image.BufferedImage; | |
| import java.io.ByteArrayOutputStream; | |
| import java.io.IOException; | |
| import java.util.UUID; | |
| public interface IImageService { | |
| String getImageUrl(String name); | |
| String save(MultipartFile file) throws IOException; | |
| String save(BufferedImage bufferedImage, String originalFileName) throws IOException; | |
| void delete(String name) throws IOException; | |
| default String getExtension(String originalFileName) { | |
| return StringUtils.getFilenameExtension(originalFileName); | |
| } | |
| default String generateFileName(String originalFileName) { | |
| return UUID.randomUUID().toString() + getExtension(originalFileName); | |
| } | |
| default byte[] getByteArrays(BufferedImage bufferedImage, String format) throws IOException { | |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
| try { | |
| ImageIO.write(bufferedImage, format, baos); | |
| baos.flush(); | |
| return baos.toByteArray(); | |
| } catch (IOException e) { | |
| throw e; | |
| } finally { | |
| baos.close(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| * | |
| * | |
| * | |
| * | |
| * | |
| * | |
| @Autowired | |
| IImageService imageService; | |
| @PostMapping | |
| public ResponseEntity create(@RequestParam(name = "file") MultipartFile[] files) { | |
| for (MultipartFile file : files) { | |
| try { | |
| String fileName = imageService.save(file); | |
| String imageUrl = imageService.getImageUrl(fileName); | |
| // do whatever you want with that | |
| } catch (Exception e) { | |
| // throw internal error; | |
| } | |
| } | |
| return ResponseEntity.ok().build(); | |
| } | |
| * | |
| * | |
| * | |
| * | |
| * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| * | |
| * | |
| * | |
| * | |
| * | |
| <dependency> | |
| <groupId>com.google.firebase</groupId> | |
| <artifactId>firebase-admin</artifactId> | |
| <version>6.10.0</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.google.cloud</groupId> | |
| <artifactId>google-cloud-storage</artifactId> | |
| <version>1.96.0</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.google.api-client</groupId> | |
| <artifactId>google-api-client</artifactId> | |
| <version>1.30.4</version> | |
| </dependency> | |
| * | |
| * | |
| * | |
| * | |
| * |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment