123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- const OSS = require('ali-oss');
- const fs = require('fs');
- const path = require('path');
- const { REACT_APP_ENV = 'dev' } = process.env;
-
- let bucket = REACT_APP_ENV === 'dev' ? 'zx-web-book-manage-test' : '';
- var client = new OSS({
- region: 'oss-cn-hangzhou',
- accessKeyId: 'LTAI5tBcAkDx7i9PR8uvjqxN',
- accessKeySecret: 'tWjouJKsb7ApTJLKVebxQFod6GYbOL',
- bucket,
- });
- async function deleteAllFiles() {
- try {
- let result;
-
- do {
- result = await client.list({
- 'max-keys': 1000,
- });
- if (result.objects && result.objects.length) {
- const keys = result.objects.map((item) => item.name);
- console.log('Deleting files:', keys);
-
- await client.deleteMulti(keys);
- }
- } while (result.isTruncated);
- console.log(`已删除bucket:${bucket}内的全部文件`);
- } catch (error) {
- console.error(`删除bucket:${bucket}内的全部文件错误:`, error);
- }
- }
- async function uploadFilesFromDist(dirPath, ossPath) {
- const files = fs.readdirSync(dirPath);
- for (const file of files) {
- const localFilePath = path.join(dirPath, file);
- const ossFilePath = path.join(ossPath, file).replace(/\\/g, '/');
- const stats = fs.statSync(localFilePath);
- if (stats.isDirectory()) {
-
- await uploadFilesFromDist(localFilePath, ossFilePath);
- } else {
-
- console.log(`正在上传: ${localFilePath} 到 ${ossFilePath}`);
- await client.put(ossFilePath, localFilePath);
- }
- }
- }
- (async () => {
- if(!bucket){
- return
- }
- await deleteAllFiles();
- const distPath = path.join(__dirname, '../dist');
- await uploadFilesFromDist(distPath, '');
- })();
|