简述
目的在于实现一个vue读取minio图片资源的功能
效果展示
读取图片
这里从minio读取图片,将图片转变成base64数据流,如何不知道如何安裝minio庫可以參考我[上一篇文章(minio的圖片上傳)]。(https://blog.csdn.net/weixin_73368873/article/details/130039287)
const express = require('express')
const router = express.Router()
var Minio = require('minio')
const fs = require('fs');
// Instantiate the minio client with the endpoint
// and access keys as shown below.
var minioClient = new Minio.Client({
endPoint: 't1', //这里不能带http或者https
port: 9000,
useSSL: false, //不需要https
accessKey: 'admin', //账号密码同web登陆时一致
secretKey: 'admin123'
});
router.get('/myInfo', function(req, res) {
const bucketName = 'btest2' // 替换为您的存储桶名称
const objectName = 'm2.png' // 替换为您的图片文件名
// 获取指定存储桶中的对象,并将其内容作为Buffer返回
minioClient.getObject(bucketName, objectName, (err, dataStream) => {
if (err) {
console.log(err)
return res.status(500).send({ error: 'Failed to get image' })
}
let chunks = []
// 将数据流转换成Buffer
dataStream.on('data', chunk => {
chunks.push(chunk)
})
dataStream.on('end', () => {
const buffer = Buffer.concat(chunks)
// 将Buffer转换为Base64字符串
const base64String = buffer.toString('base64')
console.log("返回图片")
// 将Base64字符串作为JSON响应发送回客户端
return res.send({ data: 'data:image/png;base64,' + base64String })
})
})
})
module.exports = router
前端读取数据流
异步获取base64数据流,转化为image的src资源,這樣直接轉化就非常自然了