This is the seventh day of my participation in the August More text Challenge. For details, see: August More Text Challenge

preface

The full name of the CDN is Content Delivery Network. The basic idea is to avoid the bottlenecks and links that may affect the speed and stability of data transmission on the Internet as far as possible, so that the content transmission is faster and more stable. Through the server placed throughout the network node of a layer of intelligence on the basis of the existing Internet virtual network, CDN system can in real time according to the network traffic and each node connections, load condition and to the user’s distance and comprehensive information such as response time will the user’s request to guide users closest service node. Its purpose is to enable users to obtain the required content nearby, solve the Internet network congestion, improve the response speed of users to visit the website.

Deploy static resources to SDN using the Node + Qiniu SDK with one click

Preparation Environment configuration

element

Node (my version V8.11.2)

NPM (my version V6.1.0)


NPM component package Colors and Qiniu

Node methods fs, PATH, readline, console


instructions

Readline is used to read data from a readable stream, such as Process.stdin, one line at a time

Colors sets colors and styles in the console

Fs File system operation documentation

The Console module provides a simple debugging console, similar to the JavaScript console provided by Web browsers. documentation

The path module provides utility functions to handle path documentation for files and directories

Qiniu is available in the Qiniu Cloud SDK documentation for Node.js


Other packages except Qiniu are installed by default
Execute the command
npm install qiniu
Copy the code

Design ideas

upload
Using the file processing mechanism of Node, get all files in the specified directory, and upload them to qiniuSDk storage via qiniuSDkCopy the code
Batch update
Use qiniuSDK to obtain the remote specified bucket and the file list under a certain prefix, and then upload all the local filesCopy the code

The specific implementation

Create the qiniu.js file

1. Introduce needed dependencies

const readline = require('readline') const colors = require( "colors"); Const QiNiu = require(' FS ') const Join = require('path').Copy the code

2. Perform initial configuration

Seven cattle secret key

const accessKey = '*******'; Const secretKey = '*******'; Var var var var var var var var var var var var var var var var var var var var var var var var var var var var var var UploadNore = ['index.html'] // Ignore the array of files (can be files or folders) // Authentication object const MAC = new QiNiu.auth.digest.Mac(accessKey, secretKey); Const config = new qiniu-conf.config (); // whether to useHttpsDomain name // config.usehttpsdomain = true; Config. useCdnDomain = true; Zone_z0(East China) config.zone = qiniu-zone.zone_z0; Const BucketManager = new QiNiu.rs.bucketManager (MAC, config); SetTheme ({silly: 'rainbow', input: 'grey', verbose: 'cyan', prompt: 'grey', info: 'green', data: 'grey', help: 'cyan', warn: 'yellow', debug: 'blue', error: 'red' }); //Copy the code

3. Define related methods

3.1 Obtaining the specified space name and prefix of Remote Qiniu

