JavaScript SDK for Sirv S3
Quick setup: JavaScript with Sirv S31. Install the AWS SDK for JavaScript.2. Copy your S3 Bucket, S3 Key and S3 Secret from Account › Settings › API.3. Point the SDK at the Sirv endpoint
https://s3.sirv.com No account yet? Create one freeShort answer
Use the AWS SDK for JavaScript in the browser with the endpoint s3.sirv.com and your Sirv S3 credentials. The example HTML page on this page connects to your Sirv account, shows a form to upload files, then lists the files in your root folder after each upload.
After reading this you will know
- How to use the AWS SDK for JavaScript with Sirv
- How to upload files to Sirv from a web page
- How to list the files in a Sirv folder
Manage your images on Sirv through the browser using the AWS SDK for JavaScript.
Example
Section titled “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.
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.
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.1.43.min.js"></script>
<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>Remove the comment before listObjs() to list the files in your bucket when the page loads.
