[英]Accessing AWS S3 using AWS SDK for JavaScript
Right now I am using the javascript SDK to access my s3 bucket and it works fine but I have hard-coded all my credentials in the javascript directly but in the SDK they are saying you can store these in the AWS.config object and I dont know how to do this. 现在我正在使用javascript SDK访问我的s3存储桶它工作正常,但我已经直接在javascript中硬编码了我的所有凭据,但在SDK中他们说你可以将它们存储在AWS.config对象中,我不会知道怎么做。 Also, the online resources are not informative so can someone please let me know how to do this or any other better way to do this instead of hard-coding the credentials? 此外,在线资源不提供信息,所以有人可以让我知道如何做到这一点或任何其他更好的方式来做到这一点,而不是硬编码凭证?
<script type="text/javascript">
AWS.config.accessKeyId = 'dddddddddd';
AWS.config.secretAccessKey = 'rrrrrrrrrrrrrrrreeeeeeeeeeeeeeeeeeeee';
AWS.config.region = 'us-east-1';
// create the AWS.Request object
var bucket = new AWS.S3({ params: { Bucket: 'some.bucket' } });
bucket.listObjects(function (err, data) {
if (err) {
document.getElementById('status').innerHTML =
'Could not load objects from S3';
} else {
document.getElementById('status').innerHTML =
'Loaded ' + data.Contents.length + ' items from S3';
for (var i = 0; i < data.Contents.length; i++) {
document.getElementById('objects').innerHTML +=
'<li>' + data.Contents[i].Key + '</li>';
}
}
});
</script>
I think a better way is to store it in ~/.aws/credentials. 我认为更好的方法是将其存储在〜/ .aws /凭证中。 You can do that by creating the file or using aws configure with the cli command, answer the questions and it will generate this file: 您可以通过创建文件或使用带有cli命令的aws configure来执行此操作,回答问题并生成此文件:
[default]
aws_access_key_id = THEACCESSKEYHERE
aws_secret_access_key = THESECRETACCESSKEYHERE
and ~/.aws/config: 和〜/ .aws / config:
[default]
output = json (or whatever you prefer here)
region = us-east-1 (or whatever region you are using)
This works without having to manually add it for each command or call to the AWS you require. 无需为每个命令手动添加它,也无需调用您需要的AWS。
I believe this is not supported in the browser. 我相信浏览器不支持此功能。 The AWS Javascript SDK is fully supported on node.js. node.js完全支持AWS Javascript SDK。
Read this: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS.html 请阅读: http : //docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS.html
It kind of makes sense, since nobody would want to expose private authentication/authorization information in public. 这是有道理的,因为没有人愿意公开公开私人认证/授权信息。
If you want to get fancy you could potentially pass the credentials using JSONP from another server ( http://en.wikipedia.org/wiki/JSONP ) and restrict access to certain clients using a firewall, but it can get kinda messy. 如果你想得到想象,你可能会使用来自另一台服务器( http://en.wikipedia.org/wiki/JSONP )的JSONP传递凭证,并限制使用防火墙访问某些客户端,但它可能会变得有点混乱。
On a different note, why not expose the S3 bucket publicly for read access since the javascript on the browser is public anyways? 另外,为什么不公开公开S3存储桶以进行读取访问,因为浏览器上的javascript是公共的?
There is a way to achieve JS uploads in the browser without revealing your private credentials. 有一种方法可以在浏览器中实现JS上传,而不会泄露您的私人凭据。 It does however, require some server side logic. 但它需要一些服务器端逻辑。
See answer here: S3 upload directly in JavaScript 请在此处查看答案: S3直接在JavaScript中上传
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.