|
1 | | -ng-s3upload |
| 1 | +ng-s3upload - Upload to S3 using AngularJ |
2 | 2 | =========== |
3 | 3 |
|
4 | | -Upload to S3 using AngularJS |
| 4 | +An AngularJS directive that allows you to simply upload files directly to AWS S3. |
| 5 | + |
| 6 | +## Setup |
| 7 | +1. Create AWS S3 bucket |
| 8 | + |
| 9 | +2. Add CORS configuration to your bucket |
| 10 | +In AWS web interface, select S3 and select the wanted bucket. |
| 11 | +Expand the "Permissions" section and click on the "Add CORS configuration" button. Paste the wanted CORS configuration, for example: |
| 12 | + ```XML |
| 13 | + <?xml version="1.0" encoding="UTF-8"?> |
| 14 | + <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> |
| 15 | + <CORSRule> |
| 16 | + <AllowedOrigin>*</AllowedOrigin> |
| 17 | + <AllowedMethod>GET</AllowedMethod> |
| 18 | + <MaxAgeSeconds>3000</MaxAgeSeconds> |
| 19 | + <AllowedHeader>Authorization</AllowedHeader> |
| 20 | + </CORSRule> |
| 21 | + <CORSRule> |
| 22 | + <AllowedOrigin>*</AllowedOrigin> |
| 23 | + <AllowedMethod>PUT</AllowedMethod> |
| 24 | + <MaxAgeSeconds>3000</MaxAgeSeconds> |
| 25 | + <AllowedHeader>Content-Type</AllowedHeader> |
| 26 | + <AllowedHeader>x-amz-acl</AllowedHeader> |
| 27 | + <AllowedHeader>origin</AllowedHeader> |
| 28 | + </CORSRule> |
| 29 | + </CORSConfiguration> |
| 30 | + ``` |
| 31 | +Once the CORS permissions are updated, your bucket is ready for client side uploads. |
| 32 | + |
| 33 | +3. Create a server side service that will return the needed details for uploading files to S3. |
| 34 | +your service shall return a json in the following format: |
| 35 | + |
| 36 | + ```json |
| 37 | + { |
| 38 | + "policy": "XXXX", |
| 39 | + "signature": "YYY", |
| 40 | + "key": "ZZZ" |
| 41 | + } |
| 42 | + ``` |
| 43 | +XXX - A policy json that is required by AWS, base64 encoded. |
| 44 | +YYY - HMAC and sha of your private key |
| 45 | +ZZZ - Your public key |
| 46 | +Here's a rails example, even if you're not a rails developer, read the code, it's very straight forward. |
| 47 | + ```ruby |
| 48 | + def s3_access_token |
| 49 | + render json: { |
| 50 | + policy: s3_upload_policy, |
| 51 | + signature: s3_upload_signature, |
| 52 | + key: GLOBAL[:aws_key] |
| 53 | + } |
| 54 | + end |
| 55 | + |
| 56 | + protected |
| 57 | + |
| 58 | + def s3_upload_policy |
| 59 | + @policy ||= create_s3_upload_policy |
| 60 | + end |
| 61 | + |
| 62 | + def create_s3_upload_policy |
| 63 | + Base64.encode64( |
| 64 | + { |
| 65 | + "expiration" => 1.hour.from_now.utc.xmlschema, |
| 66 | + "conditions" => [ |
| 67 | + { "bucket" => GLOBAL[:aws_bucket] }, |
| 68 | + [ "starts-with", "$key", "" ], |
| 69 | + { "acl" => "public-read" }, |
| 70 | + [ "starts-with", "$Content-Type", "" ], |
| 71 | + [ "content-length-range", 0, 10 * 1024 * 1024 ] |
| 72 | + ] |
| 73 | + }.to_json).gsub(/\n/,'') |
| 74 | + end |
| 75 | + |
| 76 | + def s3_upload_signature |
| 77 | + Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), GLOBAL[:aws_secret], s3_upload_policy)).gsub("\n","") |
| 78 | + end |
| 79 | + end |
| 80 | + ``` |
| 81 | +4. Download ng-s3upload.min.js and add it to your project or use bower (bower install ng-s3upload --save). |
| 82 | + |
| 83 | +## Usage |
| 84 | +1. Add ng-s3upload.min.js to your main file (index.html) |
| 85 | + |
| 86 | +2. Set `ngS3upload` as a dependency in your module |
| 87 | + ```javascript |
| 88 | + var myapp = angular.module('myapp', ['ngS3upload']) |
| 89 | + ``` |
| 90 | + |
| 91 | +3. Add s3-upload directive to the wanted element, example: |
| 92 | + ```html |
| 93 | + <div s3-upload bucket="s3Bucket" ng-model="product.remote_product_file_url" |
| 94 | + s3-upload-options="{getOptionsUri: s3OptionsUri}" > |
| 95 | + ``` |
| 96 | + |
| 97 | +attributes: |
| 98 | +* bucket - Speificy the wanted bucket |
| 99 | +* s3-upload-options - Provide additional options: |
| 100 | + * getOptionsUri - The uri of the server service that is needed to sign the request (mentioned in section Setup#3) - Required. |
| 101 | + |
0 commit comments