第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Java 中將圖像/文件上傳到 Firebase 存儲?

如何在 Java 中將圖像/文件上傳到 Firebase 存儲?

藍山帝景 2021-12-01 16:33:51
我正在處理一個功能,我需要使用 java 將圖像/文件上傳到 firebase 存儲并將其公開為 API。我已經(jīng)在 angular 4 打字稿中實現(xiàn)了這個功能。但是現(xiàn)在我需要這個方法作為 Java Rest API,這樣我的同行也可以使用相同的方法而不是編寫一個新的方法。那么是否有任何 API 或方法可以將圖像寫入 firebase 存儲?
查看完整描述

3 回答

?
忽然笑

TA貢獻1806條經(jīng)驗 獲得超5個贊

嘗試這個:


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"));



查看完整回答
反對 回復 2021-12-01
?
GCT1015

TA貢獻1827條經(jīng)驗 獲得超4個贊

如果 Java 項目在受信任的環(huán)境中運行(例如他們的開發(fā)機器、您控制的服務器或 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));


查看完整回答
反對 回復 2021-12-01
?
慕森卡

TA貢獻1806條經(jīng)驗 獲得超8個贊

如果你使用的是 Spring Boot,你可以試試這個:


創(chuàng)建一個類以將其公開為 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;


@RestController

public 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();

    }

}

創(chuàng)建一個服務以將圖像上傳到 firebase 存儲。

@Service

public 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>


查看完整回答
反對 回復 2021-12-01
  • 3 回答
  • 0 關注
  • 196 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號