简体   繁体   English

Amazon S3存储桶的Codeigniter域掩码

[英]Codeigniter Domain Masking for Amazon S3 Bucket

In my web app, i'm using Amazon S3 bucket to hold images and i need my codeigniter host to show images from S3 bucket but with my host url. 在我的Web应用程序中,我正在使用Amazon S3存储桶保存图像,并且我需要我的codeigniter主机来显示S3存储桶中的图像,但要显示主机URL。

For example: 例如:

mywebapp.com/products/image1.jpg will display content from mywebapp.s3.amazonaws.com/products/image1.jpg mywebapp.com/products/image1.jpg将显示mywebapp.s3.amazonaws.com/products/image1.jpg内容

I'm using Codeigniter and i'm not sure if i will handle this problem inside my codeigniter project or from other configurations. 我正在使用Codeigniter,但不确定在我的Codeigniter项目中还是从其他配置中解决此问题。

First load the url helper in your constructor if you haven't done it already: 如果尚未完成,请首先在构造函数中加载url helper:

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

then whenever you need the redirect you can simply call: 然后,每当您需要重定向时,您都可以简单地调用:

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

I guess you want a service where you access the url and get the image, here's my solution 我想您想要访问URL并获取图像的服务,这是我的解决方案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM