- 创建连接
npm install --save minio
const Minio = require('minio')
const minioClient = new Minio.Client({
endPoint: '192.l68.0.1',
port: 9000,
accessKey: 'xxxx',
secretKey: 'xxxx',
useSSL: false
})
- 上传文件
const stream = require('stream')
export function uploadFile (backetName, fileObj, callback) {
console.log(backetName, fileObj)
if (fileObj) {
const file = fileObj
if (file === undefined) {
} else {
const date = new Date()
const year = new Date().getFullYear()
const month =
date.getMonth() + 1 < 10
? '0' + (date.getMonth() + 1)
: date.getMonth() + 1
const day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate()
const fileName = date.getTime() + file.name
const fileDate = '' + year + month + day
const mineType = file.type
const fileSize = file.size
console.log('fileName', fileName)
const metadata = {
'content-type': mineType,
'content-length': fileSize
}
minioClient.bucketExists(backetName, function (err) {
console.log('判断储存桶是否存在')
if (err) {
if (err.code === 'NoSuchBucket') {
return console.log('bucket does not exist.')
}
console.log('1233221')
return console.log(err)
}
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onloadend = function (e) {
const dataurl = e.target.result
const blob = toBlob(dataurl)
const reader2 = new FileReader()
reader2.readAsArrayBuffer(blob)
reader2.onload = function (ex) {
const bufferStream = new stream.PassThrough()
bufferStream.end(Buffer.from(ex.target.result))
minioClient.putObject(
backetName,
`${fileDate}/${fileName}`,
bufferStream,
fileSize,
metadata,
(err) => {
if (err == null) {
minioClient.presignedPutObject(
backetName,
`${fileDate}/${fileName}`,
24 * 60 * 60,
function (err1, presignedUrl) {
console.log(err1)
if (err1) return
if (presignedUrl) {
const arr = presignedUrl.split('?')
if (arr.length === 2) {
callback(arr[0])
}
}
}
)
}
}
)
}
}
})
}
}
}
export function toBlob (base64Data) {
let byteString = base64Data
if (base64Data.split(',')[0].indexOf('base64') >= 0) {
byteString = window.atob(base64Data.split(',')[1])
} else {
byteString = unescape(base64Data.split(',')[1])
}
const mimeString = base64Data.split(';')[0].split(':')[1]
const uintArr = new Uint8Array(byteString.length)
for (let i = 0; i < byteString.length; i++) {
uintArr[i] = byteString.charCodeAt(i)
}
const blob = new Blob([uintArr], {
type: mimeString
})
return blob
}
export function checkedAndUpload (bucketName, info, callback) {
minioClient.bucketExists(bucketName, err => {
if (err) {
minioClient.makeBucket(bucketName, 'us-east-1', err1 => {
if (err1) {
console.error(`${info.file.name}文件上传失败`)
return
}
uploadFile(bucketName, info, callback)
})
} else {
uploadFile(bucketName, info, callback)
}
})
}
export function connectionStatus (bucketName, callback) {
minioClient.bucketExists(bucketName, err => {
console.Log(err)
callback(err)
})
}
- 下载文件
export function downloadFile (bucketName, fileName, callback) {
minioClient.getObject(bucketName, fileName, (err, dataStream) => {
callback(err, dataStream)
})
}
- 调用
<div @click="selectFile"> 上传 </div>
<input type="file id="uploadInput" ref="uploadInput" v-show="false" @change="changeInput()"
/>