【问题标题】:how to upload an image to firebase with rest api如何使用rest api将图像上传到firebase
【发布时间】:2021-12-16 12:41:49
【问题描述】:
import {Response} from 'express';
import {db} from './config/firebase'

type EntryType = {
    image: string,
    kleur: string,
    beschrijving: string
}

type Request = {
    body: EntryType,
    params: {entryId:string}
}

const addArt = async(req:Request, res:Response)=>{
    const {image, kleur,beschrijving} = req.body
    try{
        const entry = db.collection('arts').doc()
        const entryObject = {
            id: entry.id,
            image,
            kleur,
            beschrijving
        }

        entry.set(entryObject)

        res.status(200).send({
            status: 'succes',
            message: 'entry addes succesfully',
            data: entryObject
        })
    }catch(error){
        res.status(500).json(error)
    }
}

目前我正在使用这种代码将数据更新到集合数据库firestore,现在我想知道如何最好地发布图片。

Idea 用户提供图像、颜色、描述,然后将其上传到 Firebase。 目前我的图片只是一个字符串,它引用了我自己网站文件夹中已经可用的图片,但对于新图片当然不可能。

有什么想法吗?喜欢听。

ps。对于学校项目,我们必须使用 api 来执行此操作

【问题讨论】:

    标签: typescript firebase api google-cloud-firestore


    【解决方案1】:

    没有用于将文件上传到 Cloud Storage Firebase 的特定 REST API。通过 Firebase 上传的唯一选项是使用其中一个 SDK。而通过 REST API 上传的唯一选择是使用 REST API for Cloud Storage。

    来自uploading a file through the REST API 使用 CURL 的文档:

    curl -X POST --data-binary @[OBJECT_LOCATION] \
    -H "Authorization: Bearer [OAUTH2_TOKEN]" \
    -H "Content-Type: [OBJECT_CONTENT_TYPE]" \
    "https://storage.googleapis.com/upload/storage/v1/b/[BUCKET_NAME]/o?uploadType=media&name=[OBJECT_NAME]"
    

    【讨论】:

      猜你喜欢
      • 2020-11-26
      • 1970-01-01
      • 2015-01-10
      • 2014-06-01
      • 2021-07-30
      • 1970-01-01
      • 2018-12-22
      • 2021-01-24
      • 2021-05-16
      相关资源
      最近更新 更多