tencentyun / cos-js-sdk-v5

腾讯云 COS JS SDK(XML API)
https://cloud.tencent.com/product/cos
MIT License
329 stars 568 forks source link

服务器签名这一块代码有没有sdk #22

Closed slince closed 5 years ago

slince commented 6 years ago

https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/sts-auth.php

理解起来还是挺费劲的,有没有现成的sdk可以做这个事情,帮助生成签名 就好

carsonxu commented 6 years ago

有的,近期优化这个demo。

  1. 使用 https://github.com/tencentyun/qcloud-cos-sts-sdk 获取临时密钥
  2. 再用 https://github.com/tencentyun/cos-nodejs-sdk-v5 生成签名
slince commented 6 years ago

@carsonxu 有php版本的吗

carsonxu commented 6 years ago

PHP:

  1. 用这个代码获取临时密钥 https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/sts.php
  2. 用 SDK 获取签名:https://github.com/tencentyun/cos-php-sdk-v5
slince commented 6 years ago

@carsonxu

本项目readme上的这段获取授权和qcloud文档上的有什么区别,两端代码分别请求的后端是sts.php和sts-auth.php; 所用到的参数也不完全相同,我是需要按照官方文档上的来还是本项目的demo来

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

https://cloud.tencent.com/document/product/436/9067

   xhr.onload = function (e) {
                var AuthData;
                try {
                    AuthData = JSON.parse(xhr.responseText)
                } catch (e) {}
                if (AuthData && AuthData.Authorization) {
                    callback(null, {
                        Authorization: AuthData.Authorization,
                        XCosSecurityToken: AuthData.XCosSecurityToken,
                    });
                } else {
                    console.error(AuthData);
                    callback('获取签名出错');
                }
            };
            xhr.onerror = function (e) {
                callback('获取签名出错');
            };
slince commented 6 years ago

@carsonxu

我按照cloud,tencent。com 的上面的例子去写,得到这么个结果:

<?xml version='1.0' encoding='utf-8' ?>
<Error>
    <Code>RequestTimeTooSkewed</Code>
    <Message>The difference between the request time and the current time is too large.</Message>
    <Resource>phpdish-1251726071.cos.ap-beijing.myqcloud.com</Resource>
    <RequestId>NWIxZjVlMGNfZDI4NWQ2NF8yMDY2MV8yYjE3M2I=</RequestId>
    <TraceId>OGVmYzZiMmQzYjA2OWNhODk0NTRkMTBiOWVmMDAxODc0OWRkZjk0ZDM1NmI1M2E2MTRlY2MzZDhmNmI5MWI1OTBjYzE2MjAxN2M1MzJiOTdkZjMxMDVlYTZjN2FiMmI0NGYxMTg3ODZiZjg0NjU3NjRlMmYxZWMxZmQwZDM2NGU=</TraceId>
</Error>

麻烦给看下是什么原因。

yuanfangluo commented 5 years ago

有的,近期优化这个demo。

  1. 使用 https://github.com/tencentyun/qcloud-cos-sts-sdk 获取临时密钥
  2. 再用 https://github.com/tencentyun/cos-nodejs-sdk-v5 生成签名

你好, 后端获取临时秘钥再返回给客户端, 客户端直接拿着秘钥就可以调用云存储的接口上传图片了, 那这里面的生成签名是干嘛用的?为啥还要生成签名

carsonxu commented 5 years ago

@luoshuisheng 这里是前端如果不想用SDK,不方便计算签名,就可以让服务端计算好签名给前端用。

yuanfangluo commented 5 years ago

为啥JavaScript的sdk里面有jquery, 建议sdk不要使用第三方库

carsonxu commented 5 years ago

这里是包含了 jquery.ajax.js 库,打包后不依赖 jQuery 也可以运行。

yuanfangluo commented 5 years ago

我在reactnative使用不了,说是有jquery,而reactnative是可以使用ajax的

yuanfangluo commented 5 years ago

@luoshuisheng 这里是前端如果不想用SDK,不方便计算签名,就可以让服务端计算好签名给前端用。

node怎么计算签名给前端用?