当前位置:网站首页>upload上传图片到腾讯云,如何上传图片
upload上传图片到腾讯云,如何上传图片
2022-08-05 04:16:00 【狐逍超】
讲解一下如何上传图片到腾讯云,上传到其他第三方云盘操作也是大致一样的;
一:首先需要拥有一个账号(有的直接可以跳过看后面)
1.创建一个腾讯云账号,跟着提示操作即可(必须实名-为了安全),中间选择 对象存储cos,这里就创建好了;
2.接下来需要创建存储桶,里面的权限为:公有读,私有写;
3.设置cors规则,AllowHeader配置成* 号,这里存储桶也创建好了;
二:封装一个上传组件
这里使用到element组件库中的Upload来进行上传;
Upload使用可以对照着文档来-并结合我给出的代码;
项目中需要安装 cos-js-sdk-v5 --save 腾讯云包才能上传;
页面引入并创建实例,配置储存桶密钥-》再云产品-》访问密钥
下面这是一个完整的封装,可以直接粘贴复制使用;
<template>
<div>
<!--必填:
action:指定上传地址
file-list:必备数组管理图片
样式:list-type 影响渲染样式,指定 pictur-card 照片墙
钩子:on-preview 图片被点击时触发函数,绑定会出现放大镜,需要自己显示弹窗
on-remove 删除图片时触发的功能
on-change 修改图片触发的函数-->
<el-upload
action="#"
list-type="picture-card"
:file-list="fileList"
:on-preview="onPreview"
:on-remove="onRemove"
:on-change="onChange"
:http-request="httpRequest"
:before-upload="beforeUpload"
:class="{hasImg:fileList.length>0}"
>
<i class="el-icon-plus" />
</el-upload>
<el-progress v-if="isShowPercent" :text-inside="true" :stroke-width="16" :percentage="percent" style="width:146px;" />
<el-dialog title="预览" :visible="isShowDialog" @close="isShowDialog=false">
<el-row type="flex" justify="center">
<img :src="previewImg" alt="">
</el-row>
</el-dialog>
</div>
</template>
<script>
// 引入对象储存工具库,创建实例
import COS from 'cos-js-sdk-v5'
// 前端使用固定密钥计算签名,该格式适用于前端调试
const cos = new COS({
SecretId: '密钥号',
SecretKey: '密钥密码'
})
export default {
name: 'Dashboard',
data() {
return {
isShowPercent: false,
percent: 0,
isShowDialog: false,
previewImg: '',
// 文件列表每个对象都是一张图片,url:显示图片路径,udi:唯一标识,status:状态
fileList: []
}
},
methods: {
// 点击文件列表中已上传的文件时的钩子,放大镜
onPreview(file) {
this.previewImg = file.url // 显示图片
this.isShowDialog = true // 弹窗
},
// 移除文件时的钩子
onRemove(file, newFileList) {
this.fileList = newFileList
},
// 添加,上传成功/失败 都会触发
onChange(file, newFileList) {
// 由于 fileList 的绑的是单向的,需要手动替换
this.fileList = newFileList
},
// 上传文件之前的钩子,参数为上传的文件,若返回 false 或者返回 Promise 且被 reject,则停止上传。
beforeUpload(file) {
console.log('上传前触发')
console.log(file)
// 校验格式
const typeList = ['image/jpeg', 'image/png', 'image/gif']
if (!typeList.includes(file.type)) {
this.$message.error('目前只支持 jpg/png/gif 图片格式')
return false
}
// 校验大小
const maxSize = 5 * 1024 * 1024
if (file.size > maxSize) {
this.$message.error('图片大小不能超过 5M')
return false
}
},
// 覆盖默认的上传行为,可以自定义上传的路径
httpRequest(params) {
// 开始上传时显示进度条
this.isShowPercent = true
cos.putObject({
Bucket: 'chao-13', /* 填入您自己的存储桶,必须字段 */
Region: 'ap-nanjing', /* 存储桶所在地域,例如ap-beijing,必须字段 */
Key: params.file.name, /* 存储在桶里的对象键(例如1.jpg,a/b/test.txt),必须字段 */
StorageClass: 'STANDARD',
Body: params.file, // 上传文件对象
onProgress: (progressData) => {
console.log(JSON.stringify(progressData))
this.percent = progressData.percent * 100
}
}, (err, data) => {
console.log('腾讯工具库上传完毕')
// 延迟提升用户体验-消失进度条
setTimeout(() => {
this.isShowPercent = false
}, 1000)
console.log(err || data)
if (!err) {
// 手动处理 fileList
this.fileList[0].url = 'http://' + data.Location
this.fileList[0].status = 'success'
}
})
}
}
}
</script>
<style lang="scss" scoped>
.hasImg {
::v-deep .el-upload--picture-card {
display: none;
}
}
::v-deep img {
// 让图片在相等大小中,保持完整显示
object-fit: cover;
}
</style>
上面代码中需要注意干掉默认上传地址-使用#号即可;
若只需上传一张图片,那就设置一个动态样式,用于隐藏上传组件,通过存储图片数组的长度进行判断即可;
上传图片需要用到的组件内置函数分别有:
onPreview(点击已上传图片触发):这里用于显示图片(放大);
beforeUpload(上传前触发) 函数中进行图片的 格式、大小等校验;
onChange(上传就会触发):因为数据绑定是单项的,所以需要手动进行图片的替换;
onRemove(移除图片触发):用于删除不需要的图片;
httpRequest(默认上传路径):这里配置腾讯云上传路径,需要按需填入对应的数据;
密钥在哪里查看?

