-
忽然笑
尝试这个: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 { @Autowired private FirebaseFileService firebaseFileService; @PostMapping("/api/v1/test") public ResponseEntity create(@RequestParam(name = "file") MultipartFile file) { try { String fileName = firebaseFileService.saveTest(file); // do whatever you want with that } catch (Exception e) { // throw internal error; } return ResponseEntity.ok().build(); }}创建一个服务以将图像上传到 firebase 存储。@Servicepublic class FirebaseFileService { private Storage storage; @EventListener public void init(ApplicationReadyEvent event) { try { ClassPathResource serviceAccount = new ClassPathResource("firebase.json"); storage = StorageOptions.newBuilder(). setCredentials(GoogleCredentials.fromStream(serviceAccount.getInputStream())). setProjectId("YOUR_PROJECT_ID").build().getService(); } catch (Exception ex) { ex.printStackTrace(); } } public String saveTest(MultipartFile file) throws IOException{ String imageName = generateFileName(file.getOriginalFilename()); Map<String, String> map = new HashMap<>(); map.put("firebaseStorageDownloadTokens", imageName); BlobId blobId = BlobId.of("YOUR_BUCKET_NAME", imageName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId) .setMetadata(map) .setContentType(file.getContentType()) .build(); storage.create(blobInfo, file.getInputStream()); return imageName; } private String generateFileName(String originalFileName) { return UUID.randomUUID().toString() + "." + getExtension(originalFileName); } private String getExtension(String originalFileName) { return StringUtils.getFilenameExtension(originalFileName); }}请注意,您需要下载 Firebase 配置文件并将其存储为 src/main/resources 文件夹下的“firebase.json”。 https://support.google.com/firebase/answer/7015592?hl=en您还需要添加 Maven 依赖项:<dependency> <groupId>com.google.firebase</groupId> <artifactId>firebase-admin</artifactId> <version>6.14.0</version></dependency>