华为云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;
    })
},
相关推荐
咕噜企业分发小米2 天前
阿里云与华为云基因测序数据同步的日志内容中,哪些字段对于故障排查最为关键?
数据库·阿里云·华为云
qq_316837754 天前
使用post方式上传文件到华为云obs
华为云
咕噜企业分发小米5 天前
阿里云与华为云基因测序数据同步安全性对比?
阿里云·华为云·云计算
咕噜企业分发小米5 天前
阿里云与华为云基因测序数据同步延迟对比?
阿里云·华为云·云计算
洲洲不是州州5 天前
APP与华为云和设备端的通信
华为云·手机apk·云端通信
咕噜企业分发小米5 天前
阿里云与华为云基因测序数据同步的日志格式是什么?
阿里云·华为云·云计算
咕噜企业分发小米5 天前
阿里云和华为云基因测序数据同步的审计日志如何查看?
阿里云·oracle·华为云
咕噜企业分发小米6 天前
阿里云与华为云基因测序数据分析中如何优化成本?
阿里云·华为云·云计算
咕噜企业分发小米6 天前
阿里云与华为云基因测序数据分析如何实现数据协同?
阿里云·华为云·云计算
咕噜企业分发小米6 天前
阿里云与华为云基因测序数据分析中如何实现数据共享?
阿里云·华为云·云计算