三:找不到上传文档?不知道httpRequest 里的内容在哪里找?看这里就够了
第一步:点击spi文档会有弹框,点击SDK列表进入

第二步:点击javaScript SDK快速入门

第三步:上传对象复制需要的即可

总结:以上就是如何上传的全过程,代码可以结合文档来查看-效果更好
边栏推荐
- bytebuffer put flip compact clear 方法演示
- UE4 更改组件变量 (以修改第一人称角色模板的最大行走速度和跳跃高度为例)
- 工业级远距离无线传输装置的功能有哪些?
- What is the function of industrial-grade remote wireless transmission device?
- Analyses the mainstream across technology solutions
- MySql的索引学习和使用;(本人觉得足够详细)
- GC Gaode coordinate and Baidu coordinate conversion
- write the story about us
- Some conventional routines of program development (1)
- There are several common event handling methods in Swing?How to listen for events?
猜你喜欢

Based on holding YOLOv5 custom implementation of FacePose YOLO structure interpretation, YOLO data format conversion, YOLO process modification"

UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)

flink读取mongodb数据源
![Spark Basics [Introduction, Getting Started with WordCount Cases]](/img/90/ebe887db0f8c36895691dea05f62cf.png)
Spark Basics [Introduction, Getting Started with WordCount Cases]
![[MRCTF2020] PYWebsite](/img/d4/57e8e5ee45b742894679f3f5671516.png)
[MRCTF2020] PYWebsite

【8.4】代码源 - 【数学】【历法】【删库】【不朴素的数列(Bonus)】
![[BJDCTF2020] EasySearch](/img/60/464de3bcdda876171b9f61ad31bff1.png)
[BJDCTF2020] EasySearch

There are several common event handling methods in Swing?How to listen for events?

JeeSite New Report

Swing有几种常用的事件处理方式?如何监听事件?
随机推荐
小程序_动态设置tabBar主题皮肤
不看后悔,appium自动化环境完美搭建
[8.2] Code Source - [Currency System] [Coins] [New Year's Questions (Data Enhanced Edition)] [Three Stages]
1007 Climb Stairs (greedy | C thinking)
10 years of testing experience, worthless in the face of the biological age of 35
[SWPU2019]Web1
dedecms织梦tag标签不支持大写字母修复
Cron(Crontab)--use/tutorial/example
Mysql的redo log详解
UI自动化测试 App的WebView页面中,当搜索栏无搜索按钮时处理方法
MySql index learning and use; (I think it is detailed enough)
The production method of the powered small sailboat is simple, the production method of the electric small sailboat
What is the function of industrial-grade remote wireless transmission device?
将故事写成我们
Bosses, I noticed that a mysql CDC connector parameters scan. The incremental. Sna
[极客大挑战 2019]FinalSQL
关于sklearn库的安装
UE4 第一人称角色模板 添加蹲伏功能
[CISCN2019 South China Division]Web11
how to measure distance from point to face in creo