Reasons to upload files to AWS S3
Unlimited Storage
Reliability & Scalability
Laravel Integration
Versioning
High-grade Security
Cost-efficiency
We can use following way to implement AWS S3 upload in Laravel:
- Install following package-
- Create new library for AWS connection.
\app\LibrariesAwsUtility.php
- ENV variables-
#Client info
AWS_ACCESS_KEY_ID=AKIAQQP246GMXXHSNV5I
AWS_SECRET_ACCESS_KEY=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
AWS_DEFAULT_REGION=us-west-2
AWS_BUCKET=dynafios-development-app-storage
END_POINT=https://dynafios-development-app-storage.S3.us-west-2.amazonaws.com
AWS_USE_PATH_STYLE_ENDPOINT=false
- To upload report –
//For AWS File Upload-need to include
use App\Libraries\AwsUtility;
//Start S3 bucket url change aws – nvn
$storage_path_file_path=$report_path.’/’.$report_filename;
$awsObj_result = AwsUtility::awsUploadSubmit($storage_path_file_path);
$effectiveUri=$awsObj_result->getData()->effectiveUri;
//End S3 bucket url change aws – nvn
//To save URL path in DB-
$hospital_report->awsfilename = $effectiveUri;
storage\temp
- For download, we use function from LibrariesAwsUtility.php and download on the fly temporary url that does not exist after download.
//S3 file download from bucket–nvn
$awsObj_result_down = awsUtility::awsDownloadTempUrl($filename);
$effectiveUri_arr = $awsObj_result_down->getData()->effectiveUri;
$onlyfilename = $awsObj_result_down->getData()->onlyfilename;
if ($effectiveUri_arr == “Failed”) {
//In case URL not saved or link break
$S3fileDownlaod = “Failed”;
return Redirect::back()->with([
//”error” => Lang::get(“hospital.S3_filenot_found_uploaded”),
“error” => “File Not Found!!”,
]);
//REDIRECT WITH MESSAGE
}
$filename = $onlyfilename;
$tempfile = tempnam(sys_get_temp_dir(), $filename);
//COPY SIGNED URL TO TEMP FOLDER
copy($effectiveUri_arr, $tempfile);
return response()->download($tempfile, $filename);
- For delete we use function from LibrariesAwsUtility.php which delete file from aws but not from db. As per requirment.
// aws S3 file delete
$awsObj_result_del = awsUtility::awsDeleteFile($filename);
if (isset($awsObj_result_del)) {
// Log::Info(‘Deleted S3 File!HospitalController-getDeleteReport() ‘);
return Redirect::back()->with([
“success” => Lang::get(“hospitals.delete_report_success”),
]);
} else {
return Redirect::back()->with([
“error” => Lang::get(“hospital.delete_report_error”),
]);
}
return Redirect::back()->with([
“success” => Lang::get(“hospitals.delete_report_success”),
]);
File can be uploaded to AWS S3 in Core PHP as below:
- Install AWS S3 PHP SDK
- Create File Upload Form
- upload-to-S3.php file
require ‘vendor/autoload.php’;
use Aws\S3\S3Client;
// Instantiate an Amazon S3 client.
$S3Client = new S3Client([
‘version’ => ‘latest’,
‘region’ => ‘YOUR_AWS_REGION’,
‘credentials’ => [
‘key’ => ‘ACCESS_KEY_ID’,
‘secret’ => ‘SECRET_ACCESS_KEY’
]
]);
// Check whether file exists before uploading it
if(move_uploaded_file($_FILES[“anyfile”][“tmp_name”], “upload/” . $filename)){
$bucket = ‘YOUR_BUCKET_NAME’;
$file_Path = __DIR__ . ‘/upload/’. $filename;
$key = basename($file_Path);
try {
$result = $S3Client->putObject([
‘Bucket’ => $bucket,
‘Key’ => $key,
‘Body’ => fopen($file_Path, ‘r’),
‘ACL’ => ‘public-read’, // make file ‘public’
]);
echo “Image uploaded successfully. Image path is: “. $result->get(‘ObjectURL’);
} catch (Aws\S3\Exception\S3Exception $e) {
echo “There was an error uploading file.\n”;
echo $e->getMessage();
}
echo “Your file was uploaded successfully.”;
}else{
echo “File is not uploaded”;
}
Are You Interested in Building a Top-Class Website or Mobile App?