Express picture upload to Qiuniuyun

Rely on

npm install qiniu -S

npm install formidable-S

Copy the code

Upload code

const express = require('express');

const Routers = express.Router();

var path = require('path')
const fs = require('fs')
// Get images and other files
var formidable = require('formidable');

// Time processing plug-in
const moment = require('moment');

// 7 ox cloud configuration
const qiniu = require('qiniu')

const accessKey = '* * * * * * * * * * *'  //AK see personal center Key management
const secretKey = '* * * * * * * * * *'   //SK see personal center Key Management for details
const bucket = '* * * * *'  // Upload the space name/ Qiuyun upload phones. post('/upload', (req,res) => {
    let form = new formidable.IncomingForm();
        form.parse(req, function (err, fields, filesa){

               	let MathRoundNumber = Math.round(Math.random()*100000)
                 let MathRound = moment().format("YYYY_MM_DD_hh_mm_ss")
		// As a personal preference, add the date in front of the file name to distinguish the time and also facilitate sorting
                let key = MathRound+MathRoundNumber+filesa.file.type 
		 // The name of the uploaded file

                let  path= filesa.file.path


               let mac = new qiniu.auth.digest.Mac(accessKey,secretKey)
               let options = {
                    scope: bucket,
                    expires: 3600 * 24
               }

                let putPolicy = new qiniu.rs.PutPolicy(options)
                let uploadToken = putPolicy.uploadToken(mac)

                uploadFile(uploadToken, key, path).then(idea= >{
                    res.json({
                        code: 0.msg: 'Upload successful'.data: {url: "http://img.abcdf.top/"+idea.key  // Here to join your domain name, please add and resolve in the domain name provider
                        }
                    })
                })
                .catch(err= >{
                    // For the sake of strict control
                    res.json({
                        code: 4.msg: 'Upload failed'.data: {url: "http://img.abcdf.top/"+err.key  // Here to join your domain name, please add and resolve in the domain name provider}})})// Construct the upload function
              async  function uploadFile(uptoken, key, localFile) {
             
                     var config = new qiniu.conf.Config();
                        // Space corresponding to the machine room
                        config.zone = qiniu.zone.Zone_z0;
                    var formUploader = new qiniu.form_up.FormUploader(config);
                    var putExtra = new qiniu.form_up.PutExtra();
                    return  new Promise((resolve,reject) = >{
                        // File upload
                            formUploader.putFile(uptoken, key, localFile, putExtra, function(respErr, respBody, respInfo) {
                              if (respErr) {
                                throw respErr;
                              }
                              if (respInfo.statusCode == 200) {
                                resolve(respBody)
                              } else {
                                reject(respBody)  
				// For the sake of strict control}})})}})Copy the code