Skip to content

Instantly share code, notes, and snippets.

@krisnaw
Last active April 9, 2024 16:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save krisnaw/6cb6faac7f746c10a988b7aed9a0bed1 to your computer and use it in GitHub Desktop.
Save krisnaw/6cb6faac7f746c10a988b7aed9a0bed1 to your computer and use it in GitHub Desktop.
How to use Amazon S3 Presigned POSTs on Laravel + Vue JS
<?php
namespace App\Http\Controllers;
use Aws\S3\PostObjectV4;
use Aws\S3\S3Client;
use Illuminate\Http\Request;
class ImageUploadController extends Controller
{
public function createPreSignedPost()
{
$client = new S3Client([
'version' => 'latest',
'region' => 'ap-southeast-1',
'credentials' => ['key' => env('AWS_KEY'), 'secret' => env('AWS_SECRET'),],
]);
$bucket = env('AWS_BUCKET');
$formInputs = ['acl' => 'public-read'];
$options = [['acl' => 'public-read'], ['bucket' => $bucket], ['starts-with', '$key', ''],];
$expires = '+1 hours';
$postObject = new PostObjectV4($client, $bucket, $formInputs, $options, $expires);
$formAttributes = $postObject->getFormAttributes();
$formInputs = $postObject->getFormInputs();
return response()->json(['code' => 200, 'formAttributes' => $formAttributes, 'formInputs' => $formInputs]);
}
}
Route::get('create/pre-signed-url', 'ImageUploadController@createPreSignedPost');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment