12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- /*
- * @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('../package.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) {
- debugger
- 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/cdfg/editor',
- },
- ].forEach(item => {
- list_files(toPath(path.join(__dirname, item.dir)), filepath => {
- let temppath = filepath.replace(/\\/g, '/')
- let keyspath = item.key + temppath.split(item.dir.replace(/\.\.\//g, ''))[1]
- if (item.sdk) {
- keyspath = item.key + '/' + package.version + temppath.split(item.dir)[1]
- }
- upload(filepath, keyspath, 'max-age=3153600') // 缓存30天
- })
- })
- }
- uploads()
|