【问题标题】:PHP Amazon S3 access private files through URLPHP Amazon S3 通过 URL 访问私有文件
【发布时间】:2015-04-27 17:18:07
【问题描述】:

我正在使用 AWS PHP sdk 在 S3 上保存图像。文件以私密方式保存。然后,我在我的 Web 应用程序中使用 S3 文件 url 显示图像缩略图,但由于文件是私有的,所以图像显示为损坏。

当用户单击文件名时,会打开一个模式以显示更大的文件,但由于同样的问题,文件在那里也显示为损坏。

现在,我知道有两种方法可以实现这一点。 1。公开文件。 2. 为文件生成预签名的 url。 但是由于我的项目的要求,我不能使用这两个选项中的任何一个。

我的问题是有没有第三种方法可以解决这个问题?

【问题讨论】:

  • 面临同样的问题。为此 +1
  • 你找到解决办法了吗?
  • 不完全是,我必须将图像上传到具有公共读取权限的 S3 上,才能在浏览器中访问它们。
  • @SibtainNorain 你找到解决方案了吗?

标签: php amazon-web-services amazon-s3 aws-sdk


【解决方案1】:

我强烈建议不要这样做,但您可以在自己的服务器上创建一个脚本,通过 API 拉取图像,缓存它并提供服务。然后,您可以随意限制访问,而无需公开图像。

通过脚本示例:

$headers = get_headers($realpath); // Real path being where ever the file really is

foreach($headers as $header) {
    header($header);
}
$filename = $version->getFilename();

// These lines if it's a download you want to do
// header('Content-Description: File Transfer');
// header("Content-Disposition: attachment; filename={$filename}");

$file = fopen($realpath, 'r');
fpassthru($file);
fclose($file);
exit;

这几乎不会“触及侧面”并且不会过多地延迟文件的出现,但仍会占用一些资源和带宽。

【讨论】:

    【解决方案2】:

    您需要通过服务器上的脚本访问这些文件。该脚本将执行某种身份验证以确保请求有效并且您希望他们看到该文件。然后使用可以访问私有文件的有效 IAM 配置文件从 S3 获取文件。输出文件

    而不是从 S3 请求文件 http://www.yourdomain.com/fetchimages.php?key=8498439834

    那么这里是 fetchimages.php 中的一些伪代码

    <?php
    
    //if authorized to get this image
    
    $key=$_GET['key'];
    //validate key is the proper format
    
    //get s3 url from a database based on the $key
    
    //connect to s3 securely and read the file from s3
    
    //output the file
    
    ?>
    

    【讨论】:

      【解决方案3】:

      据我所知,您可以尝试将您的 S3 存储桶设为“Web 服务器”like this,但随后您可能会“公开文件”。然后,如果您有某种逻辑来限制访问,您可以创建一个bucket policy

      【讨论】:

        猜你喜欢
        • 2011-12-02
        • 2011-10-21
        • 2012-10-11
        • 2020-10-08
        • 1970-01-01
        • 1970-01-01
        • 2017-02-05
        • 2015-07-27
        • 1970-01-01
        相关资源
        最近更新 更多