当前位置: 首页 > news >正文

minio javascript 常用操作整理(未完继续)

1 启动minio

.\minio.exe server D:\CodeNext\miniojs\minioFolder

2 创建client

import * as Minio from 'minio'const minioClient = new Minio.Client({endPoint: 'localhost',port: 9000,useSSL: false,accessKey: 'minioadmin',secretKey: 'minioadmin',
})

3 Bucket-用例1 bucket操作 创建-list-是否存在-移除


minioClient.makeBucket('mybucket', function (err) {if (err) return console.log('Error creating bucket with object lock.', err)console.log('Bucket created successfully in "us-east-1" and enabled object lock')})try {const buckets = await minioClient.listBuckets()console.log('Success', buckets)} catch (err) {console.log(err.message)}const exists = await minioClient.bucketExists('mybucket')
if (exists) {console.log('Bucket exists.')
}try {await minioClient.removeBucket('mybucket')console.log('Bucket removed successfully.')} catch (err) {console.log('unable to remove bucket.')}

4 Bucket-用例2 新建bucket-上传文件及文件夹-查看

minioClient.makeBucket('mybucket', function (err) {if (err) return console.log('Error creating bucket with object lock.', err)console.log('Bucket created successfully in "us-east-1" and enabled object lock')})const data = []const stream = minioClient.listObjects('mybucket', '', true)stream.on('data', function (obj) {data.push(obj)})stream.on('end', function () {console.log(data)})stream.on('error', function (err) {console.log(err)})

打印


[{name: 'A_Penthouse.dgn',lastModified: 2024-10-29T06:14:03.557Z,etag: 'ebbe65a1e3e090c30a402ada444a1127',size: 5001216},{name: 'Design1/Design.dgn',lastModified: 2024-10-29T06:14:13.429Z,etag: '916d544b4ec16761ebab1fc638b61e80',size: 8399872}
]

5 Bucket-用例3 给Bucket打version

const versioningConfig = { Status: 'Enabled' }
await minioClient.setBucketVersioning('mybucket', versioningConfig)const versionInfo = await minioClient.getBucketVersioning('mybucket')
console.log('Success ', versionInfo)

6 Bucket Policy

const readPolicy = {"Version": "2012-10-17","Statement": [{"Sid": "PublicRead","Effect": "Allow","Principal": "*","Action": ["s3:GetBucketLocation","s3:GetObject"],"Resource": "arn:aws:s3:::**",}]
}await minioClient.setBucketPolicy('mybucket',JSON.stringify(readPolicy) );
const policyDetail = await minioClient.getBucketPolicy('mybucket')console.log(`Bucket policy file: ${policyDetail}`)

结果

这里的policy这里会有一些概念是来自amazon,arn是资源,IAM代表身份和访问管理(Identity and Access Management)

7 Object 上传

putObject

const file = './tmp/test.json'
const fileStream = Fs.createReadStream(file)
const fileStat = Fs.stat(file, function (err, stats) {if (err) {return console.log(err)}minioClient.putObject('mybucket', 'test.json', fileStream, stats.size, function (err, objInfo) {if (err) {return console.log(err) // err should be null}console.log('Success', objInfo)})
})
fileStat;

fputObject

FPutObject,把指定文件上传到一个对象中。当对象小于 128MB 时,会调用一次 PUT 请求进行上传。当大于 128MB 时,会根据文件的实际大小自动拆分成 128MB 一块或更大一些的块儿进行分片上传。需要注意是对象的最大大小是 5TB。

const file = './tmp/big.bim'
const metaData = {'Content-Type': 'text/html','Content-Language': 123,'X-Amz-Meta-Testing': 1234,example: 5678,
}
minioClient.fPutObject('mybucket', 'big.bim', file, metaData, function (err, objInfo) {if (err) {return console.log(err)}console.log('Success', objInfo.etag, objInfo.versionId)
})

这里上传了一个很大的文件big.bim

运行上传后马上停掉进程,然后去调用listIncompleteUploads去查询,能看到结果

listIncompleteUploads

const Stream = minioClient.listIncompleteUploads('mybucket', 'big.bim', true)console.log(Stream)
Stream.on('data', function (obj) {console.log(obj)
})
Stream.on('end', function () {console.log('End')
})
Stream.on('error', function (err) {console.log(err)
})

removeIncompleteUpload

删除  await minioClient.removeIncompleteUpload('mybucket', 'big.bim')

8 Object下载

getObject

Downloads an object as a stream 流式下载

fGetObject

minioClient.fGetObject('mybucket', 'A_Penthouse.dgn', './tmp/A_Penthouse.dgn', function (err) {if (err) {return console.log(err)}console.log('success')
})

下载文件并将文件保存到本地文件系统。

9 Object删除

removeObject

删除单个文件

;(async function () {try {await minioClient.removeObject('mybucket', 'test.json')console.log('Removed the object')} catch (err) {console.log('Unable to remove object', err)}
})()

removeObjects

删除所有文件

const objectsList = []
const objectsStream = minioClient.listObjects('mybucket', '', true)objectsStream.on('data', function (obj) {objectsList.push(obj.name)
})objectsStream.on('error', function (e) {console.log(e)
})objectsStream.on('end', async () => {await minioClient.removeObjects('mybucket', objectsList)
})

这里删除了所有的文件,但是文件夹保留了

删除文件夹

forceDelete: true, 增加删除的Opt可以删除

;(async function () {try {await minioClient.removeObject('mybucket', 'copy/',{forceDelete: true,})console.log('Removed the object')} catch (err) {console.log('Unable to remove object', err)}
})()

10 Copy 存疑问

本Bucket内的copy 加文件夹也可 conditions 不成功

 await minioClient.copyObject('mybucket', 'testcopy.json', '/mybucket/test.json')

11 消息机制

可以和RabbitQ一起,如文件的删除和新增可以记录,未尝试


http://www.mrgr.cn/news/62041.html

相关文章:

  • 推荐一款功能强大的文字处理工具:Atlantis Word Processor
  • Flutter动画渐变
  • JS | CommonJS、AMD、CMD、ES6-Module、UMD五种JS模块化规范
  • thinkphp和vue基于Workerman搭建Websocket服务实现用户实时聊天,完整前后端源码demo及数据表sql
  • 深度学习案例:带有一个隐藏层的平面数据分类
  • 智能合约分享
  • 在AdaBoost中,分类错误的样本的权重会增大
  • 《达梦》达梦数据库安装步骤(VMware16+麒麟 10+DM8)
  • JS:列表操作
  • 『Linux学习笔记』如何在 Ubuntu 22.04 上安装和配置 VNC
  • Python第六次作业
  • 云腾五洲的智联引擎是什么?
  • Day 3 DNS劫持
  • 微信小程序app.js里面onLaunch里面的函数比page里面的onshow里面的方法后执行
  • 【Eclipse系列】eclipse快捷键和设置
  • HDFS Balancer原理及优化方向
  • 如何在Linux系统中使用Zabbix进行监控
  • 深度学习-39-基于PyTorch的预训练源模型微调到目标模型的流程
  • 宠物空气净化器哪个好?希喂、352真实测评分享
  • Docker-基础
  • Transformer中的Encoder
  • ef core $ 附近有语法错误_ef core contains $符近语法错
  • 进程与文件系统(页框与内存的块的映射关系?)
  • 数对的最大曼哈顿距离[ABC178E] Dist Max
  • -函数结构
  • 多传感器数字化分析系统