Skip to content

Instantly share code, notes, and snippets.

@philfreo
Created October 6, 2012 01:27
Show Gist options
  • Select an option

  • Save philfreo/3843375 to your computer and use it in GitHub Desktop.

Select an option

Save philfreo/3843375 to your computer and use it in GitHub Desktop.

Revisions

  1. philfreo revised this gist Dec 18, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion view.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    import os, json, base64, urllib, hmac, sha
    import time, os, json, base64, urllib, hmac, sha

    @app.route('/sign_s3_put/')
    @login_required
  2. philfreo revised this gist Dec 10, 2012. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions iam_policy.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    {
    "Statement": [
    {
    "Statement": [{
    "Action": [
    "s3:GetObject",
    "s3:PutObject",
  3. philfreo revised this gist Dec 10, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion view.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    import json, base64, urllib, hmac, sha
    import os, json, base64, urllib, hmac, sha

    @app.route('/sign_s3_put/')
    @login_required
  4. philfreo revised this gist Dec 9, 2012. 1 changed file with 38 additions and 0 deletions.
    38 changes: 38 additions & 0 deletions view.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    import json, base64, urllib, hmac, sha

    @app.route('/sign_s3_put/')
    @login_required
    def sign_s3_put():
    """
    Provide a temporary signature so that users can upload files directly from their
    browsers to our AWS S3 bucket.
    The authorization portion is taken from Example 3 on
    http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html
    """
    # don't give user full control over filename - avoid ability to overwrite files
    random = base64.urlsafe_b64encode(os.urandom(2))
    object_name = random+request.args.get('s3_object_name')
    object_name = urllib.quote_plus(object_name) # make sure it works for filenames with spaces, etc.
    mime_type = request.args.get('s3_object_type')

    expires = int(time.time()+300) # PUT request to S3 must start within X seconds
    amz_headers = "x-amz-acl:public-read" # set the public read permission on the uploaded file
    resource = '%s/%s' % (app.config['AWS_EMAIL_ATTACHMENTS_BUCKET_NAME'], object_name)
    str_to_sign = "PUT\n\n{mime_type}\n{expires}\n{amz_headers}\n/{resource}".format(
    mime_type=mime_type,
    expires=expires,
    amz_headers=amz_headers,
    resource=resource
    )
    sig = urllib.quote_plus(base64.encodestring(hmac.new(app.config['AWS_EMAIL_ATTACHMENTS_SECRET_ACCESS_KEY'], str_to_sign, sha).digest()).strip())

    url = 'https://%s.s3.amazonaws.com/%s' % (app.config['AWS_EMAIL_ATTACHMENTS_BUCKET_NAME'], object_name)
    return json.dumps({
    'signed_request': '{url}?AWSAccessKeyId={access_key}&Expires={expires}&Signature={sig}'.format(
    url=url,
    access_key=app.config['AWS_EMAIL_ATTACHMENTS_ACCESS_KEY_ID'],
    expires=expires,
    sig=sig
    ),
    'url': url
    })
  5. philfreo revised this gist Dec 8, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion client.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    // https://github.com/elasticsales/s3upload-coffee-javascript
    var s3upload = new S3Upload({
    file_dom_selector: '#files',
    file_dom_selector: '#files', // an <input type="file"> element
    s3_sign_put_url: '/sign_s3_put',
    onProgress: function(percent, message, publicUrl, file) { // Use this for live upload progress bars
    console.log('Upload progress: ', percent, message);
  6. philfreo revised this gist Dec 8, 2012. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions client.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    // https://github.com/elasticsales/s3upload-coffee-javascript
    var s3upload = new S3Upload({
    file_dom_selector: '#files',
    s3_sign_put_url: '/sign_s3_put',
    onProgress: function(percent, message, publicUrl, file) { // Use this for live upload progress bars
    console.log('Upload progress: ', percent, message);
    },
    onFinishS3Put: function(public_url, file) { // Get the URL of the uploaded file
    console.log('Upload finished: ', public_url);
    },
    onError: function(status, file) {
    console.log('Upload error: ', status);
    }
    });
  7. philfreo revised this gist Dec 8, 2012. 2 changed files with 15 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion policy.js → bucket_policy.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    "AWS": "*"
    },
    "Action": "s3:GetObject",
    "Resource": "arn:aws:s3:::your-bucket-name/*"
    "Resource": "arn:aws:s3:::bucket_name_here/*"
    }
    ]
    }
    14 changes: 14 additions & 0 deletions iam_policy.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    {
    "Statement": [
    {
    "Action": [
    "s3:GetObject",
    "s3:PutObject",
    "s3:PutObjectAcl"
    ],
    "Effect": "Allow",
    "Resource": [
    "arn:aws:s3:::bucket_name_here/*"
    ]
    }]
    }
  8. philfreo revised this gist Dec 5, 2012. 2 changed files with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions cors.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    <?xml version="1.0" encoding="UTF-8"?>
    <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>Authorization</AllowedHeader>
    </CORSRule>
    <CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>PUT</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>Content-Type</AllowedHeader>
    <AllowedHeader>x-amz-acl</AllowedHeader>
    <AllowedHeader>origin</AllowedHeader>
    </CORSRule>
    </CORSConfiguration>
    File renamed without changes.
  9. philfreo created this gist Oct 6, 2012.
    13 changes: 13 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    {
    "Statement": [
    {
    "Sid": "AllowPublicRead",
    "Effect": "Allow",
    "Principal": {
    "AWS": "*"
    },
    "Action": "s3:GetObject",
    "Resource": "arn:aws:s3:::your-bucket-name/*"
    }
    ]
    }