Access AWS S3 using the AWS SDK for JavaScript

Right now I use the javascript SDK to access my s3 bucket and it works fine, but I hardcoded all my credentials directly in javascript, but in the SDK they say that you can store them in the AWS.config object and I don’t know how to do it. Also, online resources are not informative, so can someone tell me how to do this or some other way to do this instead of hard-coding 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>
+4
source share
3 answers

, - ~/.aws/credentials. , aws configure cli, :

[default]
aws_access_key_id = THEACCESSKEYHERE
aws_secret_access_key = THESECRETACCESSKEYHERE

~/.aws/config:

[default]
output = json (or whatever you prefer here)
region = us-east-1 (or whatever region you are using)

AWS.

+1

, . SDK AWS Javascript node.js.

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

, /.

, JSONP (http://en.wikipedia.org/wiki/JSONP) , .


, S3 , javascript ?

0

JS , . .

. : S3 JavaScript

0

All Articles