Last active
April 9, 2024 16:30
-
-
Save krisnaw/6cb6faac7f746c10a988b7aed9a0bed1 to your computer and use it in GitHub Desktop.
How to use Amazon S3 Presigned POSTs on Laravel + Vue JS
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 | |
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]); | |
} | |
} |
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
template |
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
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