华为云OBS-文件上传

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

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

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

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;
}

文件上传函数

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;
    });

}

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

// 获取上传临时路径
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)
        })
},

返回的接口内容为

{
   
    "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 中的规范

//  请求上传数据
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. 华为】SpringBoot + OBS 文件

    2024-01-27 08:18:01       41 阅读
  2. 华为OBS-文件

    2024-01-27 08:18:01       35 阅读
  3. 阿里文件

    2024-01-27 08:18:01       35 阅读
  4. 【SpringBoot】文件到阿里

    2024-01-27 08:18:01       37 阅读
  5. 文件到阿里

    2024-01-27 08:18:01       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-27 08:18:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-27 08:18:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-27 08:18:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-27 08:18:01       18 阅读

热门阅读

  1. react的高阶函数HOC:

    2024-01-27 08:18:01       29 阅读
  2. flink-cdc实战之oracle问题记录01

    2024-01-27 08:18:01       39 阅读
  3. 需求分析师岗位的基本职责文本(合集)

    2024-01-27 08:18:01       24 阅读
  4. B3847 [GESP样题 一级] 当天的第几秒 题解

    2024-01-27 08:18:01       26 阅读
  5. Go 通过 goroutines 实现类似线程池的模式

    2024-01-27 08:18:01       27 阅读
  6. css flex布局详解

    2024-01-27 08:18:01       33 阅读
  7. trino-435: trino接入TIDB数据源

    2024-01-27 08:18:01       32 阅读