Reputation: 41
I am currently uploading photos to AWS s3 bucket and that S3 URL is being saved in the database as shown picture below:
Now I just created a Cloudfront Distribution since I believe its will boost the speed of retrieving images. However, not since a Cloudfront URL is automatically generated for any object that is uploaded to my bucket, the S3 URL needs to be replaced with the Cloudfront URL when serving the images, And how to do that?
I want to do either option 1:
Like i think it can be done either by creating another column in db for the generated Cloudfront URL and pulling that, still I don't know how to do it?
or option 2: Replace the s3 URL in the obtained URL with cloudfront URL when are pulling it from database.
So what best think to do to make it work?, I need help to switch to CloudFront but still my data stored in S3 bucket.
<?php
include "common.php";
include "aws.php";
try{
if ($_SERVER["REQUEST_METHOD"]=="POST"){
$conn=get_db_connection();
$post_id=$_REQUEST["post_id"];
//$form=R::findOne('answer','id=?',array($form_id));
//$formimage=R::dispense('formimage');
$url="";
if (isset($_FILES["media_file"]) && $_FILES["media_file"]["size"]>0){
$filename=$_FILES["media_file"]["name"];
$fullfilepath="admin/user_media/".$filename;
move_uploaded_file($_FILES["media_file"]["tmp_name"],$fullfilepath);
$emUrl = "http".(!empty($_SERVER['HTTPS'])?"s":"").
"://".$_SERVER['SERVER_NAME'].($_SERVER['SERVER_PORT']=='80'?"":(":".$_SERVER['SERVER_PORT'])).$_SERVER['REQUEST_URI'];
$codeUrl=dirname($emUrl)."/".$fullfilepath;
$url=$codeUrl;
$client = Aws\S3\S3Client::factory(array(
'version' => 'latest',
'region' => 'us-west-2',
'scheme' => 'http'
));
$pathToFile=$fullfilepath;
$bucket='2sale';
try{
$result = $client->putObject(array(
'ACL' => 'public-read',
'Bucket' => $bucket,
'Key' => time().'_'.$filename,
'SourceFile' => $pathToFile
)
);
$stCode=$result['@metadata']['statusCode'];
if ($stCode!="200"){
$result=array("status"=>400,"msg"=>"Error in uploading to s3: ".$stCode);
echo json_encode($result);
exit();
}
$url=$result['ObjectURL'];
unlink($fullfilepath);
}catch(Exception $e){
$result=array("status"=>400,"msg"=>"Exception in s3: ".$e->getMessage());
echo json_encode($result);
exit();
}
$query="INSERT INTO postimage(image_url,post_id) VALUES('".mysql_real_escape_string($url)."',".$post_id.")";
mysql_query($query,$conn);
$query="UPDATE posts SET image_url='".mysql_real_escape_string($url)."' WHERE id=".$post_id;
mysql_query($query,$conn);
}else{
$result=array("status"=>400,"msg"=>"Your image not uploaded to our script");
echo json_encode($result);
exit();
}
mysql_close($conn);
$result=array("status"=>200,"image_url"=>$url,"media_file" => $_FILES["media_file"]);
echo json_encode($result);
exit();
}
}catch(Exception $ex){
$result=array("status"=>400,"msg"=>"Global exception: ".$ex->getMessage());
echo json_encode($result);
exit();
}
?>
<body>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="media_file" />
<input type="text" value="1" name="post_id" />
<input type="submit" value="Submit" />
</form>
</body>
Upvotes: 0
Views: 1102
Reputation: 41
Never mind just fixed it hardcoded.
In 46 line or in the next line - Added this :
$cloudfront = "https://XXXXX.cloudfront.net/";
$fileNameFull = time().'_'.$filename;
$url = $cloudfront.$fileNameFull;
Upvotes: -1
Reputation: 10567
You shouldn't store the S3 or CloudFront domain name in the
database. You only need to store the object's full key name (e.g.,
/photos/2006/February/sample.jpg
). This way, you can always switch
between S3 buckets or CloudFront distributions by just modifying a
global config variable in your application, which contains the
domain name value.
Better yet, you can always use your own
Alternate Domain Names (CNAMEs) for your CloudFront distribution
with an SSL certificate from AWS Certificate Manager (e.g.,
static.example.com
instead of XXXXXXXXXXXXXX.cloudfront.net
).
This way, you only need to change the A record in your DNS when
changing your S3 bucket or CloudFront distribution.
Upvotes: 5