Before all steps, I assume you are met these prerequisite:

  • Using .env file (link)
  • Using Composer
  • Amazon access key id and secret ready
  1. Install Amazon SDK via Composer
composer require aws/aws-sdk-php
  1. Adding these information to your .env environment file
AWS_ACCESS_KEY_ID=*your key*
AWS_SECRET_ACCESS_KEY=*your secret access*
AWS_REGION=*your s3 bucket region*
AWS_BUCKET=*your bucket name*
  1. Upload the file to bucket

$file = $_FILES['filename'];

$provider = \Aws\Credentials\CredentialProvider::env(); // load the credentials info from the env file
$region = getenv('AWS_REGION'); // setup the region
$bucket = getenv('AWS_BUCKET');  // setup the bucket name

// create a new s3 instance
$s3 = new \Aws\S3\S3Client([
    'version' => 'latest',
    'region' => $region,
    'credentials' => $provider
]);
$random_filename = md5(time()); // create 

...

[ Read More ]