1、config.js
module.exports = {
//这里不能带http或者https
endPoint: 'minio-spider-images.lan',
port: 9000,
useSSL: false, //不需要https
accessKey: 'G2ga7dnJxg7M24C8',
secretKey: 'ch7by1y17ytbXZOowW9PMz2lF69mzElX'
}
/**
* @description split_range:截取源文件的名称,作为下载后的文件名称,可以为空或者两个数字,两个数字的长度不能超过源文件的名称的长度
* @description download_ext:需要下载的文件后缀
* @description file_path:下载后的文件存储的目录
* @description exclude_ext:需要排除下载的文件格式,与download_ext冲突,两者只能存在一个有值
* @description bucket:mio中需要下载的bucketname,不能为空
* @description prefix:mio中bucketname下一级目录,为空则表示所有
* example:
* split_range:[1,10] 表示从前截取1位,从后截取10位,拼接成下载后文件的名称
* download_ext:['.json','.docx'] 表示需要下载json格式和docx格式的文件,与exclude_ext冲突,仅可有一个字段有值,或者都为空,为空则下载所有
* file_path:'/temp/download' 表示下载后的文件存储的在/temp/download位置,如果没有该目录,则会新建
* exclude_ext:['.docx'] 表示下载非docx格式的文件,与download_ext冲突,仅可有一个字段有值,或者都为空,为空则下载所有
* bucket:'fileinspection' 表示从mio的名称为fileinspection的buckets中下载文件
* @type {{bucket: string, file_path: string, split_range: *[], download_ext: string[], prefix: string, exclude_ext: *[]}}
*/
module.exports.setting = {
split_range: [],
download_ext: [],
file_path: '/temp000/hello/',
exclude_ext: ['.txt'],
bucket:'file',
prefix:''
}
2、run.js
const config = require('./config')
const Minio = require('minio')
let connect = (config, client) => {
return new Promise((resolve, reject) => {
let bucket_name = null
client.listBuckets(function (err, buckets) {
if (err) return console.log(err)
for (let i in buckets) {
if (buckets.hasOwnProperty(i)) {
if (config.setting.bucket !== '') {
if (buckets[i]['name'] === `${config.setting.bucket}`) {
bucket_name = buckets[i]['name']
break
}
} else {
throw new Error("the value of [config.setting.bucket] can't be empty")
}
}
}
resolve(bucket_name)
})
})
}
let listObject = (bucket_name, prefix, client) => {
return new Promise((resolve, reject) => {
let list = []
let stream = client.listObjectsV2(`${bucket_name}`, prefix, true, '')
stream.on('data', function (obj) {
list.push(obj)
})
stream.on('error', function (err) {
console.log(err)
})
stream.on('end', function (obj) {
resolve(list)
})
})
}
let download = (bucket_name, object_name, path, client) => {
return new Promise((resolve, reject) => {
client.fGetObject(bucket_name, object_name, path, function (err) {
if (err) {
return console.log(err)
}
console.log(`downloading ${object_name}`)
resolve('success')
})
})
}
(async () => {
let s3Client = new Minio.Client(config);
let bucket = await connect(config, s3Client)
let list = await listObject(bucket, config.setting.prefix, s3Client)
for (let i of list) {
let length = i['name'].length;
let start;
let end;
if (config.setting.split_range.length === 0) {
start = 0;
end = 0
} else {
start = config.setting.split_range[0];
end = config.setting.split_range[1];
}
if (start > length || end > length) {
throw new Error('the value of config.setting.split_range exceeds the length of filename')
}
let filepath = config.setting.file_path;
if (config.setting.download_ext.length !== 0) {
for (let j in config.setting.download_ext) {
if (config.setting.download_ext.hasOwnProperty(j)) {
if (i['name'].endsWith(config.setting.download_ext[j])) {
await download(bucket, i['name'], `${filepath}${i['name'].slice(0, start)}${i['name'].slice(-end)}`, s3Client)
}
}
}
} else if (config.setting.download_ext.length === 0 && config.setting.exclude_ext.length !== 0) {
for (let j in config.setting.exclude_ext) {
if (config.setting.exclude_ext.hasOwnProperty(j)) {
if (!i['name'].endsWith(config.setting.exclude_ext[j])) {
await download(bucket, i['name'], `${filepath}${i['name'].slice(0, start)}${i['name'].slice(-end)}`, s3Client)
}
}
}
} else if (config.setting.download_ext.length === 0 && config.setting.exclude_ext.length === 0) {
await download(bucket, i['name'], `${filepath}${i['name'].slice(0, start)}${i['name'].slice(-end)}`, s3Client)
} else {
throw new Error('one of config.setting.download_ext and config.setting.exclude_ext must be empty')
}
}
})()