Skip to content

Commit e1d0448

Browse files
committed
Merge pull request #48 from Serhioromano/add-var-policy
The way of providing policy object as scope variable.
2 parents db83254 + b1c4d12 commit e1d0448

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ ng-s3upload - Upload to S3 using AngularJS
33

44
An AngularJS directive that allows you to simply upload files directly to AWS S3.
55

6-
## Setup
6+
## Setup
77
1. Create AWS S3 bucket
88

9-
2. Grant "put/delete" permissions to everyone
10-
In AWS web interface, select S3 and select the destination bucket, then
9+
2. Grant "put/delete" permissions to everyone
10+
In AWS web interface, select S3 and select the destination bucket, then
1111
expand the "Permissions" sections and click on the "Add more permissions" button. Select "Everyone" and "Upload/Delete" and save.
1212

1313
3. Add CORS configuration to your bucket
1414

15-
In AWS web interface, select S3 and select the wanted bucket.
16-
Expand the "Permissions" section and click on the "Add CORS configuration" button. Paste the wanted CORS configuration, for example:
15+
In AWS web interface, select S3 and select the wanted bucket.
16+
Expand the "Permissions" section and click on the "Add CORS configuration" button. Paste the wanted CORS configuration, for example:
1717
```XML
1818
<?xml version="1.0" encoding="UTF-8"?>
1919
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
@@ -41,7 +41,7 @@ expand the "Permissions" sections and click on the "Add more permissions" button
4141
Once the CORS permissions are updated, your bucket is ready for client side uploads.
4242

4343
4. Create a server side service that will return the needed details for uploading files to S3.
44-
your service shall return a json in the following format:
44+
your service shall return a json in the following format:
4545

4646
```json
4747
{
@@ -53,7 +53,7 @@ your service shall return a json in the following format:
5353
XXX - A policy json that is required by AWS, base64 encoded.
5454
YYY - HMAC and sha of your private key
5555
ZZZ - Your public key
56-
Here's a rails example, even if you're not a rails developer, read the code, it's very straight forward.
56+
Here's a rails example, even if you're not a rails developer, read the code, it's very straight forward.
5757

5858
For a php example, please refer to [this guide](https://github.com/asafdav/ng-s3upload/wiki/PHP-Creating-the-S3-Policy).
5959
```ruby
@@ -75,7 +75,7 @@ Here's a rails example, even if you're not a rails developer, read the code, it'
7575
Base64.encode64(
7676
{
7777
"expiration" => 1.hour.from_now.utc.xmlschema,
78-
"conditions" => [
78+
"conditions" => [
7979
{ "bucket" => GLOBAL[:aws_bucket] },
8080
[ "starts-with", "$key", "" ],
8181
{ "acl" => "public-read" },
@@ -89,16 +89,16 @@ Here's a rails example, even if you're not a rails developer, read the code, it'
8989
Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), GLOBAL[:aws_secret], s3_upload_policy)).gsub("\n","")
9090
end
9191
```
92-
92+
9393
The following code generates an upload policy that will be used by S3, in this example the maximum file size is limited to 10MB (10 * 1024 * 1024), update it to match your requirments. for a full list of S3's policy options, please refer to [AWS documentation](http://docs.aws.amazon.com/AmazonS3/latest/dev/HTTPPOSTExamples.html#HTTPPOSTExamplesTextAreaPolicy).
94-
9594

96-
## How to get it ?
95+
96+
## How to get it ?
9797

9898
#### Manual Download
9999
Download the from [here](https://github.com/asafdav/ng-s3upload/releases)
100100

101-
#### Bower
101+
#### Bower
102102
```
103103
bower install ng-s3upload
104104
```
@@ -125,14 +125,15 @@ npm install ng-s3upload
125125
s3-upload-options="{getOptionsUri: s3OptionsUri, folder: 'images/'}">
126126
```
127127

128-
attributes:
128+
attributes:
129129
* bucket - Specify the wanted bucket
130130
* s3-upload-options - Provide additional options:
131-
* getOptionsUri - The uri of the server service that is needed to sign the request (mentioned in section Setup#4) - Required.
131+
* getOptionsUri - The uri of the server service that is needed to sign the request (mentioned in section Setup#4) - Required if second option is empty.
132+
* getManualOptions - if for some reason you need to have your own mechanism of getting a policy, you can simply assign your scope variable to this option. Note it should be resolved on the moment of directive load.
132133
* folder - optional, specifies a folder inside the bucket the save the file to
133134
* enableValidation - optional, set to "false" in order to disable the field validation.
134135
* targetFilename - An optional attribute for the target filename. if provided the file will be renamed to the provided value instead of having the file original filename.
135-
136+
136137
## Themes
137138
ng-s3upload allows to customize the directive template using themes. Currently the available themes are: bootstrap2, bootstrap3
138139

src/ng-s3upload/directives/s3-upload.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ angular.module('ngS3upload.directives', []).
3030
opts = angular.extend({
3131
submitOnChange: true,
3232
getOptionsUri: '/getS3Options',
33+
getManualOptions: null,
3334
acl: 'public-read',
3435
uploadingKey: 'uploading',
3536
folder: '',
@@ -55,7 +56,17 @@ angular.module('ngS3upload.directives', []).
5556
var filename = selectedFile.name;
5657
var ext = filename.split('.').pop();
5758

58-
S3Uploader.getUploadOptions(opts.getOptionsUri).then(function (s3Options) {
59+
if(angular.isObject(opts.getManualOptions)) {
60+
_upload(opts.getManualOptions);
61+
} else {
62+
S3Uploader.getUploadOptions(opts.getOptionsUri).then(function (s3Options) {
63+
_upload(s3Options);
64+
}, function (error) {
65+
throw Error("Can't receive the needed options for S3 " + error);
66+
});
67+
}
68+
69+
function _upload(s3Options){
5970
if (opts.enableValidation) {
6071
ngModel.$setValidity('uploading', false);
6172
}
@@ -87,11 +98,7 @@ angular.module('ngS3upload.directives', []).
8798
ngModel.$setValidity('succeeded', false);
8899
}
89100
});
90-
91-
}, function (error) {
92-
throw Error("Can't receive the needed options for S3 " + error);
93-
});
94-
101+
}
95102
};
96103

97104
element.bind('change', function (nVal) {

0 commit comments

Comments
 (0)