123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- /*
- * @Author: Rindy
- * @Date: 2020-03-13 10:56:21
- * @LastEditors: Rindy
- * @LastEditTime: 2022-05-26 10:20:55
- * @Description: 静态文件上传
- */
- const { S3 } = require('@aws-sdk/client-s3')
- const fs = require('fs')
- const path = require('path')
- const mime = require('mime')
- const package = require('../lerna.json')
- process.env.AWS_ACCESS_KEY_ID = 'AKIAWCV5QFZ34YYVET2Q'
- process.env.AWS_SECRET_ACCESS_KEY = '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'
- const client = new S3({ region: 'eu-west-2' })
- const toPath = dist => path.resolve(dist)
- async function upload(filepath, key, cache, retry = 0) {
- fs.readFile(filepath, (err, fileData) => {
- if (err) {
- console.log('上传失败:' + filepath)
- throw err
- }
- client.putObject(
- {
- Bucket: '4dkankan',
- Key: key,
- Body: fileData,
- ContentType: mime.getType(filepath),
- },
- (err, data) => {
- if (err) {
- if (++retry < 3) {
- return upload(filepath, key, cache, retry)
- }
- console.log('上传失败:' + filepath)
- throw err
- }
- console.log('上传成功:' + key)
- }
- )
- })
- }
- function list_files(dir, callback) {
- fs.readdir(dir, (err, files) => {
- if (err) {
- throw err
- }
- files.forEach(async file => {
- let filepath = path.join(dir, file)
- const state = fs.statSync(filepath)
- if (state.isDirectory()) {
- list_files(filepath, callback)
- } else {
- if (filepath.endsWith('.map') == false) {
- callback(filepath)
- }
- }
- })
- })
- }
- function uploads() {
- ;[
- {
- dir: 'dist/editor/editor',
- key: 'v4/www/editor',
- },
- {
- dir: 'dist/viewer/viewer',
- key: 'v4/www/viewer',
- },
- {
- dir: 'dist/sdk',
- key: 'v4/www/sdk',
- },
- {
- dir: 'dist/sdk',
- key: 'v4/sdk',
- sdk: true,
- },
- ].forEach(item => {
- list_files(toPath(item.dir), filepath => {
- let temppath = filepath.replace(/\\/g, '/')
- let keyspath = item.key + temppath.split(item.dir)[1]
- if (item.sdk) {
- keyspath = item.key + '/' + package.version + temppath.split(item.dir)[1]
- }
- upload(filepath, keyspath, 'max-age=3153600') // 缓存30天
- })
- })
- }
- uploads()
|