1

在我的网络应用程序中,我使用Amazon S3存储桶来保存图像,我需要我的codeigniter主机显示S3存储桶中的图像,但是使用我的主机url。亚马逊S3桶Codeigniter域掩码

例如:

mywebapp.com/products/image1.jpg将显示来自mywebapp.s3.amazonaws.com/products/image1.jpg

我用笨的内容和我不知道我是否会处理我的笨项目中或从其他配置这个问题。

+0

为什么不直接使用S3链接直接使用S3链接什么时候需要它?我们至少需要知道你想达到什么目的。 – xiankai

+0

我知道它不需要,但我只是认为这种URL重定向会显得更专业,而且我希望我的所有网址具有相同的原点以获得更好的外观。 –

+0

然后你想要的不是url重定向,它是域掩码。 http://en.wikipedia.org/wiki/Domain_Masking – xiankai

回答

0

先装在你的构造网址助手,如果你还没有做它已经:

$this->load->helper('url'); 

那么当你需要重定向,你可以简单地调用:

$s3_url = "https://mywebapp.s3.amazonaws.com/products/image1.jpg"; 

// you can omit the last two parameters for the default redirect 
redirect($s3_url, 'location', 301); 

我猜你想这是我的解决方案

<?php if (!defined('BASEPATH')) die(); 
class Img extends CI_Controller { 

    public function __construct() 
    { 
     parent::__construct(); 

     $this->load->helper('url'); 

     // this is the db model where you store the image's urls 
     $this->load->model('images_model', 'img_m'); 
    } 

    // accessed as example.com/img/<image_id> 
    // redirects to the appropiate s3 URL 
    public function index() 
    { 
     // get the second segment (returns false if not set) 
     $image_id = $this->uri->segment(2); 

     // if there was no image in the url set: 
     if ($image_id === false) 
     { 
      // load an image index view 
      $this->load->view('image_index_v'); 
      exit; 
     } 

     $url = $this->img_m->get_url($image_id); 

     // get_url() should return something like this: 
     $url = "https://mywebapp.s3.amazonaws.com/products/image1.jpg"; 

     // then you simply call: 
     redirect($url, 'location', 301); 
    } 
}