cxmo 10 luni în urmă
părinte
comite
2a4fab7773
1 a modificat fișierele cu 85 adăugiri și 0 ștergeri
  1. 85 0
      src/utils/common.js

+ 85 - 0
src/utils/common.js

@@ -4,6 +4,8 @@
 
 import {Message} from "element-ui"
 import{getOSSSign} from "@/api/api.js"
+//or <script src="https://sdk.amazonaws.com/js/aws-sdk-2.744.0.min.js"></script>
+import AWS from 'aws-sdk'
 const Minio = require('minio')
 const stream = require('stream')
 
@@ -96,6 +98,12 @@ export function uploadFileDirect(objectStorageClient,file,temName,options={}){
       }
       return handleUploadToMinIO(file,temName,minioOptions)
       // break;
+    case "s3":
+        let s3Options = {}
+        if(options.S3){
+            s3Options = options.S3
+        }
+        return handleUploadToS3(file,temName,s3Options)
     default:
       break;
   }
@@ -229,4 +237,81 @@ const handleUploadToMinIO=(file,fileName,options={})=>{
       reject(error)
     }
   })
+}
+//https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor-property
+const handleUploadToS3=(file,fileName,options={})=>{
+    return new Promise(async(resolve,reject)=>{
+        const res=await getOSSSign({StorageSource:3})
+        if(res.Ret!==200) reject("获取s3签名错误,res.Ret="+res.Ret)
+        const { Endpoint,
+                AccessKeyId,
+                AccessKeySecret,
+                Port,
+                Bucketname,
+                RegionId,
+                S3ForceStyle,
+                S3Protocol
+            } = res.Data
+
+        const s3Client = new AWS.S3({
+            apiVersion: '2006-03-01',
+            endpoint:Endpoint,
+            region:RegionId,
+            s3ForcePathStyle:S3ForceStyle,
+            credentials:{
+                accessKeyId:AccessKeyId,
+                secretAccessKey:AccessKeySecret,
+            }
+        })
+        /* window.s3Client = s3Client
+        console.log('s3:',s3Client) */
+        try {
+            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))
+                //s3 上传
+                /* s3Client.putObject({
+                    Bucket: Bucketname,
+                    ContentType: file.type || 'application/octet-stream',
+                    Body: file,
+                    ACL: "public-read",
+                    Key: fileName
+                }, function (err, data) {
+                    if (err) {
+                        console.log("Error! err =====> ", err);
+                    } else {
+                        console.log("Successfully uploaded! data =====> ", data);
+                    }
+                }); */
+                //upload会返回完整路径
+                s3Client.upload({
+                    Bucket: Bucketname,
+                    ContentType: file.type || 'application/octet-stream',
+                    Body: file,
+                    ACL: "public-read",
+                    Key: fileName
+                }, function (err, data) {
+                    if (err) {
+                        console.log("Error! err =====> ", err);
+                        throw new Error("上传到s3失败!")
+                    } else {
+                        let url = data['Location'];
+                        console.log("Successfully uploaded! URL =====> ", url);
+                        resolve(url)
+                    }
+                });
+            }
+          } catch (error) {
+            console.error(error);
+            if (error.name !== "cancel") {
+              //不是取消上传的则给错误提示
+              this.$message.warning("上传失败,请刷新重试");
+            }
+            reject(error)
+          }
+    })
 }