1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| const fs = require('fs'); const join = require('path').join;
let OSS = require('ali-oss'); let STS = OSS.STS; let sts = new STS({ accessKeyId: 'LTA&*******5p', accessKeySecret: 'VNbvLHs**********j2vPsK' });
const BucketName = 'demo-demo-demo'; const Region = 'oss-cn-shanghai***'; const Ram = 'acs:ram::185190330623****:role/aliyunoss***'; const BaseDir = 'dist';
sts.assumeRole(Ram).then((token) => { return new OSS({ region: Region, accessKeyId: token.credentials.AccessKeyId, accessKeySecret: token.credentials.AccessKeySecret, stsToken: token.credentials.SecurityToken }); }).then((client) => { client.listBuckets().then((buckets) => { let flag = false; if (buckets.buckets) { for (let i = 0, j = buckets.buckets.length; i < j; i++) { if (buckets.buckets[i].name === BucketName) { flag = true; } } } if (flag) { client.putBucketACL(BucketName, 'public-read').then(() => { client.putBucketWebsite(BucketName, {index: 'index.html'}); client.useBucket(BucketName); client.list().then((res) => { if (res.objects && res.objects.length > 0) { for (let i = 0, j = res.objects.length; i < j; i++) { client.delete(res.objects[i].name) } } }).then(() => { function getJsonFiles(jsonPath) { let jsonFiles = [];
function findJsonFile(path) { let files = fs.readdirSync(path); files.forEach(function (item, index) { let fPath = join(path, item); let stat = fs.statSync(fPath); if (stat.isDirectory() === true) { findJsonFile(fPath); } if (stat.isFile() === true) { jsonFiles.push(fPath); } }); }
findJsonFile(jsonPath); return jsonFiles; }
let filesArr = getJsonFiles("./" + BaseDir); for (let i = 0, j = filesArr.length; i < j; i++) { client.put(filesArr[i].replace('dist/', ''), filesArr[i]).catch((err) => { console.log('errors:-------\n' + err); }); } }); }); } else { client.putBucket(BucketName).then(() => { client.putBucketACL(BucketName, 'public-read').then(() => { client.putBucketWebsite(BucketName, {index: 'index.html'}); client.useBucket(BucketName);
function getJsonFiles(jsonPath) { let jsonFiles = [];
function findJsonFile(path) { let files = fs.readdirSync(path); files.forEach(function (item, index) { let fPath = join(path, item); let stat = fs.statSync(fPath); if (stat.isDirectory() === true) { findJsonFile(fPath); } if (stat.isFile() === true) { jsonFiles.push(fPath); } }); }
findJsonFile(jsonPath); return jsonFiles; }
let filesArr = getJsonFiles("./" + BaseDir); for (let i = 0, j = filesArr.length; i < j; i++) { client.put(filesArr[i].replace('dist/', ''), filesArr[i]).catch((err) => { console.log('errors:-------\n' + err); }); } }); }) } }); }).catch((res) => { console.log(res); });
|