【问题标题】:Codeigniter Domain Masking for Amazon S3 BucketAmazon S3 存储桶的 Codeigniter 域掩码
【发布时间】:2013-10-23 06:53:36
【问题描述】:

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

例如:

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

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

【问题讨论】:

  • 为什么不在需要时直接使用 S3 链接呢?我们至少需要知道您到底想要达到什么目标。
  • 我知道它不需要,但我只是认为这种 url 重定向看起来更专业,而且我希望我的所有 url 具有相同的来源以获得更好的外观。
  • 那么你想要的不是url重定向,而是域屏蔽。 en.wikipedia.org/wiki/Domain_Masking

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


【解决方案1】:

如果您尚未在构造函数中加载 url 帮助器,请先加载:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 2016-08-25
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 2021-10-09
    • 2011-09-10
    相关资源
    最近更新 更多