Skip to content

Instantly share code, notes, and snippets.

@bcachet
Last active March 2, 2018 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcachet/386ac9261086b157aa5ae40fdd05b827 to your computer and use it in GitHub Desktop.
Save bcachet/386ac9261086b157aa5ae40fdd05b827 to your computer and use it in GitHub Desktop.
Upload file to sos-ch-dk-2.exo.io with aws PHP SDK and curl

Usage

It is important that file field is in last position (fields after file are omitted)

curl `./post_upload.php` -F file=@some-file-to-upload.txt
{
"require": {
"aws/aws-sdk-php": "^3.52"
}
}
#!/usr/bin/php
<?php
require_once "./vendor/autoload.php";
$bucket = 'post-tests';
$endpoint = 'sos-ch-dk-2.exo.io';
$s3 = new Aws\S3\S3Client([
'endpoint' => sprintf("https://%s", $endpoint),
'version' => 'latest',
'region' => 'ch-dk-2',
'credentials' => [
'key' => 'YOUR_KEY',
'secret' => 'YOUR_SECRET'
]
]);
// Set some defaults for form input fields
$formInputs = [
'acl' => 'public-read-write',
];
// Construct an array of conditions for policy
$options = [
['starts-with', '$key' ,''],
['acl' => 'public-read-write'],
['bucket' => $bucket],
["starts-with", '$Content-Type', ''],
];
// Optional: configure expiration time string
$expires = '+2 hours';
$postObject = new \Aws\S3\PostObjectV4(
$s3,
$bucket,
$formInputs,
$options,
$expires
);
// Get attributes to set on an HTML form, e.g., action, method, enctype
$formAttributes = $postObject->getFormAttributes();
// Get form input fields. This will include anything set as a form input in
// the constructor, the provided JSON policy, your AWS Access Key ID, and an
// auth signature.
$formInputs = $postObject->getFormInputs();
$curlParams = array();
foreach($formInputs as $key => $value) {
array_push($curlParams, sprintf('--form %s=%s', $key, $value));
}
/* array_push($curlParams, '--form "file=@images.png"'); */
array_push($curlParams, sprintf("https://%s/%s", $endpoint, $bucket));
echo implode(' ', $curlParams);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment