Implementation in PHP for uploading a file to AWS S3


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.
- ENV variables-
- To upload report –
- For download, we use function from LibrariesAwsUtility.php and download on the fly temporary url that does not exist after download.
- For delete we use function from LibrariesAwsUtility.php which delete file from aws but not from db. As per requirment.
- Install AWS S3 PHP SDK
- Create File Upload Form
- upload-to-S3.php file
\app\LibrariesAwsUtility.php
#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
//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
//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);
// 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:
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”;
}
In Conclusion
The Complete Guide to Charts and How They Help Marketers and Businesses to Get Growth


What do you need to know about Google charts?
Why is it important to use Google charts?
- It is free to use and comes with a user-friendly interface.
- It is an easy way to make a data visualization for your blog post or website.
- You can use it to display data in a variety of formats, from bar graphs to pie charts.
- Data visualization is the process of converting information into a graphical format that can be quickly comprehended and shared with others.
Features of Google-based charts?
- Content Management
- Custom Dashboards
- Compatibility with different browsers and mobile platforms.
- Multitouch Support
- Lightweight
- Simple Configurations
- Visual Discovery
- Multiple axes
- Configurable tooltips
- Date Time support.
- Dataset Management
- Print using a web page
- External data from a server
What are some benefits of using Google-created charts?
- Easily create charts with built-in templates.
- A large library of chart types to choose from.
- Flexible chart options to change colors, fonts, etc.
- Simple tools to animate data in a chart or make changes to it over time.
TYPES OF GOOGLE CHART

How Easy They Are!
Some of the Visual EXAMPLES of the charts generated through google’s charts:








How to integrate Google chart Pie
<!DOCTYPE html>
<html>
<script type=”text/javascript” src=”https://www.gstatic.com/charts/loader.js”></script>
<body>
<div>
id=”myChart” style=”width:100%; max-width:600px; height:500px;”>
</div>
<script>
google.charts.load(‘current’, {‘packages’:[‘corechart’]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
[‘Contry’, ‘Mhl’],
[‘Italy’,54.8],
[‘France’,48.6],
[‘Spain’,44.4],
[‘USA’,23.9],
[‘Argentina’,14.5]
]);
var options = {
title:’World Wide Wine Production’
};
var chart = new google.visualization.PieChart(document.getElementById(‘myChart’));
chart.draw(data, options);
}
</script>
</body>
</html>
