图床上传
sonder 默认

由于业务需要用到图床上传于是找到了一个比较好用的图床,https://imgbb.com/

这里仅记录通过API上传

  1. 通过邮箱注册账号
  2. 注册完以后参考文档 https://api.imgbb.com/

先获取key: 6f76b23f17axxxxxxxxxx

上传图片接口: https://api.imgbb.com/1/upload

有四个参数

1
2
3
4
5
6
const data = {
key: '', // 你申请到的key
image: '', // 所要上传的图片 可以是base64 也可以是一个url 也可以是binary file
name: '', // 图片名称 该名称是访问外链的图片名称 同名将覆盖
expiration: 60 // 过期时间, 60-15552000 单位:秒数
}

这里例子是使用如果是在云函数中调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const key = '6f76b23f17axxxxxxxxxx'
const baseUrl = `https://api.imgbb.com/1/upload?key=${key}`
const res = await uniCloud.httpclient.request(baseUrl, {
method: 'POST',
dataAsQueryString:false,
data: {
key: key,
image: event.base64, // 如果是base64 必须使用 post
name: '60s1',
expiration: 600
},
headers: {
"Content-Type": "application/x-www-form-urlencoded" // 否则不会将base64上传
},
timeout: 30*1000,
dataType: 'json'
})

返回的结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 返回的结果
{
"data": {
"id": "2ndCYJK",
"title": "c1f64245afb2",
"url_viewer": "https://ibb.co/2ndCYJK",
"url": "https://i.ibb.co/w04Prt6/c1f64245afb2.gif",
"display_url": "https://i.ibb.co/98W13PY/c1f64245afb2.gif",
"width":"1",
"height":"1",
"size": "42",
"time": "1552042565",
"expiration":"0",
"image": {
"filename": "c1f64245afb2.gif",
"name": "c1f64245afb2",
"mime": "image/gif",
"extension": "gif",
"url": "https://i.ibb.co/w04Prt6/c1f64245afb2.gif",
},
"thumb": {
"filename": "c1f64245afb2.gif",
"name": "c1f64245afb2",
"mime": "image/gif",
"extension": "gif",
"url": "https://i.ibb.co/2ndCYJK/c1f64245afb2.gif",
},
"medium": {
"filename": "c1f64245afb2.gif",
"name": "c1f64245afb2",
"mime": "image/gif",
"extension": "gif",
"url": "https://i.ibb.co/98W13PY/c1f64245afb2.gif",
},
"delete_url": "https://ibb.co/2ndCYJK/670a7e48ddcb85ac340c717a41047e5c"
},
"success": true,
"status": 200
}
  • 本文标题:图床上传
  • 本文作者:sonder
  • 创建时间:2022-11-29 16:04:39
  • 本文链接:https://sonderss.github.io/2022/11/29/图床上传/
 评论