Comments (6)
Hi Raul,
I just got this implemented just yesterday where the most difficult part for me was getting the policy correct.
Here's what I ended up with, a PHP class that has one public method called access_token()
. Then I have a server side script that simply uses this class and returns the data to the s3-upload
directive.
Here I'll start with my use of the directive:
<div s3-upload
bucket="activity_images"
ng-model="artist.image_url"
s3-upload-options="{getOptionsUri: 'php_ajax/s3_access.php?bucket=activity_images'}">
</div>
Next is the relevant snippet from s3_access.php
:
$bucket_name = isset($_REQUEST['bucket']) ? $_REQUEST['bucket'] : '';
$s3_provider = new S3Provider($bucket_name);
echo $s3_provider->access_token();
exit;
And finally my S3Provider
class (where you'll see how I created the policy):
class S3Provider {
private $bucket_name;
private $aws_account_no;
private $aws_access_key_id;
private $aws_secret_key;
public function __construct($bucket_name) {
$this->bucket_name = $bucket_name;
$this->aws_access_key_id = 'xxxxxxxxxxxxxxx';
$this->aws_secret_key = 'xxxxxxxxxxxxxx';
}
public function access_token() {
$now = time() + (12 * 60 * 60 * 1000);
$expire = gmdate('Y-m-d\TH:i:s\Z', $now);
$url = 'https://' . $this->bucket_name . '.s3.amazonaws.com';
$policy_document = '
{"expiration": "' . $expire . '",
"conditions": [
{"bucket": "' . $this->bucket_name . '"},
["starts-with", "$key", ""],
{"acl": "public-read"},
["content-length-range", 0, 10485760],
["starts-with", "$Content-Type", ""]
]
}';
$policy = base64_encode($policy_document);
$hash = $this->hmacsha1($this->aws_secret_key, $policy);
$signature = $this->hex2b64($hash);
$token = array('policy' => $policy,
'signature' => $signature,
'key' => $this->aws_access_key_id);
return json_encode($token);
}
private function hmacsha1($key, $data) {
$blocksize = 64;
$hashfunc = 'sha1';
if(strlen($key) > $blocksize)
$key = pack('H*', $hashfunc($key));
$key = str_pad($key, $blocksize, chr(0x00));
$ipad = str_repeat(chr(0x36), $blocksize);
$opad = str_repeat(chr(0x5c), $blocksize);
$hmac = pack('H*', $hashfunc(($key ^ $opad).pack('H*', $hashfunc(($key ^ $ipad).$data))));
return bin2hex($hmac);
}
private function hex2b64($str) {
$raw = '';
for($i=0; $i < strlen($str); $i+=2) {
$raw .= chr(hexdec(substr($str, $i, 2)));
}
return base64_encode($raw);
}
}
Notice how I set $expire
to be in the future (probably doesn't need to be that far into the future). This works like charm for us. Hope this helps.
from ng-s3upload.
@captainmarkos that did it sweet!
I wonder why aws sdk php is broken on this regard, or maybe I'm trying to use PostObject where it was not intended to use.
https://github.com/aws/aws-sdk-php/blob/4ded8a201bd6fcac37d630197e6443f41aa2752d/src/Aws/S3/Model/PostObject.php
<?php
class PolicyProvider
{
public function signPolicy()
{
$aws = Aws::factory([
'key' => $this->key,
'secret' => $this->secret,
'region' => 'xxxxx'
]);
$s3 = $aws->get('S3');
$postObject = new PostObject($s3, $this->bucket);
$postObject->prepareData();
$inputs = $postObject->getFormInputs();
return json_encode([
'policy' => $inputs['policy'],
'signature' => $inputs['signature'],
'key' => $this->key
]);
}
}
Anyway this is working now, thanks
from ng-s3upload.
Good deal, glad it helped.
from ng-s3upload.
Hi! I'm glad it worked for you!
@captainmarkos first of all thank you! would you mind adding your php example to the README or a wiki page so others will be able to depend on it ?
from ng-s3upload.
Wiki created. https://github.com/asafdav/ng-s3upload/wiki/PHP-Creating-the-S3-Policy
from ng-s3upload.
Thank you very much!
from ng-s3upload.
Related Issues (20)
- 403 forbidden HOT 1
- add main to package.json HOT 2
- bucket evaluation not working HOT 3
- Upload does not begin in Cordova
- targetFilename parameter evaluation HOT 3
- Button text should be customizable
- Direct upload to Amazon S3: corrupted files
- build/ng-s3upload.js does not correspond to version HOT 3
- Uncaught TypeError: h.match is not a function HOT 1
- directive is too awesome HOT 1
- Support Multipart upload HOT 3
- 404 error with npm install HOT 1
- Cache-Control Header
- Original filename
- What is the button Replace?
- getting 404 on boostrap templates for buttons HOT 1
- Variable Random String Length
- Allow placeholders in targetFilename
- Why we need bucket name defined in both, frontend and backend ?
- Not working. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ng-s3upload.