Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
slince opened this issue Jun 12, 2018 · 11 comments
Closed

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

slince opened this issue Jun 12, 2018 · 11 comments

Comments

@slince
Copy link

slince commented Jun 12, 2018

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

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

@carsonxu
Copy link
Collaborator

carsonxu commented Jun 12, 2018

有的,近期优化这个demo。

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

@slince
Copy link
Author

slince commented Jun 12, 2018

@carsonxu 有php版本的吗

@carsonxu
Copy link
Collaborator

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
Copy link
Author

slince commented Jun 12, 2018

@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
Copy link
Author

slince commented Jun 12, 2018

@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
Copy link

有的,近期优化这个demo。

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

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

@carsonxu
Copy link
Collaborator

carsonxu commented Oct 18, 2018

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

@yuanfangluo
Copy link

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

@carsonxu
Copy link
Collaborator

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

@yuanfangluo
Copy link

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

@yuanfangluo
Copy link

yuanfangluo commented Oct 18, 2018

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants