How do I upload a base64 encoded image (string) directly to a Google Cloud Storage bucket using Node.js?

前端 未结 6 1743
野性不改
野性不改 2020-11-30 04:51

Currently, I am using the @google-cloud/storage NPM package to upload a file directly to a Google Cloud Storage bucket. This requires some trickery as I only have the image\

6条回答
  •  爱一瞬间的悲伤
    2020-11-30 05:19

    :) what an issue !! Have tried it and got the issue Image has uploaded on firebase Storage but not download and just loader is moving around and around... After spending time... Got the success to upload the image on firebase storage with downloading... There was an issue in an access token...

    check the screenshot
    

    If you check in the file location section on the right side bottom there is an option "create access token" and not showing any "access token" on there if you create manually access token on there then refresh the page image will showing... So now the question is how to create it by code...

    just use below code to create the access token

    const uuidv4 = require('uuid/v4');
    const uuid = uuidv4();
    metadata: { firebaseStorageDownloadTokens: uuid }
    

    Full code is given below for uploading an image to storage image on firebase storage

    const functions = require('firebase-functions')
    var firebase = require('firebase');
    var express = require('express');
    var bodyParser = require("body-parser");
    

    const uuidv4 = require('uuid/v4');
    const uuid = uuidv4();
    
        const os = require('os')
        const path = require('path')
        const cors = require('cors')({ origin: true })
        const Busboy = require('busboy')
        const fs = require('fs')
        var admin = require("firebase-admin");
    
    
        var serviceAccount = {
            "type": "service_account",
            "project_id": "xxxxxx",
            "private_key_id": "xxxxxx",
            "private_key": "-----BEGIN PRIVATE KEY-----\jr5x+4AvctKLonBafg\nElTg3Cj7pAEbUfIO9I44zZ8=\n-----END PRIVATE KEY-----\n",
            "client_email": "xxxx@xxxx.iam.gserviceaccount.com",
            "client_id": "xxxxxxxx",
            "auth_uri": "https://accounts.google.com/o/oauth2/auth",
            "token_uri": "https://oauth2.googleapis.com/token",
            "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
            "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-5rmdm%40xxxxx.iam.gserviceaccount.com"
          }
    
        admin.initializeApp({
            credential: admin.credential.cert(serviceAccount),
            storageBucket: "xxxxx-xxxx" // use your storage bucket name
        });
    
    
        const app = express();
        app.use(bodyParser.urlencoded({ extended: false }));
        app.use(bodyParser.json());
    app.post('/uploadFile', (req, response) => {
        response.set('Access-Control-Allow-Origin', '*');
        const busboy = new Busboy({ headers: req.headers })
        let uploadData = null
        busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
            const filepath = path.join(os.tmpdir(), filename)
            uploadData = { file: filepath, type: mimetype }
            console.log("-------------->>",filepath)
            file.pipe(fs.createWriteStream(filepath))
          })
    
          busboy.on('finish', () => {
            const bucket = admin.storage().bucket();
            bucket.upload(uploadData.file, {
                uploadType: 'media',
                metadata: {
                  metadata: { firebaseStorageDownloadTokens: uuid,
                    contentType: uploadData.type,
                  },
                },
              })
    
              .catch(err => {
                res.status(500).json({
                  error: err,
                })
              })
          })
          busboy.end(req.rawBody)
       });
    
    
    
    
    exports.widgets = functions.https.onRequest(app);
    

提交回复
热议问题