OSS管理文件(Node.js)
let OSS = require('ali-oss');
let config = {
region: 'oss-cn-hangzhou', //你的Region 注意 这个只要 空间名 不要 http:// 和 .aliyunoss.com !!
//secure: true,
accessKeyId: XXXX,//你的accessKeyId
accessKeySecret: XXXX,//你的accessKeySecret
bucket: 'ipb'
};
/**
* 配置
*/
let init = () => {
return new OSS(config);
}
/**
* 生成uuid
*/
let guid = () => {
let S4 = () => {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
/**
* 修改文件名字
*/
let fileName = (file,key) => {
let arr = file.name.split(".");
var uuid = guid();
if (arr.length > 1) {
return key + uuid + '.' + arr[arr.length - 1];
} else {
return uuid;
}
}
/**
* 上传文件
*/
apis.getOssToken({
roleSessionName: 123
})
.then(res => {
console.log(res)
})
let ossPut = (file,key) => {
return new Promise((resolve, reject) => {
let objectName = fileName(file,key);
init().put(objectName, file).then(({
res,
url
}) => {
if (res && res.status == 200) {
console.log('阿里云OSS上传文件成功回调', res, url, objectName);
let data = {
res: res,
url: url,
objectName: objectName
};
resolve(data);
}
}).catch((err) => {
console.log('阿里云OSS上传文件失败回调', err);
reject(err)
});
})
}
/**
* 下载文件
*/
let ossGet = (name) => {
return new Promise((resolve, reject) => {
let signUrl = init().signatureUrl(name, {expires: 300});
resolve(signUrl);
})
}
let ossDel = (name) => {
return new Promise((resolve, reject) => {
init().delete(name).then((res) => {
if (res && res.status == 200) {
console.log('阿里云OSS删除文件成功回调', res);
resolve(res);
}
}).catch((err) => {
console.log('阿里云OSS删除文件失败回调', err);
reject(err)
});
})
}
export default {
ossPut,
ossGet,
ossDel
}
调用
<el-form-item label="头像" class="user">
<el-input type="hidden"></el-input>
<el-upload class="iptImg" ref="uploadAvatar" action list-type="picture-card" :http-request="uploadImg"
:before-upload="beforeUploadImg" :on-remove="removeFileForm" :on-exceed="exceedFileForm">
<img v-if="addForm.url" :src="addForm.url" class="avatarDis" />
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</el-form-item> <script>
export default {
data() {
return {
addForm: {
avatar: "",
url:"",
}
}
}, methods: {
//图片类型验证
beforeUploadImg(file) {
const isJPG = file.type === "image/jpg";
const isJPEG = file.type === "image/jpeg";
const isPNG = file.type === "image/png";
const isGIF = file.type === "image/gif";
const isBMP = file.type === "image/bmp";
if (!isJPG && !isJPEG && !isPNG && !isGIF && !isBMP) {
this.$message.error("只能上传图片格式为jpg,jpeg,png,gif,bmp!");
}
return isJPG || isJPEG || isPNG || isGIF || isBMP;
}, //点击图片上传
uploadImg(file) {
// this.addForm.file.push(file.file);
// console.log(this.addForm.file);
if(this.addForm.avatar != "" && this.addForm.avatar != null){
this.removeFileForm(file);
}
this.$ossClient
.ossPut(file.file, "PartyMember/")
.then(res => {
this.addForm.avatar = res.objectName;
})
.catch(e => {
console.log(e);
});
}, //移除文件
removeFileForm(file) {
// this.addForm.file.splice(this.addForm.file.indexOf(file.raw), 1);
// console.log(this.addForm.file);
this.$ossClient
.ossDel(this.addForm.avatar)
.then(res => {
this.addForm.avatar = "";
})
.catch(e => {
console.log(e);
});
},
//超出数量限制
exceedFileForm() {
this.$message.error("最多上传1个头像");
},
//数据回显
getPartyInfo() {
this.partyId = this.$route.query.partyId;
axios({
method: "get", //请求方式
url: "/api/commonPartyMember/getPartyMember", //请求地址
params: {
id: this.partyId
}
})
.then(res => {
this.addForm.avatar = res.data.data.avatar;
if (this.addForm.avatar != "" && this.addForm.avatar != null) {
this.$ossClient
.ossGet(this.addForm.avatar)
.then(res => {
this.addForm.url = res;
})
.catch(e => {
console.log(e);
});
} })
.catch(err => {
console.log(err);
});
}, }, } </script>
OSS管理文件(Node.js)的更多相关文章
- 封装读取文件(node js)
我们都会简单的读取文件,今天我们就来讲一下用函数封装读取文件. 1.首先我们要先建好文件 2.我们在index.js里面写入代码: var http=require('http'); var fs=r ...
- Node.js NPM 管理包
章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json 根据安 ...
- rtags——node.js+redis实现的标签管理模块
引言在我们游览网页时,随处可见标签的身影: 进入个人微博主页,可以看到自己/他人的标签,微博系统会推送与你有相同标签的人 游览博文,大多数博文有标签标记,以说明文章主旨,方便搜索和查阅 网上购物,我们 ...
- Node.js模块
每一个Node.js都是一个Node.js模块,包括JavaScript文件(.js).JSON文本文件(.json)和二进制模块文件(.node). mymodul.js function Hell ...
- Node.js快速入门
Node.js是什么? Node.js是建立在谷歌Chrome的JavaScript引擎(V8引擎)的Web应用程序框架. 它的最新版本是:v0.12.7(在编写本教程时的版本).Node.js在官方 ...
- Node.js开发——MongoDB与Mongoose
为了保存网站的用户数据和业务数据,通常需要一个数据库.MongoDB和Node.js特别般配,因为MongoDB是基于文档的非关系型数据库,文档是按BSON(JSON的轻量化二进制格式)存储的,增删改 ...
- Node.js学习(Node.js基础)
1.开发环境搭建 v8引擎是作为解析JavaScript程序来运行的 nodejs是一个高性能的,第一个体现在他的JavaScript解析速度很快,v8引擎性能很高,第二个事件驱动和非阻塞 2.全局对 ...
- Node.js Learning Notes
简介 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事件驱动I/O服务 ...
- Node.js NPM Package.json
章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json Nod ...
- 一目了然的 Node.js Windows10 安装篇
本篇文章 介绍 NodeJS 的安装 及环境变量配置 Node JS 的 了解 1.Node.js简介 简单的说 Node.js 就是运行在服务端的 JavaScript.Node.js 是一个基于 ...
随机推荐
- 将【jar包、bat、其他文件】注册到windows服务的三种方法
将[jar包.bat.其他文件]注册到windows服务的三种方法 1.instsrv.exe和srvany.exe 1.下载配置instsrv和srvany 下载地址:https://dl.pcon ...
- JavaWeb 下载Demo
JavaWeb 继承 Httpservlet 类实现文件下载的功能 package com.gen; import javax.servlet.ServletException; import jav ...
- 关于同时使用Vue.js 和 Jquery时dom事件失效问题
先加载vue.js,让页面渲染完成后加载jq,给jq绑定ready事件 $(document).ready(function(){ $(function(){ (Jq) }); });
- Excel怎么把公式转成数值并复制
直接选中复制,粘贴时选择选择性粘贴,选择数值确认
- Python扩展(pybind11混编)
背景介绍pybind11是一个基于C++11标准的模版库. 与Boost.Python类似, pybind11主要着眼于创建C++代码的Python封装, 并为其提供了一套轻量级的解决方案. 安装与代 ...
- Ubuntu20.04安装PEA软件
PEA软件可用于实时精密卫星钟差估计,精密卫星定轨,精密单点定位,电离层建模以及DCB估计等. Ginan开发人员推荐使用Ubuntu18.04或Ubuntu20.04搭建,本文使用Ubuntu20. ...
- springboot启动日志:Multiple Spring Data modules found, entering strict repository configuration mode
问题描述 最近启动springboot项目的时候,发现有一条日志:Multiple Spring Data modules found, entering strict repository conf ...
- yolov5的训练中断恢复
Yolov5的恢复训练 方法一:使用自带参数-resume 1. train.py文件中找到函数parse_opt,修改参数–resume的默认参数为Ture 2. runs/train/exp*/w ...
- debian11 bspwm+polybar问题记录(siji字体无法正常显示)
一.siji字体无法显示. 很懒很菜,就想用开箱即用的原始配置依然遇到了问题...plybar中的bitmap字体siji无法正常显示.即便按照github的siji官方脚本安装了siji字体还是不行 ...
- C++ 17 遍历文件夹图片文件进行循环操作
#include <filesystem>using namespace std;namespace fs = std::filesystem; string path = filepat ...