Get Download URL from file uploaded with Cloud Functions for Firebase

后端 未结 23 2264
春和景丽
春和景丽 2020-11-22 01:19

After uploading a file in Firebase Storage with Functions for Firebase, I\'d like to get the download url of the file.

I have this :

...

return buck         


        
23条回答
  •  暖寄归人
    2020-11-22 02:00

    I already post my ans... in below URL Where you can get full code with solution

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

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

提交回复
热议问题