[javascript] Amazon S3 direct file upload from client browser - private key disclosure

You can do this by AWS S3 Cognito try this link here :

http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-examples.html#Amazon_S3

Also try this code

Just change Region, IdentityPoolId and Your bucket name

_x000D_
_x000D_
<!DOCTYPE html>_x000D_
<html>_x000D_
_x000D_
<head>_x000D_
    <title>AWS S3 File Upload</title>_x000D_
    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js"></script>_x000D_
</head>_x000D_
_x000D_
<body>_x000D_
    <input type="file" id="file-chooser" />_x000D_
    <button id="upload-button">Upload to S3</button>_x000D_
    <div id="results"></div>_x000D_
    <script type="text/javascript">_x000D_
    AWS.config.region = 'your-region'; // 1. Enter your region_x000D_
_x000D_
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({_x000D_
        IdentityPoolId: 'your-IdentityPoolId' // 2. Enter your identity pool_x000D_
    });_x000D_
_x000D_
    AWS.config.credentials.get(function(err) {_x000D_
        if (err) alert(err);_x000D_
        console.log(AWS.config.credentials);_x000D_
    });_x000D_
_x000D_
    var bucketName = 'your-bucket'; // Enter your bucket name_x000D_
    var bucket = new AWS.S3({_x000D_
        params: {_x000D_
            Bucket: bucketName_x000D_
        }_x000D_
    });_x000D_
_x000D_
    var fileChooser = document.getElementById('file-chooser');_x000D_
    var button = document.getElementById('upload-button');_x000D_
    var results = document.getElementById('results');_x000D_
    button.addEventListener('click', function() {_x000D_
_x000D_
        var file = fileChooser.files[0];_x000D_
_x000D_
        if (file) {_x000D_
_x000D_
            results.innerHTML = '';_x000D_
            var objKey = 'testing/' + file.name;_x000D_
            var params = {_x000D_
                Key: objKey,_x000D_
                ContentType: file.type,_x000D_
                Body: file,_x000D_
                ACL: 'public-read'_x000D_
            };_x000D_
_x000D_
            bucket.putObject(params, function(err, data) {_x000D_
                if (err) {_x000D_
                    results.innerHTML = 'ERROR: ' + err;_x000D_
                } else {_x000D_
                    listObjs();_x000D_
                }_x000D_
            });_x000D_
        } else {_x000D_
            results.innerHTML = 'Nothing to upload.';_x000D_
        }_x000D_
    }, false);_x000D_
    function listObjs() {_x000D_
        var prefix = 'testing';_x000D_
        bucket.listObjects({_x000D_
            Prefix: prefix_x000D_
        }, function(err, data) {_x000D_
            if (err) {_x000D_
                results.innerHTML = 'ERROR: ' + err;_x000D_
            } else {_x000D_
                var objKeys = "";_x000D_
                data.Contents.forEach(function(obj) {_x000D_
                    objKeys += obj.Key + "<br>";_x000D_
                });_x000D_
                results.innerHTML = objKeys;_x000D_
            }_x000D_
        });_x000D_
    }_x000D_
    </script>_x000D_
</body>_x000D_
_x000D_
</html>
_x000D_
_x000D_
_x000D_

For more details, Please check - Github

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to amazon-web-services

How to specify credentials when connecting to boto3 S3? Is there a way to list all resources in AWS Access denied; you need (at least one of) the SUPER privilege(s) for this operation Job for mysqld.service failed See "systemctl status mysqld.service" What is difference between Lightsail and EC2? AWS S3 CLI - Could not connect to the endpoint URL boto3 client NoRegionError: You must specify a region error only sometimes How to write a file or data to an S3 object using boto3 Missing Authentication Token while accessing API Gateway? The AWS Access Key Id does not exist in our records

Examples related to authentication

Set cookies for cross origin requests How Spring Security Filter Chain works What are the main differences between JWT and OAuth authentication? http post - how to send Authorization header? ASP.NET Core Web API Authentication Token based authentication in Web API without any user interface Custom Authentication in ASP.Net-Core Basic Authentication Using JavaScript Adding ASP.NET MVC5 Identity Authentication to an existing project LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903A9, comment: AcceptSecurityContext error, data 52e, v1db1

Examples related to amazon-s3

How to specify credentials when connecting to boto3 S3? AWS S3 CLI - Could not connect to the endpoint URL How to write a file or data to an S3 object using boto3 The AWS Access Key Id does not exist in our records AccessDenied for ListObjects for S3 bucket when permissions are s3:* Save Dataframe to csv directly to s3 Python Listing files in a specific "folder" of a AWS S3 bucket How to get response from S3 getObject in Node.js? Getting Access Denied when calling the PutObject operation with bucket-level permission Read file content from S3 bucket with boto3