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
|
const path = require('path')
const Minio = require('minio') const { promises } = require('fs')
const endPoint = "xxxx" const port = xxxx const accessKey = "xxxx" const secretKey = "xxxx" const bucketName = "typora" const filePath = new Date().getFullYear() + "-" + (new Date().getMonth() + 1) + "/" + new Date().getDate()
const parseArgv = () => { const imageList = process.argv.slice(2).map(u => path.resolve(u)) console.info("选择的文件列表:", imageList) return imageList }
const policy = () => { return { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": [ "*" ] }, "Action": [ "s3:GetBucketLocation", "s3:ListBucket", "s3:ListBucketMultipartUploads" ], "Resource": [ "arn:aws:s3:::" + bucketName ] }, { "Effect": "Allow", "Principal": { "AWS": [ "*" ] }, "Action": [ "s3:ListMultipartUploadParts", "s3:PutObject", "s3:AbortMultipartUpload", "s3:DeleteObject", "s3:GetObject" ], "Resource": [ "arn:aws:s3:::" + bucketName + "/*" ] } ] } }
const uploadImageFile = async (imageList = []) => { const minioClient = new Minio.Client({ endPoint: endPoint, port: port, useSSL: false, accessKey: accessKey, secretKey: secretKey }) minioClient.bucketExists(bucketName, function (err, exists) { if (!exists) { minioClient.makeBucket(bucketName, '', function (err) { if (err) throw err minioClient.setBucketPolicy(bucketName, JSON.stringify(policy()), function (err) { if (err) throw err }) }) } imageList.map(image => { const name = `${Date.now()}${path.extname(image)}` minioClient.fPutObject(bucketName, filePath + "/" + name, image, {}, function(info) { const url = `http://${endPoint}:${port}/${bucketName}/${filePath}/${name}` console.log(url) }) }) }) }
uploadImageFile(parseArgv())
|