• 4.7 minio下载文件代码优化


    1、config.js

    1. module.exports = {
    2. //这里不能带http或者https
    3. endPoint: 'minio-spider-images.lan',
    4. port: 9000,
    5. useSSL: false, //不需要https
    6. accessKey: 'G2ga7dnJxg7M24C8',
    7. secretKey: 'ch7by1y17ytbXZOowW9PMz2lF69mzElX'
    8. }
    9. /**
    10. * @description split_range:截取源文件的名称,作为下载后的文件名称,可以为空或者两个数字,两个数字的长度不能超过源文件的名称的长度
    11. * @description download_ext:需要下载的文件后缀
    12. * @description file_path:下载后的文件存储的目录
    13. * @description exclude_ext:需要排除下载的文件格式,与download_ext冲突,两者只能存在一个有值
    14. * @description bucket:mio中需要下载的bucketname,不能为空
    15. * @description prefix:mio中bucketname下一级目录,为空则表示所有
    16. * example:
    17. * split_range:[1,10] 表示从前截取1位,从后截取10位,拼接成下载后文件的名称
    18. * download_ext:['.json','.docx'] 表示需要下载json格式和docx格式的文件,与exclude_ext冲突,仅可有一个字段有值,或者都为空,为空则下载所有
    19. * file_path:'/temp/download' 表示下载后的文件存储的在/temp/download位置,如果没有该目录,则会新建
    20. * exclude_ext:['.docx'] 表示下载非docx格式的文件,与download_ext冲突,仅可有一个字段有值,或者都为空,为空则下载所有
    21. * bucket:'fileinspection' 表示从mio的名称为fileinspection的buckets中下载文件
    22. * @type {{bucket: string, file_path: string, split_range: *[], download_ext: string[], prefix: string, exclude_ext: *[]}}
    23. */
    24. module.exports.setting = {
    25. split_range: [],
    26. download_ext: [],
    27. file_path: '/temp000/hello/',
    28. exclude_ext: ['.txt'],
    29. bucket:'file',
    30. prefix:''
    31. }

    2、run.js

    1. const config = require('./config')
    2. const Minio = require('minio')
    3. let connect = (config, client) => {
    4. return new Promise((resolve, reject) => {
    5. let bucket_name = null
    6. client.listBuckets(function (err, buckets) {
    7. if (err) return console.log(err)
    8. for (let i in buckets) {
    9. if (buckets.hasOwnProperty(i)) {
    10. if (config.setting.bucket !== '') {
    11. if (buckets[i]['name'] === `${config.setting.bucket}`) {
    12. bucket_name = buckets[i]['name']
    13. break
    14. }
    15. } else {
    16. throw new Error("the value of [config.setting.bucket] can't be empty")
    17. }
    18. }
    19. }
    20. resolve(bucket_name)
    21. })
    22. })
    23. }
    24. let listObject = (bucket_name, prefix, client) => {
    25. return new Promise((resolve, reject) => {
    26. let list = []
    27. let stream = client.listObjectsV2(`${bucket_name}`, prefix, true, '')
    28. stream.on('data', function (obj) {
    29. list.push(obj)
    30. })
    31. stream.on('error', function (err) {
    32. console.log(err)
    33. })
    34. stream.on('end', function (obj) {
    35. resolve(list)
    36. })
    37. })
    38. }
    39. let download = (bucket_name, object_name, path, client) => {
    40. return new Promise((resolve, reject) => {
    41. client.fGetObject(bucket_name, object_name, path, function (err) {
    42. if (err) {
    43. return console.log(err)
    44. }
    45. console.log(`downloading ${object_name}`)
    46. resolve('success')
    47. })
    48. })
    49. }
    50. (async () => {
    51. let s3Client = new Minio.Client(config);
    52. let bucket = await connect(config, s3Client)
    53. let list = await listObject(bucket, config.setting.prefix, s3Client)
    54. for (let i of list) {
    55. let length = i['name'].length;
    56. let start;
    57. let end;
    58. if (config.setting.split_range.length === 0) {
    59. start = 0;
    60. end = 0
    61. } else {
    62. start = config.setting.split_range[0];
    63. end = config.setting.split_range[1];
    64. }
    65. if (start > length || end > length) {
    66. throw new Error('the value of config.setting.split_range exceeds the length of filename')
    67. }
    68. let filepath = config.setting.file_path;
    69. if (config.setting.download_ext.length !== 0) {
    70. for (let j in config.setting.download_ext) {
    71. if (config.setting.download_ext.hasOwnProperty(j)) {
    72. if (i['name'].endsWith(config.setting.download_ext[j])) {
    73. await download(bucket, i['name'], `${filepath}${i['name'].slice(0, start)}${i['name'].slice(-end)}`, s3Client)
    74. }
    75. }
    76. }
    77. } else if (config.setting.download_ext.length === 0 && config.setting.exclude_ext.length !== 0) {
    78. for (let j in config.setting.exclude_ext) {
    79. if (config.setting.exclude_ext.hasOwnProperty(j)) {
    80. if (!i['name'].endsWith(config.setting.exclude_ext[j])) {
    81. await download(bucket, i['name'], `${filepath}${i['name'].slice(0, start)}${i['name'].slice(-end)}`, s3Client)
    82. }
    83. }
    84. }
    85. } else if (config.setting.download_ext.length === 0 && config.setting.exclude_ext.length === 0) {
    86. await download(bucket, i['name'], `${filepath}${i['name'].slice(0, start)}${i['name'].slice(-end)}`, s3Client)
    87. } else {
    88. throw new Error('one of config.setting.download_ext and config.setting.exclude_ext must be empty')
    89. }
    90. }
    91. })()

  • 相关阅读:
    2023年云南省职业院校技能大赛中职组“网络安全”赛项样题
    微突发丢包的艺术
    Android系统服务DropBoxManagerService详解与实践应用
    使用AOP进行日志记录
    mysql创建数据库时的常见约束
    SpringBoot电商项目之实现购物车功能
    serveless 思想 Midway.js 框架使用教程(七)
    序列召回基础+GRU4Rec论文阅读
    docker学习笔记
    idea2021+Activiti【最完整笔记一(基础使用)】
  • 原文地址:https://blog.csdn.net/LetsStudy/article/details/126269808