# JavaScript SDK for Sirv S3

Manage your images on Sirv through the browser using the [AWS SDK for JavaScript](https://aws.amazon.com/sdk-for-browser/).

### Example

The example HTML page below will help you get started. It will:

1.  Connect to your Sirv account.
2.  Show a form to upload files to your account.
3.  After upload, show a list of files in the root directory.

### Usage

To use the script:

1.  Copy and paste the code into a new HTML file.
2.  Enter [your Sirv S3 credentials](https://app.sirv.com/settings/uploads) 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
3.  Save the HTML file and test it in your browser.

```java
    <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.
