- install dependencies via composer
- replace Uploadcare API keys in
config.php
- replace AWS keys in
config.php
- set your bucket name in
config.php
- run the example
- upload files and check S3 bucket for copied files :)
Last active
February 13, 2017 04:26
-
-
Save dmitry-mukhin/4b608e1d5444464416ff to your computer and use it in GitHub Desktop.
Saving uploaded files to custom S3 key with PHP
This file contains 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
{ | |
"name": "uploadcare/custom-s3", | |
"description": "Custom S3 storage example", | |
"license": "MIT", | |
"authors": [ | |
{ | |
"name": "Dmitry Mukhin", | |
"email": "[email protected]" | |
} | |
], | |
"repositories": [{ | |
"type": "vcs", | |
"url": "https://github.com/uploadcare/uploadcare-php" | |
}], | |
"minimum-stability": "dev", | |
"require": { | |
"uploadcare/uploadcare-php": "dev-master", | |
"aws/aws-sdk-php": "2.*" | |
} | |
} |
This file contains 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 | |
define('UC_PUBLIC_KEY', 'demopublickey'); | |
define('UC_SECRET_KEY', 'demoprivatekey'); | |
define('AWS_ACCESS_KEY_ID', '** your ID here **'); | |
define('AWS_SECRET_ACCESS_KEY', '** your KEY here **'); | |
$bucket = '** name of your S3 bucket **'; |
This file contains 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 'config.php'; | |
require_once 'vendor/autoload.php'; | |
use Uploadcare\Api; | |
use Aws\S3\S3Client; | |
$ucAPI = new Api(UC_PUBLIC_KEY, UC_SECRET_KEY); | |
function handle_uploads() { | |
global $ucAPI, $bucket; | |
$s3Client = S3Client::factory(array( | |
'key' => AWS_ACCESS_KEY_ID, | |
'secret' => AWS_SECRET_ACCESS_KEY, | |
)); | |
$uuid = $_POST['images']; | |
$group = $ucAPI->getGroup($uuid); | |
$username = $_POST['username']; | |
// iterage over all uploaded files | |
foreach($group->getFiles() as $file) { | |
// s3 object key: e.g.: user1/image_{uuid} | |
$key = $username . '/image_' . $file->getFileId(); | |
// this request creates a copy of uploaded file in the bucket | |
$res = $s3Client->upload( | |
$bucket, | |
$key, | |
file_get_contents($file->getUrl())); | |
// debug output :) | |
// var_dump($res); | |
} | |
// do something more useful here, i.e. redirect to success page | |
echo "files are succesfully copied to S3 bucket: $bucket"; | |
die(); | |
} | |
if(isset($_POST['images'])) { | |
handle_uploads(); | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta encoding='utf-8'> | |
<title>Uploadcare</title> | |
<?php echo $ucAPI->widget->getScriptTag(); ?> | |
<script type="text/javascript"> | |
$ = uploadcare.jQuery; | |
$(function() { | |
$('#uc-form').submit(function() { | |
if (!$('#uc-form input[name=images]').val()) { | |
alert("Upload at least 1 file!"); | |
return false; | |
} | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<form method="post" action="" id="uc-form"> | |
Username: <input type="text" name="username" value="user<?php echo rand(1, 10) ?>" /><br /> | |
Images: <?php echo $ucAPI->widget->getInputTag('images', array('data-multiple' => 'true')); ?><br /> | |
<input type="submit" value="Upload!" /> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your gist.
This is cool, but do you have any idea if it's possible to upload the files directly to S3, without saving the file to the filesystem first?