|
@@ -2,6 +2,11 @@
|
|
|
// import getNetworkType from './getNetworkType';
|
|
|
// import getSystemInfo from './getSystemInfo';
|
|
|
|
|
|
+import {Message} from "element-ui"
|
|
|
+import{getOSSSign} from "@/api/api.js"
|
|
|
+const Minio = require('minio')
|
|
|
+const stream = require('stream')
|
|
|
+
|
|
|
// 根据字节流下载文件
|
|
|
/**
|
|
|
* @param {Blob} data 流数据
|
|
@@ -45,4 +50,181 @@ export function getUrlParams(str=window.location.href,key) {
|
|
|
str.split('?')[1].split('&').map(i => obj[(i.split('=')[0])] = i.split('=')[1]);
|
|
|
|
|
|
return obj[key]
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param {*} objectStorageClient 1-走oss 2-走minio
|
|
|
+ * @param {*} file 上传文件
|
|
|
+ * @param {*} temName 文件路径/文件名字
|
|
|
+ * @param {*} options 文件路径/文件名字
|
|
|
+ * @param {*} options.OSS 上传至阿里云的配置
|
|
|
+ * @param {*} options.MINIO 上传至MINIO的配置
|
|
|
+ */
|
|
|
+
|
|
|
+// 上传文件 直接走对象存取服务器
|
|
|
+export function uploadFileDirect(objectStorageClient,file,temName,options={}){
|
|
|
+ const objectStorageType= (objectStorageClient || JSON.parse(localStorage.getItem('dynamicOutLinks')).ObjectStorageClient)+""
|
|
|
+ console.log(objectStorageType,'objectStorageType');
|
|
|
+ return
|
|
|
+ if(!objectStorageType){
|
|
|
+ Message.error("ObjectStorageClient参数丢失")
|
|
|
+ return new Promise((resolve,reject) => reject("ObjectStorageClient参数丢失"))
|
|
|
+ }
|
|
|
+ if(!file){
|
|
|
+ Message.error("file参数错误")
|
|
|
+ return new Promise((resolve,reject) => reject("file参数错误"))
|
|
|
+ }
|
|
|
+ if(!temName){
|
|
|
+ Message.error("temName参数错误")
|
|
|
+ return new Promise((resolve,reject) => reject("temName参数错误"))
|
|
|
+ }
|
|
|
+ console.log(objectStorageClient,file,temName,options,'objectStorageClient,file,temName,options');
|
|
|
+ switch (objectStorageClient) {
|
|
|
+ case "1":
|
|
|
+ let ossOptions = {}
|
|
|
+ if(options.OSS){
|
|
|
+ ossOptions=options.OSS
|
|
|
+ }
|
|
|
+
|
|
|
+ return handleUploadToOSS(file,temName,ossOptions)
|
|
|
+ // break;
|
|
|
+ case "2":
|
|
|
+ let minioOptions = {}
|
|
|
+ if(options.MINIO){
|
|
|
+ minioOptions=options.MINIO
|
|
|
+ }
|
|
|
+ return handleUploadToMinIO(file,temName,minioOptions)
|
|
|
+ // break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const handleUploadToOSS= (file,fileName,options={})=>{
|
|
|
+ return new Promise(async (resolve,reject)=>{
|
|
|
+ // 获取oss临时签名
|
|
|
+ const res=await getOSSSign()
|
|
|
+ if(res.Ret!==200) reject("获取oss临时签名错误")
|
|
|
+ try {
|
|
|
+
|
|
|
+ let oss_params = {
|
|
|
+ // yourRegion填写Bucket所在地域。以华东1(杭州)为例,Region填写为oss-cn-hangzhou。
|
|
|
+ region: res.Data.RegionId,
|
|
|
+ // 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。
|
|
|
+ accessKeyId: res.Data.AccessKeyId,
|
|
|
+ accessKeySecret: res.Data.AccessKeySecret,
|
|
|
+ // 从STS服务获取的安全令牌(SecurityToken)。
|
|
|
+ stsToken: res.Data.SecurityToken,
|
|
|
+ // 填写Bucket名称,例如examplebucket。
|
|
|
+ bucket: res.Data.Bucketname,
|
|
|
+ endpoint: res.Data.Endpoint,
|
|
|
+ cname:true,
|
|
|
+ timeout:6000000
|
|
|
+ }
|
|
|
+
|
|
|
+ let imgHost = res.Data.Imghost;
|
|
|
+
|
|
|
+ const ALOSSINS=new OSS(oss_params);
|
|
|
+
|
|
|
+ const resp=await ALOSSINS.multipartUpload(fileName,file,{...options})
|
|
|
+ console.log('上传结果',resp);
|
|
|
+ if(resp.res.status===200){
|
|
|
+ let url=imgHost+resp.name
|
|
|
+ resolve(url)
|
|
|
+ console.log('oss文件地址',url);
|
|
|
+ }else{
|
|
|
+ throw new Error('上传到阿里云失败:res.status'+resp.res.status)
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error(error);
|
|
|
+ if (error.name !== "cancel") {
|
|
|
+ Message.warning('上传失败,请刷新重试')
|
|
|
+ }
|
|
|
+ reject(error)
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+// minio sdk 文档 https://min.io/docs/minio/linux/developers/javascript/API.html
|
|
|
+const handleUploadToMinIO=(file,fileName,options={})=>{
|
|
|
+ return new Promise(async (resolve,reject)=>{
|
|
|
+ // console.log(Minio);
|
|
|
+ const minioClient = new Minio.Client({
|
|
|
+ endPoint: '8.136.199.33',
|
|
|
+ port: 9000,
|
|
|
+ useSSL: false,
|
|
|
+ accessKey: 'eta',
|
|
|
+ secretKey: 'eta202309',
|
|
|
+ })
|
|
|
+
|
|
|
+ try {
|
|
|
+ var metaData = {...{
|
|
|
+ 'Content-Type': file.type||'application/octet-stream',
|
|
|
+ "Content-Length": file.size,
|
|
|
+ // example: 5678,
|
|
|
+ },...options}
|
|
|
+
|
|
|
+ minioClient.bucketExists("etastatic", function (err, exists) {
|
|
|
+ if (err) {
|
|
|
+ throw "minio 查看桶是否存在失败"+err
|
|
|
+ // return console.log(err);
|
|
|
+ }
|
|
|
+ if (!exists) {
|
|
|
+ // 不存在桶,创建桶
|
|
|
+ minioClient.makeBucket('etastatic', function (err) {
|
|
|
+ if (err) {
|
|
|
+ throw "minio 创建桶失败"+err
|
|
|
+ }
|
|
|
+
|
|
|
+ let reader = new FileReader();
|
|
|
+ // console.log(reader);
|
|
|
+ reader.readAsArrayBuffer(file);
|
|
|
+ reader.onloadend = function (e) {
|
|
|
+ const dataurl = e.target.result;
|
|
|
+ let bufferStream = new stream.PassThrough();
|
|
|
+ // 转化成数据流 minio接受数据流格式
|
|
|
+ bufferStream.end(Buffer.from(dataurl))
|
|
|
+ // console.log(bufferStream);
|
|
|
+ minioClient.putObject('etastatic', fileName, bufferStream, metaData, function (err, etag) {
|
|
|
+ if (err){
|
|
|
+ throw "上传到minio失败:"+err
|
|
|
+ }
|
|
|
+
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ if (exists) {
|
|
|
+ // console.log("exists");
|
|
|
+ let reader = new FileReader();
|
|
|
+ console.log(reader);
|
|
|
+ reader.readAsArrayBuffer(file);
|
|
|
+ reader.onloadend = function (e) {
|
|
|
+ const dataurl = e.target.result;
|
|
|
+ let bufferStream = new stream.PassThrough();
|
|
|
+
|
|
|
+ bufferStream.end(Buffer.from(dataurl))
|
|
|
+ console.log(bufferStream);
|
|
|
+ // Using fPutObject API upload your file to the bucket europetrip.
|
|
|
+ minioClient.putObject('etastatic', fileName, bufferStream, metaData, function (err, etag) {
|
|
|
+ if (err){
|
|
|
+ throw "上传到minio失败:"+err
|
|
|
+ }
|
|
|
+ console.log(minioClient);
|
|
|
+ // let fileUrl = http://8.136.199.33:9000/etastatic/testOne.jpeg
|
|
|
+ resolve("http://8.136.199.33:9000/etastatic/testOne.jpeg")
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ console.error(error);
|
|
|
+ if (error.name !== "cancel") {
|
|
|
+ //不是取消上传的则给错误提示
|
|
|
+ this.$message.warning("上传失败,请刷新重试");
|
|
|
+ }
|
|
|
+ reject(error)
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|