Last active
August 29, 2015 14:13
-
-
Save sscovil/318acc3a13d840743a02 to your computer and use it in GitHub Desktop.
An example of how one might use https://github.com/nervgh/angular-file-upload to communicate with a PHP proxy and Amazon S3.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var module = angular.module('myApp', [ | |
'angularFileUpload' | |
]); | |
module.controller('MyCtrl', ['$scope', 'FileUploader', function($scope, FileUploader) { | |
'use strict'; | |
$scope.uploader = new FileUploader({ | |
url : 'http://mydomain.com/s3proxy.php', | |
filters : [ | |
{ | |
name : 'imageFilter', | |
fn : function(item /*{File|FileLikeObject}*/, options) { | |
var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|'; | |
return '|jpg|jpeg|png|'.indexOf(type) !== -1; | |
} | |
} | |
], | |
onWhenAddingFileFailed : function(item /*{File|FileLikeObject}*/, filter, options) {}, | |
onAfterAddingFile : function(fileItem) { | |
fileItem.upload(); | |
}, | |
onAfterAddingAll : function(addedFileItems) {}, | |
onBeforeUploadItem : function(item) {}, | |
onProgressItem : function(fileItem, progress) {}, | |
onProgressAll : function(progress) {}, | |
onSuccessItem : function(fileItem, response, status, headers) { | |
fileItem.s3url = response.data.url; | |
}, | |
onErrorItem : function(fileItem, response, status, headers) {}, | |
onCancelItem : function(fileItem, response, status, headers) {}, | |
onCompleteItem : function(fileItem, response, status, headers) {}, | |
onCompleteAll : function() {} | |
}); | |
}]); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"aws/aws-sdk-php": "~2.7.0" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('vendor/autoload.php'); | |
function handleRequest() { | |
header( 'Expires: ' . date( 'Y-m-d H:i:s' ) ); | |
header( 'Pragma: no-cache' ); | |
header( 'Cache-control: value="no-cache, no-store, must-revalidate' ); | |
header( 'x-frame-options: sameorigin' ); | |
header( 'Content-type: application/json; charset=utf-8' ); | |
$count = count( $_FILES ); | |
if ( $count === 1 ) { | |
$file = $_FILES['file']; | |
$data = uploadFileToS3( $file ); | |
success( $data ); | |
} | |
else { | |
$message = sprintf( 'One file required per request, this request contained %s files.', $count ); | |
badRequest( $message ); | |
} | |
} | |
function uploadFileToS3() { | |
$aws_access_key_id = ''; | |
$aws_secret_access_key = ''; | |
$bucket = 'my-bucket'; | |
$prefix = 'my-app/'; | |
$acl = 'public-read'; | |
$errors = validate( $file ); | |
if ( count( $errors ) > 0 ) badRequest( null, $errors ); | |
$credentials = array( | |
'key' => $aws_access_key_id, | |
'secret' => $aws_secret_access_key | |
); | |
$client = S3Client::factory( $credentials ); | |
$object = array( | |
'Bucket' => $bucket, | |
'Key' => $prefix . $file['name'], | |
'SourceFile' => $file['tmp_name'], | |
'ACL' => $acl | |
); | |
$result = $client->putObject( $object ); | |
return $result->asArray(); | |
} | |
function validate( array $file ) { | |
$max_file_size = 2097152; | |
$accepted_file_types = array( 'jpg', 'jpeg', 'png' ); | |
$file_extension = strtolower( substr( strrchr( ( $file['name'] ), '.' ), 1 ) ); | |
$errors = array(); | |
if ( $file['name'] == '' ) | |
$errors['name'] = 'Missing file name'; | |
if ( $file['error'] ) | |
$errors['upload'] = sprintf( 'Error uploading file: %s', $file['error'] ); | |
if ( $file['size'] > $max_file_size ) | |
$errors['size'] = sprintf( 'File size exceeds maximum of %s bytes', $max_file_size ); | |
if ( ! in_array( $file_extension, $accepted_file_types ) ) | |
$errors['type'] = sprintf( 'Only the following file types are accepted: %s', $accepted_file_types ); | |
return $errors; | |
} | |
function badRequest( $message = null, array $errors = null ) { | |
header( 'HTTP/1.1 400 Bad Request' ); | |
$response = array('success' => false); | |
if ( isset( $message ) ) | |
$response['message'] = $message; | |
if ( isset( $errors ) ) | |
$response['errors'] = $errors; | |
exit( json_encode( $response ) ); | |
} | |
function success( array $data ) { | |
$response = array('success' => true); | |
if ( isset( $data ) ) { | |
header( 'HTTP/1.1 200 OK' ); | |
$response['data'] = $data; | |
} | |
else { | |
header( 'HTTP/1.1 204 No Content' ); | |
} | |
exit(json_encode($response)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment