文件上传到腾讯云cos的简单步骤

简介: 文件上传到腾讯云cos的简单步骤

文件上传到腾讯云cos的简单步骤


简单cos上传的话,看下步骤,思路就一目了然了。

网络异常,图片无法展示
|

1.请求自己服务器的接口 拿到cos的临时秘钥

请求接口,拿到秘钥,秘钥大约拿到这些参数:

网络异常,图片无法展示
|

2. 用临时秘钥初始化一个cos实例

首先确保你有cos插件,参照官方文档

npm i cos-js-sdk-v5

然后,用刚刚的临时秘钥生成实例:

const cosInstance = new COS({
    getAuthorization: (_, callback) => {
      callback(config);
    },
  });

3.上传文件

如果过期的话,重新走上面的步骤。

官方的系列文档,官方的uploadFile接口兼容分片上传,还是蛮方便的。

不过期的话,就可以上传文件了

cosInstance.uploadFile(params) 

这里注意文件名,很多时候是唯一的,所谓的key就是路径+唯一文件名+文件后缀,下面简单示例一个:

// fileObject不是原生的file,而是普通对象,结构在下面文字
const cosUploadFile = (fileObject) => {
  const suffix = fileObject.name.slice(fileObject.name.lastIndexOf('.'));
  const key = cosConfig.Path + getUuid() + suffix
  cosInstance.uploadFile(
    {
      ...cosConfig,
      Key:key,
      Body: fileObject.originFileObj, /* 必须 */
      onProgress(progressData) {
        // 需要显示进度的话
        fileObject.progress = Math.floor(progressData.percent * 100);
      },
    },
    (err) => {
      if (err) {
        console.log(err)
        // 需要显示状态的话
        fileObject.status = 'fail';
        return;
      }
      // 上传成功的话,就有url了
      fileObject.status = 'done';
      fileObject.url = `${cosConfig.ServerName}${key}`;
    },
  );
};
function getUuid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
        const r = Math.random() * 16 | 0;
        const v = c === 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

fileObject一般比较好的数据结构,参考antd:

网络异常,图片无法展示
|

demo:利用antd的upload实现cos上传单个文件

刚入门react,思路仅做参考:

import React, { useState, useEffect } from 'react';
import { Upload, message, Button,Image } from 'antd';
import { PlusOutlined,LoadingOutlined } from '@ant-design/icons';
import COS from 'cos-js-sdk-v5';
// TODO 根据自己的接口调整
export async function getConfigAjax() {
  return {
    state:1,
    success:true,
    data:{
    bucketName:'',
    expiredTime:'',
    region:'',
    serverName:'',
    sessionToken:'',
    startTime:'',
    tmpSecretId:'',
    tmpSecretKey:'',
    uploadPath:'',
  }
  }
}
export default (props) => {
  const [loading, setLoading] = useState(false);
  const [imageUrl, setImageUrl] = useState(props.imageUrl);
  const [cosInstance, setCosInstance] = useState(null);
  const [cosConfig, setCosConfig] = useState(null);
  const [fileList, setFileList] = useState([]);
  const requestCosConfigAndGetCosInstance = async () => {
    const res = (await getConfigAjax()).data;
    const isSuccess = res.success === true;
    if (!isSuccess) {
      message.error('服务器有问题,请稍候再试');
      return;
    }
    const {
      bucketName,
      expiredTime,
      region,
      serverName,
      sessionToken,
      startTime,
      tmpSecretId,
      tmpSecretKey,
      uploadPath,
    } = res.data;
    // Key是path和文件名的拼接 Body是file文件
    const config = {
      TmpSecretId: tmpSecretId,
      TmpSecretKey: tmpSecretKey,
      XCosSecurityToken: sessionToken,
      // 是10位的话就不用再除以1000了
      StartTime: Math.floor(startTime/1000),
      ExpiredTime: Math.floor(expiredTime/1000),
      Bucket: bucketName,
      Region: region,
      Path: uploadPath,
      ServerName: serverName,
    };
    setCosConfig(config);
    const instance = new COS({
      getAuthorization: (_, callback) => {
        callback(config);
      },
    });
    setCosInstance(instance);
  };
  const detectExpired = () => {
    const isExpired = +new Date() >= cosConfig.ExpiredTime * 1000;
    isExpired && requestCosConfigAndGetCosInstance();
  };
  // antd这边的fileObject属性已经封装好,所以可直接使用
  const cosUploadFile = (fileObject) => {
    setLoading(true)
    const suffix = fileObject.name.slice(fileObject.name.lastIndexOf('.'));
    const newName = fileObject.uid + suffix
    const key = cosConfig.Path + newName
    cosInstance.uploadFile(
      {
        ...cosConfig,
        Key:key,
        Body: fileObject.originFileObj,
        onProgress(progressData) {
          console.log(fileObject,progressData)
          fileObject.progress = Math.floor(progressData.percent * 100);
        },
      },
      (err) => {
        setLoading(false)
        if (err) {
          console.log(err)
          fileObj.status = 'fail';
          return;
        }
        fileObject.status = 'done';
        fileObject.url = `${cosConfig.ServerName}${key}`;
        setImageUrl(fileObject.url)
      },
    );
  };
  useEffect(() => {
    requestCosConfigAndGetCosInstance();
  }, []);
  const uploadAttrs = {
    name: 'file',
    className:"file-uploader",
    listType:"picture-card",
    showUploadList:false,
    maxCount:1,
    onChange: ({ fileList }) => {
      console.log('fileList', fileList);
      setFileList(fileList)
      // const { onChange } = props;
      // if (onChange) {
      //   onChange([...fileList]);
      // }
    },
    customRequest: () => {
      detectExpired();
      fileList && fileList.length && cosUploadFile(fileList[0]);
    },
  };
  const uploadButton = (
    <div>
      {loading ? <LoadingOutlined /> : <PlusOutlined/>}
    </div>
  );
  return (
    <Upload { ...uploadAttrs} >
        {imageUrl ? (<Image preview={false} width={200} src={imageUrl} />) : uploadButton}
      </Upload>
  );
};

网络异常,图片无法展示
|

目录
相关文章
|
API 对象存储
腾讯云对象存储cos获取图片像素信息
简述获取图片像素信息的几种方案
腾讯云对象存储cos获取图片像素信息
|
存储 云安全 域名解析
腾讯云COS对象存储攻防
账户中的访问策略包括用户组策略、用户策略、存储桶访问控制列表(ACL)和存储桶策略(Policy)等不同的策略类型。
699 0
腾讯云COS对象存储攻防
|
8月前
|
存储 对象存储 数据安全/隐私保护
typora+腾讯云cos+Picgo图床搭建
typora+腾讯云cos+Picgo图床搭建
60 0
|
12月前
|
存储 API 对象存储
手把手教你Typora图床配置(PicGo+阿里云OSS/腾讯云COS)
手把手教你Typora图床配置(PicGo+阿里云OSS/腾讯云COS)
1876 0
|
存储 网络协议 大数据
腾讯云主机安装COSFS工具并使用COS对象存储
腾讯云主机安装COSFS工具并使用COS对象存储
542 0
腾讯云主机安装COSFS工具并使用COS对象存储
|
存储 Web App开发 安全
从腾讯云COS向华为云OBS迁移数据的解决方案
目前我们的云存储方案除了自己的对象存储外,还集成了腾讯云的COS存储。现在有一个新需求,要支持云存储数据向其他云存储迁移的能力,比如支持把腾讯云COS存储的数据迁移到华为云的OBS存储。这就是本文今天要讨论的主题,关于这个需求,调研得到的一些技术方案分享给大家,希望能帮到有需要的小伙伴。
485 0
Java springboot项目引入腾讯云COS实现上传
Java springboot项目引入腾讯云COS实现上传
|
存储 JavaScript 开发工具
个人图床的最简单制作-腾讯云COS
最终实现效果 在图片上右键上传,自动生成图片URL到剪切板中,任意地方Ctrl+v均可粘贴 在文件上右键上传,自动生成图片URL到剪切板中,任意地方Ctrl+v均可粘贴 准备工作 COS ...
3355 0

热门文章

最新文章