当前位置:网站首页>Tencent cloud, realize image upload
Tencent cloud, realize image upload
2022-07-05 02:45:00 【13 less】
When uploading pictures , There is no current interface , At this time, third-party uploading can be realized , Here is an introduction to the pictures uploaded by Tencent cloud .
One . Configure a Tencent cloud
1. Registered account
... Log in to the official website directly .
2. Create a bucket
3. Create cross domain rules
The function of the key :
You need a key when uploading files .
Two .element-ui Upload files
<el-upload
class="avatar-uploader"
action="https://jsonplaceholder.typicode.com/posts/"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload">
<img v-if="imageUrl" :src="imageUrl" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
Important parameters :
How to upload files
Upload successful
Override default behavior , Custom upload
Be careful :
http-request:action invalid , And the value after successful upload will also be invalid .
3、 ... and . Tencent cloud upload
1. Installing a plug-in
cos-js-sdk-v5 yarn add cos-js-sdk-v5
2: Import
import Cos from 'cos-js-sdk-v5'
3: Instantiation
let cos = new Cos({
SecretId: 'COS_SECRETID', // Identity recognition ID
SecretKey: 'COS_SECRETKEY', // Identity key
});
Search the key directly to get :
4: Use
cos.putObject({
Bucket: 'XXXXXXXXXXXXXXXXXXX', /* bucket : */
Region: 'ap-guangzhou', /* The area where the bucket is located , Required fields */
Key: uid+'', /* Must be a string */
StorageClass: 'STANDARD', // Fixed value
Body: File object , // Upload file object
onProgress: (progressData)=> {
// Upload progress
console.log(JSON.stringify(progressData));
}
}, (err, data)=> {
// Callback for upload success or failure
console.log(err || data);
});
Bucket:
Where it is :
Key:
Use the uid
Region:
Region: 'ap-chongqing',
Four . Actual operation
<template>
<div class="">
<el-upload
class="avatar-uploader"
action="#"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload"
:http-request="httpRequest"
>
<img v-if="imageUrl" :src="imageUrl" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</div>
</template>
<script>
import Cos from 'cos-js-sdk-v5'
let cos = new Cos({
SecretId:'xxxxxxxxxxxxxxxxxxxxxxx', // Identity recognition ID
SecretKey:'xxxxxxxxxxxxxxxxx', // Identity key
});
export default {
data() {
return {
imageUrl: ''
};
},
methods: {
httpRequest(res){
cos.putObject({
Bucket: 'xxxxxxxxxxxx', /* bucket : */
Region: 'ap-chongqing', /* The area where the bucket is located , Required fields */
Key: res.file.uid+'', /* Must be a string */
StorageClass: 'STANDARD', // Fixed value
Body:res.file, // Upload file object
onProgress: (progressData)=> {
// Upload progress
console.log(JSON.stringify(progressData));
}
},(err,data)=>{
// console.log(err||data);
this.imageUrl='http://'+data.Location
});
},
handleAvatarSuccess(res, file) {
this.imageUrl = URL.createObjectURL(file.raw);
},
beforeAvatarUpload(file) {
const isPng=file.type==='image/png'
const limit=file.size/1024/1024<1
if (!isPng) {
alert(' Please upload png picture ')
}
if (!limit) {
alert(' Please upload 1M Inside picture ')
}
return isPng && limit
}
}
}
</script>
<style>
.avatar-uploader .el-upload {
border: 1px dashed #d9d9d9;
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
}
.avatar-uploader .el-upload:hover {
border-color: #409EFF;
}
.avatar-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 178px;
height: 178px;
line-height: 178px;
text-align: center;
}
.avatar {
width: 178px;
height: 178px;
display: block;
}
</style>
picture :
Look, everyone , Ongoing update .
边栏推荐
- 2.常见的请求方法
- [技术发展-26]:新型信息与通信网络的数据安全
- Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal
- Sqoop命令
- Character painting, I use characters to draw a Bing Dwen Dwen
- College Students' innovation project management system
- [机缘参悟-38]:鬼谷子-第五飞箝篇 - 警示之一:有一种杀称为“捧杀”
- LeetCode --- 1071. Great common divisor of strings problem solving Report
- Master Fur
- Apache Web page security optimization
猜你喜欢
[uc/os-iii] chapter 1.2.3.4 understanding RTOS
Spark SQL learning bullet 2
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Hmi-31- [motion mode] solve the problem of picture display of music module
【LeetCode】106. Construct binary tree from middle order and post order traversal sequence (wrong question 2)
Design and implementation of campus epidemic prevention and control system based on SSM
Design and implementation of community hospital information system
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
[技术发展-26]:新型信息与通信网络的数据安全
看 TDengine 社区英雄线上发布会,听 TD Hero 聊开发者传奇故事
随机推荐
Design and implementation of campus epidemic prevention and control system based on SSM
Last words record
Talk about the things that must be paid attention to when interviewing programmers
Marubeni Baidu applet detailed configuration tutorial, approved.
From task Run get return value - getting return value from task Run
Hmi-31- [motion mode] solve the problem of picture display of music module
Apache Web page security optimization
Summary and practice of knowledge map construction technology
Idea inheritance relationship
Pytest (4) - test case execution sequence
Six stone programming: advantages of automated testing
When the low alcohol race track enters the reshuffle period, how can the new brand break the three major problems?
Yolov5 model training and detection
Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘
PHP cli getting input from user and then dumping into variable possible?
Kotlin - 协程 Coroutine
Spoon inserts and updates the Oracle database, and some prompts are inserted with errors. Assertion botch: negative time
8. Commodity management - commodity classification
【LeetCode】111. Minimum depth of binary tree (2 brushes of wrong questions)
Asp+access campus network goods trading platform