Async function getQiniuList() {var options = {var options = {var options = {var options = {}} limit: limit, prefix: Prefix,} var array = [] var list = await getList() Async function getList(mark=false) {if(mark){var options = {limit: options.limit, prefix: options.prefix, mark: mark } } return new Promise(function(resolve, reject){ bucketManager.listPrefix(bucket, options, function(err, respBody, respInfo) { if (err) { console.log(err); throw err; } if (respinfo.statuscode == 200) {// If this nextMarker is not empty, there is still a list of unlisted files. Next time I call listPrefix, Var nextMarker = respbody.marker; var commonPrefixes = respBody.commonPrefixes; var items = respBody.items; items.forEach(function(item) { array.push(QiNiu.rs.deleteOp(bucket, item.key)) }); if(respBody.marker){ getList(respBody.marker) } else{ resolve(array) } } else { console.log(respInfo.statusCode); console.log(respBody); }}); }) } return list }Copy the code

3.2 Deleting the remote Qiniu list data

Async function delAll(){async function delQiniuAll() {return new Promise(function(resolve, Reject){// get the remote list data getQiniuList().then(res => {if (res.length! ==0){console.log(' remote list is empty '.debug); del(res, resolve) } else { resolve() } }) }) } await delQiniuAll() } function del(deleteOperations, resolve) { bucketManager.batch(deleteOperations, function(err, respBody, respInfo) { if (err) { console.log(err); //throw err; } else { // 200 is success, 298 is part success if (parseInt(respInfo.statusCode / 100) == 2) { respBody.forEach(function(item, Index) {if (item.code == 200) {resolve(index) console.log(' delete successfully '+'第'+(parseInt(index)+1)+' file '.info)} else { Console. log(' deletion failed '.error); console.log(item.code + "\t" + item.data.error.error); resolve(index) } }); } else { console.log(respInfo.deleteusCode); console.log(respBody); }}}); }Copy the code

3.3 Uploading a local file to Qiniu

Function upAllToQiniu(){console.log(' delete 7 cow remote resource list when on '.debug); DelAll ().then(res => {console.log(' upload resources to qnix on open '.debug); var files = FS.readdirSync('dist/'); Var localFile = findSync('dist/') var localFile = findSync('dist/') var localFile = findSync('dist/') upOneToQiniu(localFile[key], Function upOneToQiniu(localFile)}})} function upOneToQiniu(localFile, key) { var mac = new QiNiu.auth.digest.Mac(accessKey, secretKey); var options = { scope: bucket, }; var putPolicy = new QiNiu.rs.PutPolicy(options); var uploadToken = putPolicy.uploadToken(mac); Var uploader = new qiniu-form_up.formUploader (config) var putExtra = new qiniu-form_up.putextra () // Upload file formUploader.putFile(uploadToken, key, localFile, putExtra, function(respErr, respBody, respInfo) { if (respErr) { throw respErr } if (respInfo.statusCode == 200) { The console. The log (localFile. Info + '= >' + respBody. Key. The info + 'upload success')} else {the console. The log (' upload failure '+ respInfo. StatusCode. Error); Console. log(' failed to upload '+ respbody.error)}})} // Get the file directory path startPath Root directory name function findSync(startPath) {let targetObj={}; Function finder(path) {let files= fs.readdirSync (path); ForEach ((val,index) => {let fPath=Join(path,val); let stats=FS.statSync(fPath); if(stats.isDirectory()) { finder(fPath); } if(stats.isFile() && isNore(fPath)) { targetObj[fPath.replace(startPath, prefix)] = fPath; }}); } finder(startPath); return targetObj; } /** * Check whether the current path is in the ignore file array * @param {String} path */ function isNore(path) {for(var item of uploadNore) {// Check whether the current path is in the ignore file array if (path.indexOf(item) ! == -1) { return false } } return true }Copy the code

4. Process control

// The process object is a global variable that provides information about the current Node.js process and controls the current node.js process without using require() because it is a global variable. Var rl = readline.createInterface({input: process.stdin, prompt: process.stdout, prompt: {input: process.stdout, prompt: {input: process. Warn}); rl.prompt(); // Fires the 'line' event every time the input stream receives a receiving line terminator (\n, \r, or \r\n). Usually occurs when the user presses the <Enter> key or the <Return> key. The listener function is called with a string containing the received line of input. Rl.on ('line', (line) => {switch (line.trim()) {case 'y': case 'y': console.log(' start remote deployment '.help); UpAllToQiniu () rl.close(); break; Case 'n': case 'n': console.log(' you cancelled remote deployment '.help); rl.close(); break; Default: console.log(' you typed: '${line.trim()}' is invalid, please re-enter '.warn); rl.prompt(); break; }})Copy the code

Finally, run node build/qiniu. Js in the terminal or add it in the package.json scripts item

"qiniu": "node build/qiniu.js",
Copy the code

Then run NPM run qiniu