如何在 Java 中将图像/文件上传到 Firebase 存储?

我正在处理一个功能,我需要使用 java 将图像/文件上传到 firebase 存储并将其公开为 API。我已经在 angular 4 打字稿中实现了这个功能。但是现在我需要这个方法作为 Java Rest API,这样我的同行也可以使用相同的方法而不是编写一个新的方法。那么是否有任何 API 或方法可以将图像写入 firebase 存储?


蓝山帝景
浏览 168回答 3
3回答

忽然笑

尝试这个:FirebaseOptions options = FirebaseOptions.builder()                    .setCredentials(credential)                    .setDatabaseUrl(projectUrl)                    .setStorageBucket("YOUR BUCKET LINK")                    .build();    FirebaseApp fireApp = FirebaseApp.initializeApp(options);    StorageClient storageClient = StorageClient.getInstance(fireApp);            InputStream testFile = new FileInputStream("YOUR FILE PATH");            String blobString = "NEW_FOLDER/" + "FILE_NAME.EXT";            storageClient.bucket().create(blobString, testFile , Bucket.BlobWriteOption.userProject("YOUR PROJECT ID"));

GCT1015

如果 Java 项目在受信任的环境中运行(例如他们的开发机器、您控制的服务器或 Cloud Functions),他们可以使用 Firebase Admin SDK 访问 Cloud Storage。请参阅Firebase Admin SDK 文档了解如何开始,然后参阅适用于 Java 客户端的Google Cloud Storage 文档了解更多信息。具体看一下Java上传文件的示例:BlobId blobId = BlobId.of("bucket", "blob_name");BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));

慕森卡

如果你使用的是 Spring Boot,你可以试试这个:创建一个类以将其公开为 API 中的 Web 服务:import com.yourcompany.yourproject.services.FirebaseFileService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;@RestControllerpublic class ResourceController {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private FirebaseFileService firebaseFileService;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; @PostMapping("/api/v1/test")&nbsp; &nbsp; public ResponseEntity create(@RequestParam(name = "file") MultipartFile file) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fileName = firebaseFileService.saveTest(file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do whatever you want with that&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; throw internal error;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return ResponseEntity.ok().build();&nbsp; &nbsp; }}创建一个服务以将图像上传到 firebase 存储。@Servicepublic class FirebaseFileService {&nbsp; &nbsp; private Storage storage;&nbsp; &nbsp; @EventListener&nbsp; &nbsp; public void init(ApplicationReadyEvent event) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ClassPathResource serviceAccount = new ClassPathResource("firebase.json");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; storage = StorageOptions.newBuilder().&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setCredentials(GoogleCredentials.fromStream(serviceAccount.getInputStream())).&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setProjectId("YOUR_PROJECT_ID").build().getService();&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public String saveTest(MultipartFile file) throws IOException{&nbsp; &nbsp; &nbsp; &nbsp; String imageName = generateFileName(file.getOriginalFilename());&nbsp; &nbsp; &nbsp; &nbsp; Map<String, String> map = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; map.put("firebaseStorageDownloadTokens", imageName);&nbsp; &nbsp; &nbsp; &nbsp; BlobId blobId = BlobId.of("YOUR_BUCKET_NAME", imageName);&nbsp; &nbsp; &nbsp; &nbsp; BlobInfo blobInfo = BlobInfo.newBuilder(blobId)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setMetadata(map)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setContentType(file.getContentType())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; &nbsp; &nbsp; storage.create(blobInfo, file.getInputStream());&nbsp; &nbsp; &nbsp; &nbsp; return imageName;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; private String generateFileName(String originalFileName) {&nbsp; &nbsp; &nbsp; &nbsp; return UUID.randomUUID().toString() + "." + getExtension(originalFileName);&nbsp; &nbsp; }&nbsp; &nbsp; private String getExtension(String originalFileName) {&nbsp; &nbsp; &nbsp; &nbsp; return StringUtils.getFilenameExtension(originalFileName);&nbsp; &nbsp; }}请注意,您需要下载 Firebase 配置文件并将其存储为 src/main/resources 文件夹下的“firebase.json”。 https://support.google.com/firebase/answer/7015592?hl=en您还需要添加 Maven 依赖项:<dependency>&nbsp; &nbsp; <groupId>com.google.firebase</groupId>&nbsp; &nbsp; <artifactId>firebase-admin</artifactId>&nbsp; &nbsp; <version>6.14.0</version></dependency>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java