华为云OBS-文件上传

前端配合后端 采用临时上传 相关参考文档

使用临时URL进行授权访问_对象存储服务 OBS_BrowserJS_临时授权访问_华为云

选择文件方法 【 isPay 是否上传完毕】

javascript 复制代码
handleChange(file, fileList) {
    this.active = 0;
    this.json_data = [];
    console.log(file, fileList);
    fileList.forEach((item) => {
        item.client = null;
        item.isPlay = false;
        item.isLoading = false;
        item.abortCheckpoint = false;
    });

    this.fileList = fileList;
    this.file = file.raw;
    this.uploadDisabled = false;
    this.pauseDisabled = this.resumeDisabled = true;
}

文件上传函数

javascript 复制代码
async submitForm() {
    if (!this.file) {
        this.$message.error("请选择文件");
        return;
    }

    this.fileList.forEach(async (item,) => {
        item.isLoading = true;
        let formData = new FormData();
        formData.append("file", item.raw);
        const json_data = formData.get('file');

        await this.getUploadUrl(item, json_data)
        item.isLoading = false;
    });

}

获取文件上传的路径 【后端接口】

javascript 复制代码
// 获取上传临时路径
async getUploadUrl(item, json_data) {
    const targetObj = {
        "objectKey": item.name,
        "contentType": item.raw.type
    }
    await getLink({...targetObj})
        .then(res => {
            console.log(res.data, 'res')
            const {actualSignedRequestHeaders, signedUrl} = res.data;
            this.action = signedUrl;
            this.actualSignedRequestHeaders = actualSignedRequestHeaders
            this.sendRequest(item, json_data)
        })
},

返回的接口内容为

json 复制代码
{
    "actualSignedRequestHeaders":{
        "Host":"XXXXXXXX.obs.cn-north-9.myhuaweicloud.com:443",
        "Content-Type":"text/plain"
    },
    "signedUrl":"https://XXXXXXXX.obs.cn-north-9.myhuaweicloud.com:443/?AccessKeyId=CBTB9PUIERYBKGWDDESF&Expires=1706249028&Signature=Q20YrJkCFamPI6L1uKrvVakQMjA%3D"
}

上传文件并获取文件地址

【这里需要注意的点是 axios 的请求头】

【不要把host写在header里面,不然会出现 Refused to set unsafe header "Host" 问题】

【会和 w3c 中的规范有冲突------ w3c 中的规范

javascript 复制代码
//  请求上传数据
async sendRequest(item, json_data) {
    var method = 'PUT';
    var reopt = {
        method: method,
        url: this.action,
        withCredentials: false,
        headers: {
            "Content-Type": this.actualSignedRequestHeaders[
                "Content-Type"
            ]
        } || {},
        validateStatus: function (status) {
            return status >= 200;
        },
        maxRedirects: 0,
        responseType: 'text',
        data: json_data,
        // 进度条
        onUploadProgress: (progressEvent) => {
            console.log(progressEvent, 'progressEvents')
            if (progressEvent.lengthComputable) {
                item.percentage = progressEvent.loaded / progressEvent.total * 100;
            }
        }
    };
    console.log(reopt, 'action')
    uploadFile({...reopt
    }).then(async (res) => {
        // 这里没有返回结果
        // 生成excel 文件
        const sendObj = {
            name: item.name,
            url: `https: //${this.actualSignedRequestHeaders.Host}/${item.name}`,
        };
        this.json_data.push(sendObj);
        let newJSON = JSON.stringify(this.json_data);
        let SCnewJSON = Utils.encrypt(newJSON);
        // 异步操作
        window.localStorage.setItem(`woData`, SCnewJSON);
        this.fileNames = `${this.getToday()
        }.xls`;
        this.active = 1;
    })
},
相关推荐
容器魔方1 天前
Karmada v1.18 版本发布!新增混合云溢出式调度能力
人工智能·云原生·容器·华为云·云计算
DS小龙哥3 天前
基于ESP32设计的智能养蜂监测系统
stm32·单片机·嵌入式硬件·物联网·华为云
江华森4 天前
JumpServer v4.10.16-ce 华为云 ECS 实战部署全记录
华为云
云智慧AIOps社区4 天前
云智慧Cloudwise 亮相华为云 × 霞光社中企私享会,Qreel 重构 AI 短剧出海新范式
人工智能·华为云·ai短剧·短剧创作
码农翻身4 天前
英伟达向左,华为云向右:AI数据中心该走哪条路?
人工智能·华为云
PPIO派欧云4 天前
PPIO亮相华为云创想者大会:以Agentic Cloud加速中国AI走向全球
人工智能·华为云
DS小龙哥5 天前
基于STM32与华为云的智能康养木屋环境监测与控制系统
stm32·嵌入式硬件·华为云
halazi10010 天前
如何在华为云上开通MaaS服务并创建API Key,并在CodeArts Agent中配置使用API Key
华为云·api·tokens
宋冠巡12 天前
华为云开发桌面OpenEuler搭建Nginx服务器实操记录
服务器·nginx·华为云
m0_7360348512 天前
华为云使用
华为云