Amazon S3 Bucket的Codeigniter域掩码

时间:2013-10-15 12:52:05

标签: php codeigniter amazon-web-services amazon-s3 domain-masking

在我的网络应用程序中,我正在使用Amazon S3存储桶来保存图像,我需要我的codeigniter主机来显示来自S3存储桶的图像,但是使用我的主机网址。

例如:

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

的内容

我正在使用Codeigniter,我不确定我是否会在我的codeigniter项目或其他配置中处理此问题。

1 个答案:

答案 0 :(得分:0)

首先在构造函数中加载url helper,如果你还没有这样做的话:

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