【问题标题】:Compress and upload to AWS S3压缩并上传到 AWS S3
【发布时间】:2015-04-19 00:56:51
【问题描述】:

我有用户将文件上传到我的服务器,通常是大型 HTML (4-8 MB) 文件。然后服务器将其推送到 S3 并将文件提供给用户。我需要的是在将文件推送到 S3 之前压缩文件并将压缩文件提供给用户。我怎样才能做到这一点?我的后端运行着 APACHE、PHP。

【问题讨论】:

    标签: php amazon-web-services amazon-s3 compression gzip


    【解决方案1】:

    您可以使用 PHP 压缩文件并将压缩文件推送到 S3。

    // Name of the file to compress
    $file = "yourfile.txt";
    
    // Name of compressed gz file 
    $gzfile = "test.gz";
    
    // Open the gz file (w9 is the highest compression)
    $fp = gzopen ($gzfile, 'w9');
    
    // Compress the file
    gzwrite ($fp, file_get_contents($file));
    
    //close the file after compression is done
    gzclose($fp);
    

    【讨论】:

    • 并将.gz文件推送到S3?
    • 是的,假设可以使用 .gz 文件为您的用户提供服务
    • 我们如何将.gz文件提供给用户浏览器并解压?回到html?添加标题内容类型有帮助吗?
    • 您需要将内容类型设置为 application/x-gzip。请注意,您不能上传 gz 文件,然后在 S3 中将其解压缩。
    • 如何添加流上下文来设置元数据,例如 Content-Type?使用 php 的 fopen 函数有第四个参数 $context ,但 gzopen 没有。 ?
    【解决方案2】:
    1. 您在其他答案中有示例如何压缩到 gzip

    2. 可以通过 aws 控制台复制到 s3:

      aws s3 cp /path/test.gz s3://your_bucket/path/

    您需要将密钥/付费凭证放入 ~/.aws/config 文件

    1. 这里是一些如何使用 Apache apache .gz gzip content handler for Linux documentation /usr/share/doc and localhost/doc/ 提供 gzip 压缩内容的答案

    我相信您需要将 s3fs-fuse(https://code.google.com/p/s3fs/) 文件系统集成到您的 Web 服务器以读取 gzip 压缩的内容

    另外我对 s3fs 有一些经验,不建议你尝试从服务器直接写入挂载的 s3fs 文件系统,这会带来麻烦和系统崩溃。最好的方法是通过 aws 控制台复制内容,但使用挂载的文件系统进行只读访问

    更新:有关 s3fs 问题以及如何使用 aws s3 upload api 的更多详细信息,您可以在我这里查看其他答案 How could I store uploaded images to AWS S3 on PHP

    【讨论】:

      猜你喜欢
      • 2014-07-26
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      相关资源
      最近更新 更多