在AWS JavaScript SDK中设置签名 [英] Set signature in AWS JavaScript SDK

查看:162
本文介绍了在AWS JavaScript SDK中设置签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AWS JavaScript SDK发出AWS请求时,是否可以手动设置签名?我想直接将文件上传到S3,但是我不想将纯文本的密钥提供给客户端.所有官方示例都要求您对密钥和秘密进行硬编码(同时明确告诉您不要这样做).我正在计算签名服务器端,但是还没有找到将签名添加到SDK中的方法.

Is it possible to manually set the signature when making an AWS request using the AWS JavaScript SDK? I want to upload files directly to S3 but I don't want to make my keys available in plain text to the client. All the official examples require you to hardcode both your key and secret (while explicitly telling you not to do that). I'm calculating the signature server side but I haven't found a way to get that signature into the SDK.

推荐答案

为此,我最终使用AWS STS走了一条不同的路线.但是,如果您确实想执行以下操作,则可以使用以下解决方案:

I ended up going a different route using AWS STS for this. However, if you do want to do something like this here is the solution:

使用AWS JavaScript SDK,它们使您可以预订AWS.Request对象上的请求构建事件".这些事件(validatebuildsign)将允许您在发送请求之前访问和修改请求状态(标题,正文等).大多数调用将返回AWS.Request实例(S3的.upload除外).

Using the AWS JavaScript SDK they allow you to subscribe to "Request Building Events" on a AWS.Request object. These events (validate, build and sign) will allow you to access and modify request state (headers, body, etc.) before the request is sent. Most calls will return an AWS.Request instance (except .upload for S3).

这是一个例子:

var request = s3.putObject({
  Bucket: 'MyBucket',
  Key: file.name,
  ContentType: file.contentType,
  Body: file
});

request.on('sign', function() {
  request.httpHeaders.Authorization = 'AWS Signature';
});

request.send(function(err, data) {
  // Do something here
});

我相信这里的窍门是在创建请求(在我的示例中为.putObject)并使用.send方法发起请求时不指定回调.

I believe the trick here is to not specify a callback when creating the request (.putObject in my example) and using the .send method to initiate the request.

文档: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Request.html

这篇关于在AWS JavaScript SDK中设置签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