此SDK是对uni-cloud-storage
的扩展,支持自定义域名。
仅用于个人项目(官方明确说明不可用于图床相关操作)
官方说明:https://ask.dcloud.net.cn/article/39390
目的:此项目是有一些处理需要交给服务端进行处理所以需要客户端将文件传给服务端后判断是否需要上传等一系列操作
需要存在node环境 npm :
npm i uni-cloud-storage-adapter
Yarn :
yarn add uni-cloud-storage-adapter
浏览器 ( script )
<script src="https://unpkg.com/uni-cloud-storage-adapter/dist/index.js"></script>
node
环境下 file
参数需要传入文件<Buffer>
并且需要传递文件名称:(fileName.png)
// 导入包
const UniCloudStorage = require('uni-cloud-storage-adapter');
// 初始化(两个必传参数,一个可选参数)
const unics = new UniCloudStorage(spaceId, clientSecret, host);
// 使用
unics.upload(file,fileName?):Promise
将File
类型文件直接传入就可以了无需传入文件名称
// 导入包
<script src="https://unpkg.com/uni-cloud-storage-adapter/dist/index.js"></script>
<script>
// 初始化(两个必传参数)
const unics = new UniCloudStorage(spaceId, clientSecret, host);
</script>
解决跨域问题:
server {
listen 80;
listen 443 ssl http2;
server_name apibspapp.xxxxxx.com;
if ($scheme = http ) {
return 301 https://$host$request_uri;
}
access_log /var/log/nginx/xxxxxx.com.access.log main;
ssl_certificate /Data/code/soso/ssl/fullchain.cer;
ssl_certificate_key /Data/code/soso/ssl/xxxxxx.com.key;
# curl https://ssl-config.mozilla.org/ffdhe2048.txt > /Data/code/soso/ssl/dhparam
ssl_dhparam /Data/code/soso/ssl/dhparam;
location / {
# 加上下面这一行就够了
add_header Access-Control-Allow-Origin * always;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With, token';
add_header Access-Control-Max-Age 3600;
if ($request_method = 'OPTIONS') {
return 204;
}
# 加上下面这一行就够了
proxy_pass https://api.bspapp.com;
# 以下配置,是选配,不加也能实现反向代理
# 指定头部:
proxy_ssl_server_name on;
proxy_hide_header Access-Control-Allow-Origin;
#proxy_hide_header Content-Type;
proxy_set_header Referer "https://api.bspapp.com";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header User-Agent $http_user_agent;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Accept-Encoding "";
proxy_set_header Accept-Language "zh-CN";
proxy_intercept_errors on; # 自定义404 500等
}
error_page 400 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
将File
类型文件直接传入就可以了无需传入文件名称
// 导入包
import UniCloudStorage from 'uni-cloud-storage-adpater';
{
"success": true,
"data": {
"id": "",
"cdnDomain": "",
"signature": "",
"policy": "",
"accessKeyId": "",
"ossPath": "021de.png",
"host": "",
"target": "https://vkceyugu.cdn.bspapp.com/V1de.png"
}
}
{
"success": false,
"error":{
"code":"uploadFileError",
"message":"上传文件失败"
}
}
Lexical is MIT licensed.