简体   繁体   中英

how to upload an image to firebase with rest api

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)
    }
}

currently I am using this kind of code to update data to a collection database firestore, now I am wondering how to best post pictures.

the Idea user gives an image, colour, description then uploads it to the firebase. currently my image is just a string which refers to an already available image in the folder of my own website, but that's of course not possible with new images.

any ideas? love to hear it.

ps. for the school project we have to use an api to do this

There is no specific REST API for uploading files to Cloud Storage Firebase. The only option to upload through Firebase is to use one of the SDKs. And the only option for uploading through a REST API is to use the REST API for Cloud Storage.

From the documentation on uploading a file through the REST API using 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]"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM