Joh*_*ric 0 php amazon-s3 amazon-web-services amazon-cloudfront
我目前正在将照片上传到AWS s3存储桶,并且该S3 URL正在保存在数据库中,如下图所示:
现在我刚刚创建了一个Cloudfront Distribution,因为我相信它会提高检索图像的速度.但是,由于为上传到我的存储桶的任何对象自动生成了Cloudfront URL,因此在提供映像时,需要使用Cloudfront URL替换S3 URL,以及如何执行此操作?
我想做选项1:
就像我认为可以通过在db中为生成的Cloudfront URL创建另一个列并拉动它来完成,我仍然不知道该怎么做?
或选项2:在从数据库中提取时,使用cloudfront URL替换获取的URL中的s3 URL.
那么最好的想法是让它工作吗?我需要帮助才能切换到CloudFront,但我的数据仍然存储在S3存储桶中.
<?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>
Run Code Online (Sandbox Code Playgroud)
您不应将S3或CloudFront域名存储在
数据库中.您只需要存储对象的完整密钥名称(例如
/photos/2006/February/sample.jpg).这样,您只需修改
应用程序中包含域名值的全局配置变量,即可在S3存储桶或CloudFront分配之间切换.
更好的是,您始终可以使用您自己的备用域名(CNAME)为您的CloudFront分配使用来自AWS Certificate Manager的SSL证书(例如,
static.example.com而不是XXXXXXXXXXXXXX.cloudfront.net).
这样,您只需在
更改S3存储桶或CloudFront分配时更改DNS中的A记录.
| 归档时间: |
|
| 查看次数: |
163 次 |
| 最近记录: |