当前位置:网站首页>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-v52: 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 .
边栏推荐
- 100 basic multiple choice questions of C language (with answers) 04
- Apache build web host
- [技术发展-26]:新型信息与通信网络的数据安全
- Yolov5 model training and detection
- Day_ 17 IO stream file class
- Practical case of SQL optimization: speed up your database
- 【LeetCode】501. Mode in binary search tree (2 wrong questions)
- [understanding of opportunity -38]: Guiguzi - Chapter 5 flying clamp - warning one: there is a kind of killing called "killing"
- Application and Optimization Practice of redis in vivo push platform
- this+闭包+作用域 面试题
猜你喜欢

【LeetCode】98. Verify the binary search tree (2 brushes of wrong questions)

Day_ 17 IO stream file class

Learn game model 3D characters, come out to find a job?

Watch the online press conference of tdengine community heroes and listen to TD hero talk about the legend of developers

Azkaban实战

Vb+access hotel service management system

Avoid material "minefields"! Play with super high conversion rate

Bumblebee: build, deliver, and run ebpf programs smoothly like silk

Chinese natural language processing, medical, legal and other public data sets, sorting and sharing

Devtools的简单使用
随机推荐
Linux安装Redis
[Yu Yue education] National Open University spring 2019 0505-22t basic nursing reference questions
College Students' innovation project management system
tuple and point
[技术发展-26]:新型信息与通信网络的数据安全
Devtools的简单使用
The database and recharge are gone
[daily problem insight] Li Kou - the 280th weekly match (I really didn't know it could be so simple to solve other people's problems)
Day_ 17 IO stream file class
ELK日志分析系统
2021 Li Hongyi machine learning (1): basic concepts
Use the difference between "Chmod a + X" and "Chmod 755" [closed] - difference between using "Chmod a + X" and "Chmod 755" [closed]
Open source SPL optimized report application coping endlessly
LeetCode 314. Binary tree vertical order traversal - Binary Tree Series Question 6
Learn game model 3D characters, come out to find a job?
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Avoid material "minefields"! Play with super high conversion rate
Pytorch register_ Hook (operate on gradient grad)
Unpool(nn.MaxUnpool2d)
Last week's hot review (2.7-2.13)