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); 
    } 
}