Skip to content
This repository has been archived by the owner on Nov 19, 2018. It is now read-only.

Update AWS PHP SDK to v3 #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"homepage": "http://fineuploader.com",
"license": "MIT",
"require": {
"aws/aws-sdk-php": "2.*"
"aws/aws-sdk-php": "3.*"
}
}
41 changes: 28 additions & 13 deletions endpoint-cors.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
*
* Requirements:
* - PHP 5.3 or newer
* - Amazon PHP SDK (only if utilizing the AWS SDK for deleting files or otherwise examining them)
* - Amazon PHP SDK V3 (only if utilizing the AWS SDK for deleting files or otherwise examining them)
*
* If you need to install the AWS SDK, see http://docs.aws.amazon.com/aws-sdk-php-2/guide/latest/installation.html.
* If you need to install the AWS SDK, see https://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/installation.html .
*
* Updated to AWS PHP SDK as per https://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/migration.html
*
* You will need to specify your S3 region in objectProperties, and uploadSuccess params specify region and version
*/

require '../../autoload.php';
Expand Down Expand Up @@ -68,6 +72,10 @@
// and other POST requests (all requests are sent to the same endpoint in this example).
// This condition is not needed if you don't require a callback on upload success.
if (isset($_REQUEST["success"])) {
//If Region is set in uploadSuccess params then override the default us-east-1
$s3Region = (isset($_REQUEST["region"]) ? $_REQUEST["region"] : 'us-east-1');

This comment was marked as spam.

//If (signature) version is set in uploadSuccess params then override the default 2
$s3Signature_version = (isset($_REQUEST["version"]) ? $_REQUEST["version"] : 2);
verifyFileInS3(shouldIncludeThumbnail());
}
else {
Expand Down Expand Up @@ -112,12 +120,17 @@ function handlePreflight() {
}

function getS3Client() {
global $serverPublicKey, $serverPrivateKey;

return S3Client::factory(array(
'key' => $serverPublicKey,
'secret' => $serverPrivateKey
));
global $serverPublicKey, $serverPrivateKey, $s3Region;

return new S3Client([
'region' => $s3Region,
'version' => '2006-03-01',
'signature_version' => $s3Signature_version,

This comment was marked as spam.

'credentials' => [
'key' => $serverPublicKey,
'secret' => $serverPrivateKey
]
]);
}

// Only needed if the delete file feature is enabled
Expand Down Expand Up @@ -293,11 +306,13 @@ function verifyFileInS3($includeThumbnail) {

// Provide a time-bombed public link to the file.
function getTempLink($bucket, $key) {
$client = getS3Client();
$url = "{$bucket}/{$key}";
$request = $client->get($url);

return $client->createPresignedUrl($request, '+15 minutes');
$s3Client = getS3Client();
$cmd = $s3Client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => $key
]);
$request = $s3Client->createPresignedRequest($cmd, '+15 minutes');
return $presignedUrl = (string) $request->getUri();
}

