JavaScript SDK for Sirv S3
On this page
Manage your images on Sirv through the browser using the AWS SDK for JavaScript.
Example
The example HTML page below will help you get started. It will:
- Connect to your Sirv account.
- Show a form to upload files to your account.
- After upload, show a list of files in the root directory.
Usage
To use the script:
- Copy and paste the code into a new HTML file.
- Enter your Sirv S3 credentials into the following 3 places of the script (on lines 11, 12 and 18):
YOUR_SIRV_S3_KEY
YOUR_SIRV_S3_SECRET
YOUR_SIRV_S3_BUCKET - Save the HTML file and test it in your browser.
<html> <head> <script src="https://sdk.amazonaws.com/js/aws-sdk-2.1.43.min.js"></script> </head> <body> <div id="results"></div> <input type="file" id="file-chooser" /> <button id="upload-button">Upload to S3</button> <script> AWS.config.update({ 'accessKeyId': 'YOUR_SIRV_S3_KEY', 'secretAccessKey': 'YOUR_SIRV_S3_SECRET', s3ForcePathStyle: true }); var s3 = new AWS.S3({ endpoint: new AWS.Endpoint('https://s3.sirv.com'), params: { Bucket: 'YOUR_SIRV_S3_BUCKET' } }); 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 = file.name; var params = { Key: objKey, ContentType: file.type, Body: file }; s3.putObject(params, function (err, data) { if (err) { results.innerHTML = 'ERROR: ' + err; } else { listObjs(); } }); } else { results.innerHTML = 'Nothing to upload.'; } }, false); function listObjs() { var prefix = ''; s3.listObjects({ Prefix: prefix }, function (err, data) { if (err) { results.innerHTML = 'ERROR: ' + err; } else { var objKeys = ""; data.Contents.forEach(function (obj) { objKeys += obj.Key + "<br>"; }); results.innerHTML = objKeys; } }); } // listObjs(); </script> </body> </html>