I have an web app that is hosted on ec2 ubuntu 22.04, and using PHP I want to upload a file to s3, I created an AMI role with s3fullAccess and is attached to the ec2.
This is the upload file
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
$bucket="bucket-name";
$statusMsg = '';
$status="danger";
// If file upload form is submitted
if(isset($_POST["submit"])){
// Check whether user inputs are empty
if(!empty($_FILES["userfile"]["name"])) {
// File info
$file_name = basename($_FILES["userfile"]["name"]);
$file_type = pathinfo($file_name, PATHINFO_EXTENSION);
// Allow certain file formats
$allowTypes = array('xlsx');
if(in_array($file_type, $allowTypes)){
// File temp source
$file_temp_src = $_FILES["userfile"]["tmp_name"];
if(is_uploaded_file($file_temp_src)){
// Instantiate an Amazon S3 client
$s3Client = new S3Client([
'region' => 'eu-central-1',
'version' => '2006-03-01'
]);
// Upload file to S3 bucket
try {
$result = $s3->putObject([
'Bucket' => $bucket,
'Key' => $file_name,
'SourceFile' => $file_temp_src
]);
$result_arr = $result->toArray();
if(!empty($result_arr['ObjectURL'])) {
$s3_file_link = $result_arr['ObjectURL'];
} else {
$api_error="Upload Failed! S3 Object URL not found.";
}
} catch (Aws\S3\Exception\S3Exception $e) {
$api_error = $e->getMessage();
}
if(empty($api_error)){
$status="success";
$statusMsg = "File was uploaded to the S3 bucket successfully!";
}else{
$statusMsg = $api_error;
}
}else{
$statusMsg = "File upload failed!";
}
}else{
$statusMsg = 'Sorry, only Word/Excel/Image files are allowed to upload.';
}
}else{
$statusMsg = 'Please select a file to upload.';
}
}
?>
This is the html file
<form method="post" action="" enctype="multipart/form-data">
<div class="form-group">
<label><b>Select File:</b></label>
<input type="file" name="userfile" class="form-control" required>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" name="submit" value="Upload">
</div>
</form>
when i try to upload a file is just loading until it get connection time out. How can i solve this, I can only use the role attached to the ec2 as credentials because i cant create users.
Is your instance in a private subnet?