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一起,如文件的删除和新增可以记录,未尝试