function getObjectSize($bucket, $key) {
Expand Down
93 changes: 57 additions & 36 deletions endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,40 @@
*
* Requirements:
* - PHP 5.3 or newer
* - Amazon PHP SDK (only if utilizing the AWS SDK for deleting files or otherwise examining them)
* - Amazon PHP SDK v3 (only if utilizing the AWS SDK for deleting files or otherwise examining them)
*
* If you need to install the AWS SDK, see http://docs.aws.amazon.com/aws-sdk-php-2/guide/latest/installation.html.
* If you need to install the AWS SDK, see https://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/installation.html .
*
* Updated to AWS PHP SDK as per https://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/migration.html
*
* You will need to specify your S3 region in objectProperties, and uploadSuccess params specify region and version
*/
require '../../autoload.php';
use Aws\S3\S3Client;

// These assume you have the associated AWS keys stored in
// the associated system environment variables
$clientPrivateKey = $_ENV['AWS_CLIENT_SECRET_KEY'];
// These two keys are only needed if the delete file feature is enabled
// or if you are, for example, confirming the file size in a successEndpoint
// handler via S3's SDK, as we are doing in this example.
$serverPublicKey = $_ENV['AWS_SERVER_PUBLIC_KEY'];
$serverPrivateKey = $_ENV['AWS_SERVER_PRIVATE_KEY'];


// The following variables are used when validating the policy document
// sent by the uploader.
$expectedBucketName = $_ENV['S3_BUCKET_NAME'];
$expectedHostName = $_ENV['S3_HOST_NAME']; // v4-only

This comment was marked as spam.


// $expectedMaxSize is the value you set the sizeLimit property of the
// validation option. We assume it is `null` here. If you are performing
// validation, then change this to match the integer value you specified
// otherwise your policy document will be invalid.
// http://docs.fineuploader.com/branch/develop/api/options.html#validation-option
$expectedMaxSize = (isset($_ENV['S3_MAX_FILE_SIZE']) ? $_ENV['S3_MAX_FILE_SIZE'] : null);

require '../../autoload.php';
use Aws\S3\S3Client;

// These assume you have the associated AWS keys stored in
// the associated system environment variables
$clientPrivateKey = $_ENV['AWS_CLIENT_SECRET_KEY'];
// These two keys are only needed if the delete file feature is enabled
// or if you are, for example, confirming the file size in a successEndpoint
// handler via S3's SDK, as we are doing in this example.
$serverPublicKey = $_ENV['AWS_SERVER_PUBLIC_KEY'];
$serverPrivateKey = $_ENV['AWS_SERVER_PRIVATE_KEY'];

// The following variables are used when validating the policy document
// sent by the uploader.
$expectedBucketName = $_ENV['S3_BUCKET_NAME'];
$expectedHostName = $_ENV['S3_HOST_NAME']; // v4-only
// $expectedMaxSize is the value you set the sizeLimit property of the
// validation option. We assume it is `null` here. If you are performing
// validation, then change this to match the integer value you specified
// otherwise your policy document will be invalid.
// http://docs.fineuploader.com/branch/develop/api/options.html#validation-option
$expectedMaxSize = (isset($_ENV['S3_MAX_FILE_SIZE']) ? $_ENV['S3_MAX_FILE_SIZE'] : null);

$method = getRequestMethod();
$method = getRequestMethod();

// This second conditional will only ever evaluate to true if
// the delete file feature is enabled
Expand All @@ -60,6 +65,10 @@
// and other POST requests (all requests are sent to the same endpoint in this example).
// This condition is not needed if you don't require a callback on upload success.
if (isset($_REQUEST["success"])) {
//If Region is set in uploadSuccess params then override the default us-east-1
$s3Region = (isset($_REQUEST["region"]) ? $_REQUEST["region"] : 'us-east-1');
//If (signature) version is set in uploadSuccess params then override the default 2
$s3Signature_version = (isset($_REQUEST["version"]) ? $_REQUEST["version"] : 2);
verifyFileInS3(shouldIncludeThumbnail());
}
else {
Expand All @@ -83,14 +92,20 @@ function getRequestMethod() {
}

function getS3Client() {
global $serverPublicKey, $serverPrivateKey;

return S3Client::factory(array(
'key' => $serverPublicKey,
'secret' => $serverPrivateKey
));
global $serverPublicKey, $serverPrivateKey, $s3Region;

return new S3Client([
'region' => $s3Region,
'version' => '2006-03-01',
'signature_version' => $s3Signature_version,

This comment was marked as spam.

'credentials' => [
'key' => $serverPublicKey,
'secret' => $serverPrivateKey
]
]);
}


// Only needed if the delete file feature is enabled
function deleteObject() {
getS3Client()->deleteObject(array(
Expand Down Expand Up @@ -267,11 +282,17 @@ function verifyFileInS3($includeThumbnail) {

// Provide a time-bombed public link to the file.
function getTempLink($bucket, $key) {
$client = getS3Client();
$url = "{$bucket}/{$key}";
$request = $client->get($url);
$s3Client = getS3Client();

$cmd = $s3Client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => $key
]);

$request = $s3Client->createPresignedRequest($cmd, '+15 minutes');

return $client->createPresignedUrl($request, '+15 minutes');
// Get the actual presigned-url
return $presignedUrl = (string) $request->getUri();
}

function getObjectSize($bucket, $key) {
Expand Down