当前位置:网站首页>Upload pictures to qiniu cloud through the web call interface
Upload pictures to qiniu cloud through the web call interface
2022-07-23 11:56:00 【Xu Tongbao】
Upload the domain name used :
https://developer.qiniu.com/kodo/1671/region-endpoint-fq
Use node Generate upload credentials :
https://developer.qiniu.com/kodo/1289/nodejs#5

const qiniu = require('qiniu')
// Get upload credentials
const uploadGetToken = (req, res) => {
const { accessKey, secretKey } = getQiNiuKey()
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
const putPolicy = new qiniu.rs.PutPolicy({
scope: 'xutongbao-video',
returnBody: `{
"code": 200,
"data": {"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","file":"$(fname)"},
"message": " success "
}`
})
const uploadToken = putPolicy.uploadToken(mac)
res.send({
code: 200,
data: {
token: uploadToken
},
message: ' success '
})
}

encapsulation andt Upload components :
import React from 'react'
import { Button, Upload, message } from 'antd'
import { v4 as uuidv4 } from 'uuid'
import {
imageUrlFormat,
uploadGetTokenFromLocalStorage,
} from '../../utils/tools'
import urls from '../../api/urls'
export default function UploadImgToCND({
value = '',
msg,
type = 'add',
onChange,
accept = '*.*',
imgUrlCnd,
}) {
const imageUrl = imageUrlFormat(imgUrlCnd)
let defaultValue = {
uid: '-1',
name: imageUrl,
status: 'done',
url: imageUrl,
}
const fileList = []
if (value) {
fileList.push(defaultValue)
}
const uploadProps = {
name: 'file',
action: urls.light.uploadToCDN,
data: (file) => {
const uid = uuidv4()
const reslutIndex = Array.from(file.name).findLastIndex(item => item === '.')
const fileName = uid + file.name.slice(reslutIndex, file.name.length)
return {
key: `img/${fileName}`,
fname: fileName,
token: uploadGetTokenFromLocalStorage(),
}
},
headers: {},
maxCount: 1,
listType: 'picture',
defaultFileList: [...fileList],
accept,
onChange(info) {
if (info.file.status !== 'uploading') {
console.log(info.file, info.fileList)
onChange(undefined)
}
if (info.file.status === 'done') {
message.success(`${info.file.name} Upload successful `)
if (info.file.response.code === 200) {
// console.log(info.file.xhr.responseURL)
// const imgUrl = `${getHost(info.file.xhr.responseURL)}/${info.file.response.data.filename}`
// console.log(imgUrl)
onChange(info.file.response.data.key)
}
} else if (info.file.status === 'uploading') {
} else if (info.file.status === 'error') {
message.error(`${info.file.name} Upload failed `)
}
},
}
return (
<span>
{type !== 'check' ? (
<Upload {...uploadProps}>
<Button> To upload pictures </Button>
<span className="m-upload-text">{msg}</span>
</Upload>
) : (
value && (
<img
src={imageUrl}
alt={imageUrl}
className="m-upload-img-check"
></img>
)
)}
</span>
)
}
Use components :
<Form.Item label=" Head portrait " name="avatar">
<UploadImgToCND imgUrlCnd={initValues.avatarCnd}></UploadImgToCND>
</Form.Item>
<Form.Item label=" cover " name="coverImage">
<UploadImgToCND imgUrlCnd={initValues.coverImageCnd}></UploadImgToCND>
</Form.Item>Link to the path of the picture cdn link :
// Get encrypted picture links
const getSecrecyImgUrl = (key) => {
if (key) {
const { accessKey, secretKey } = getQiNiuKey()
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
const config = new qiniu.conf.Config()
const bucketManager = new qiniu.rs.BucketManager(mac, config)
//var privateBucketDomain = 'http://rewjm2mrh.hb-bkt.clouddn.com';
const privateBucketDomain = 'http://cdn.xutongbao.top'
const deadline = parseInt(Date.now() / 1000) + 3600 * 24 // 24 Hours expired
//const key = 'video/2.mp4'
const privateDownloadUrl = bucketManager.privateDownloadUrl(
privateBucketDomain,
key,
deadline
)
return privateDownloadUrl
} else {
return ''
}
}
Call the interface of qiniu cloud when uploading , You need to bring token. The path links of pictures are saved in the database , Not seven cattle cloud cdn Picture links , When accessing the list, you need to turn the path link of the image into cdn Link to the front end . When the front end modifies the image, it still only modifies the path and link .
边栏推荐
- 2. MySQL data management - DML (add, modify, delete data)
- Data warehouse 4.0 notes - user behavior data collection IV
- NFT digital collection development: Jingdong "Qida bear takes you to the capital" tourism package
- Websocket long connection
- NFT digital collection system development, development trend of Digital Collections
- Data warehouse 4.0 notes - user behavior data collection I
- UE4解决WebBrowser无法播放H.264的问题
- Stage 1 Review
- 强迫症的硬盘分区
- 循环队列
猜你喜欢

数仓4.0笔记---用户行为数据生成

Adding environment variables and templates to systemctl service

NFT digital collection development: what are the possible application scenarios of digital collections in the future?

NFT digital collection development: Jingdong "Qida bear takes you to the capital" tourism package

Data warehouse 4.0 notes - user behavior data collection IV

8、 Collection framework and generics

Develop necessary idea use

Data warehouse 4.0 notes - user behavior data collection I

11. Multithreading

数仓4.0笔记——用户行为数据采集二
随机推荐
强迫症的硬盘分区
Accumulate SQL by date
8、 Collection framework and generics
10. I/o input / output stream
规范数据库设计
互联网通信
VMware uses wireless network card NAT to access the Internet under Windows
第一个FLINK程序之WordCount
1.认识数据库
MySQL invalid conn troubleshooting
[monitoring deployment practice] display the charts of Prometheus and loki+promtail based on granfana
数仓4.0笔记——业务数据采集
[system problems] Net Framework 3.5 installation error
Installation and process creation of activiti app used by activiti workflow
1、MySQL初体验
Service服务
3. DQL (data query statement)
数仓4.0笔记——用户行为数据采集二
BST树
9、 Practical class