Browse Source

阿里云oss对象存储本地上传

yuwenjun1997 2 năm trước cách đây
mục cha
commit
b037e6dd3e

+ 4 - 3
src/main/resources/config/dev/application-dev.yml

@@ -54,9 +54,10 @@ logging:
 caimei:
   siteEnv: 0 #网站环境,(2:正式环境,1:测试环境,0:开发环境)
   #spiServer: http://192.168.2.68:8008
-  coreServer: https://core-b.caimei365.com
-#  coreServer: http://192.168.2.67:18002
-#  coreServer: http://192.168.2.75:18002
+  #coreServer: https://core-b.caimei365.com
+  #coreServer: http://192.168.2.67:18002
+  #coreServer: http://192.168.2.75:18002
+  coreServer: http://192.168.2.100:18002
   imageDomain: https://img-b.caimei365.com
   wwwDomain: http://localhost:8009
   destPath: classpath:/

+ 80 - 0
src/main/resources/static/js/oss-upload.js

@@ -0,0 +1,80 @@
+"use strict";
+
+var client = null;
+var baseFileUrl = '';
+
+function initOssClient(callback) {
+    if (client) {
+        callback && callback();
+        return;
+    }
+
+    PublicApi.fetchOssInitData().then(function (res) {
+        if (res.code) return;
+        var _res$data = res.data,
+            accessKeyId = _res$data.accessKeyId,
+            securityToken = _res$data.securityToken,
+            bucket = _res$data.bucket,
+            accessKeySecret = _res$data.accessKeySecret;
+        var config = {
+            // 以华东1(杭州)为例,Region填写为oss-cn-hangzhou。
+            region: 'oss-cn-shenzhen',
+            accessKeyId: accessKeyId,
+            accessKeySecret: accessKeySecret,
+            stsToken: securityToken,
+            bucket: bucket
+        };
+        client = new OSS(config);
+        baseFileUrl = "https://".concat(bucket, ".").concat(config.region, ".aliyuncs.com/");
+        setInterval(function () {
+            initOssClient();
+        }, 1000 * 60 * 60);
+        if (client) callback && callback();
+    }).catch(function (error) {
+        console.log(error);
+    });
+}
+
+function multipartUpload(options, callback) {
+    initOssClient(function () {
+        if (options.status > -1) return; // 指定上传到examplebucket的Object名称,例如exampleobject.txt。
+
+        var name = $("#ossBucket").val() + '/' + options.fileName.replace(/([^\\/]+)\.([^\\/]+)/i, function (match, $1, $2) {
+            options.uuid = options.uuid + '.' + $2;
+            return options.uuid;
+        }); // 分片上传
+
+        client.multipartUpload(name, options.file, {
+            // 获取分片上传进度、断点和返回值。
+            progress: function progress(p, cpt, res) {
+                callback.progress && callback.progress(options, {
+                    p: p,
+                    cpt: cpt,
+                    res: res
+                });
+            },
+            // 设置并发上传的分片数量。
+            parallel: 10,
+            // 设置分片大小。默认值为1 MB,最小值为100 KB。
+            partSize: 1024 * 1024,
+            // 文件类型
+            mime: options.type,
+            headers: {
+                // 指定初始化分片上传时是否覆盖同名Object。此处设置为true,表示禁止覆盖同名Object。
+                'x-oss-forbid-overwrite': 'true'
+            }
+        }).then(function (res) {
+            callback.success && callback.success(options, res, baseFileUrl + res.name);
+        }).catch(function (err) {
+            console.log(err);
+            callback.success && callback.faild(options, err);
+        });
+    });
+}
+
+// 上传
+function upload(fileList, callback) {
+    for (var i = 0; i < fileList.length; i++) {
+        multipartUpload(fileList[i], callback);
+    }
+}