我目前正在将照片上传到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 …Run Code Online (Sandbox Code Playgroud)