Skip to content

Instantly share code, notes, and snippets.

@MichMich
Created November 2, 2016 13:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save MichMich/6c527394d107bb6d83e4a8f132a75405 to your computer and use it in GitHub Desktop.
Save MichMich/6c527394d107bb6d83e4a8f132a75405 to your computer and use it in GitHub Desktop.
Example of an Amazon S3 upload.
<!DOCTYPE html>
<html>
<head>
<title>AWS S3 File Upload</title>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js"></script>
</head>
<body>
<input type="file" id="file-chooser" />
<button id="upload-button">Upload to S3</button>
<div id="results"></div>
<script type="text/javascript">
AWS.config.region = 'eu-west-1'; // 1. Enter your region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'eu-west-1:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' // 2. Enter your identity pool
});
AWS.config.credentials.get(function(err) {
if (err) alert("Error: " + err);
console.log(AWS.config.credentials);
listObjs();
});
var bucketName = 'my_bucket'; // Enter your bucket name
var bucket = new AWS.S3({
params: {
Bucket: bucketName
}
});
var fileChooser = document.getElementById('file-chooser');
var button = document.getElementById('upload-button');
var results = document.getElementById('results');
button.addEventListener('click', function() {
var file = fileChooser.files[0];
if (file) {
results.innerHTML = '';
var objKey = 'testing/' + file.name;
var params = {
Key: objKey,
ContentType: file.type,
Body: file,
ACL: 'public-read'
};
bucket.upload(params, function(err, data) {
if (err) {
results.innerHTML = 'ERROR: ' + err;
} else {
console.log(data);
listObjs();
}
}).on('httpUploadProgress', function(evt) {
console.log('Progress:', evt.loaded / evt.total * 100);
});
} else {
results.innerHTML = 'Nothing to upload.';
}
}, false);
function listObjs() {
var prefix = 'testing';
bucket.listObjects({
Prefix: prefix
}, function(err, data) {
if (err) {
results.innerHTML = 'ERROR: ' + err;
} else {
console.log(data);
var objKeys = "";
data.Contents.forEach(function(obj) {
objKeys += obj.Key + "<br>";
});
results.innerHTML = objKeys;
}
});
}
</script>
</body>
</html>
@ashranjan
Copy link

I am seeing error in if i add signatureVersion: 'v4',
var params = {
Key: objKey,
ContentType: file.type,
Body: file,
ACL: 'public-read',
signatureVersion: 'v4',
ServerSideEncryption: 'aws:kms',
SSEKMSKeyId: 'b4b1287b-e4e2-4ca4-a0d6-00a95f05fb8d'
};

ERROR: UnexpectedParameter: Unexpected key 'signatureVersion' found in params
How do i resolve this error

ERROR: InvalidArgument: Requests specifying Server Side Encryption with AWS KMS managed keys require AWS Signature Version 4.

@hit-monchan
Copy link

ERROR: NetworkingError: Network Failure

@yaskshelat
Copy link

ERROR: NetworkingError: Network Failure

have you resolved this error?

@Shaheer-ahmad
Copy link

Enable CORS on bucket to resolve NetworkError

@MichMich
Copy link
Author

Please don't use this anymore. There are better alternatives.

@yogevyuval
Copy link

yogevyuval commented Oct 17, 2020

Please don't use this anymore. There are better alternatives.

Such as? Can you elaborate? It still looks like the best way to upload from a client with multi part support
@MichMich

@MichMich
Copy link
Author

If you use STS to generate temporary tokens you don’t need Cognito. Added benefit is that you can generate a token for a specific path (key) to limit the upload location.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment