GithubHelp home page GithubHelp logo

heaven-s / cos-js-sdk-v5 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tencentyun/cos-js-sdk-v5

0.0 0.0 0.0 2.67 MB

腾讯云 COS JS SDK(XML API)

Home Page: https://cloud.tencent.com/product/cos

License: MIT License

JavaScript 84.44% PHP 1.81% HTML 12.66% CSS 1.10%

cos-js-sdk-v5's Introduction

cos-js-sdk-v5

腾讯云 COS JS SDK(XML API

releases and changelog

Get started

一、前期准备

  1. 首先,JS SDK 需要浏览器支持基本的 HTML5 特性,以便支持 ajax 上传文件和计算文件 md5 值。
  2. COS对象存储控制台 创建存储桶,得到 Bucket(存储桶名称) 和 Region(地域名称)
  3. 控制台密钥管理 获取您的项目 SecretId 和 SecretKey
  4. 配置 CORS 规则,配置例子如下图:

cors

二、计算签名

由于签名计算放在前端会暴露 SecretId 和 SecretKey,我们把签名计算过程放在后端实现,前端通过 ajax 向后端获取签名结果,正式部署时请再后端加一层自己网站本身的权限检验。

这里提供 PHP 和 NodeJS 的签名例子,其他语言,请参照对应的 XML SDK

三、上传例子

  1. 创建 test.html,填入下面的代码,修改里面的 Bucket 和 Region。
  2. 部署好后端的签名服务,并修改 getAuthorization 里的签名服务地址
  3. 把 test.html 放在 Web 服务器下,然后在浏览器访问页面,测试文件上传
<input id="file-selector" type="file">
<script src="dist/cos-js-sdk-v5.min.js"></script>
<script>
var Bucket = 'test-1250000000';
var Region = 'ap-guangzhou';

// 初始化实例
var cos = new COS({
    getAuthorization: function (options, callback) {
        var url = '../server/sts.php'; // 这里替换成您的服务接口地址
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.onload = function (e) {
            try {
                var data = JSON.parse(e.target.responseText);
            } catch (e) {
            }
            callback({
                TmpSecretId: data.credentials && data.credentials.tmpSecretId,
                TmpSecretKey: data.credentials && data.credentials.tmpSecretKey,
                XCosSecurityToken: data.credentials && data.credentials.sessionToken,
                ExpiredTime: data.expiredTime,
            });
        };
        xhr.send();
    }
});

// 监听选文件
document.getElementById('file-selector').onchange = function () {

    var file = this.files[0];
    if (!file) return;

    // 分片上传文件
    cos.sliceUploadFile({
        Bucket: Bucket,
        Region: Region,
        Key: file.name,
        Body: file,
        onHashProgress: function (progressData) {
            console.log('校验中', JSON.stringify(progressData));
        },
        onProgress: function (progressData) {
            console.log('上传中', JSON.stringify(progressData));
        },
    }, function (err, data) {
        console.log(err, data);
    });

};
</script>

webpack 引入方式

支持 webpack 打包的场景,可以用 npm 引入作为模块

npm i cos-js-sdk-v5 --save

Start Demo

1. git clone cos-js-sdk-v5 至本地
2. cd cos-js-sdk-v5
3. 修改 server 文件夹中 sts.js 或 sts.php 中的 secretId、secretKey、bucket、region 配置
4. npm run sts.js # 用 node 启动服务
4. 浏览器输入 http://127.0.0.1:3000/ 即可进行 demo 演示

说明文档

使用例子

快速入门

接口文档

cos-js-sdk-v5's People

Contributors

annexwu avatar carsonxu avatar galen-yip avatar haochuan9421 avatar konakonall avatar na57 avatar tinnazhang avatar wonder233 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.