简体   繁体   English

使用php从Amazon s3压缩和下载文件

[英]Zip and download files from Amazon s3 with php

I'm creating my first web application using php hosted on Amazon Elastic Beanstalk, and I'm a little in over my head with what to do. 我正在使用托管在Amazon Elastic Beanstalk上的php创建我的第一个Web应用程序,而且我有点想做什么。 My task is to reach out to files specified by an end customer in an AWS S3 cloud, zip them up, and finally provide a download link to the resulting zip file. 我的任务是在AWS S3云中联系最终客户指定的文件,将其压缩,最后提供到生成的zip文件的下载链接。 I've done a lot of hunting around to find an instance of what exactly I'm trying to do, but my inexperience with php has been a hinderince in determining if a certain solution would work for me. 我已经做了很多狩猎,找到了我正在尝试做什么的实例,但是我对PHP的经验不足以确定某个解决方案是否对我有用。

I found this question and response here , and seeing that it seems to address php and zip downloads in a general sense, I thought I might be able to adapt it to my needs. 我在这里发现了这个问题和响应,并且看到它似乎解决了一般意义上的php和zip下载,我想我可能能够根据我的需要进行调整。 Below is what I have in php: 以下是我在php中的内容:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require "./aws.phar";
use Aws\S3\S3Client;

$client = S3Client::factory(array(
    'key'    => getenv("AWS_ACCESS_KEY_ID"),
    'secret' => getenv("AWS_SECRET_KEY")
));

echo "Starting zip test";

$client->registerStreamWrapper();

// make sure to send all headers first
// Content-Type is the most important one (probably)
//
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="file.zip"');

// use popen to execute a unix command pipeline
// and grab the stdout as a php stream
// (you can use proc_open instead if you need to 
// control the input of the pipeline too)
//
$fp = popen('zip -r - s3://myBucket/test.txt s3://myBucket/img.png', 'r');

// pick a bufsize that makes you happy (8192 has been suggested).
$bufsize = 8192;
$buff = '';
while( !feof($fp) ) {
   $buff = fread($fp, $bufsize);
   echo $buff;
}
pclose($fp);

And here is what I'm using to call it: 这就是我用它来称呼它:

$(document).ready(function() {
    $("#download_button").click(function() {
        $.get("../php/ZipAndDownload.php", function(data){alert(data)});
        return false;
    });
});

I've also tried: 我也尝试过:

$(document).ready(function() {
        $("#download_button").click(function() {
            $.ajax({
                url:url,
                type:"GET",
                complete: function (response) {
                    $('#output').html(response.responseText);
                },
                error: function () {
                    $('#output').html('Bummer: there was an error!');
                }
            });
            return false;
        });
    });

Now whenever I click the download button, I get an echo of "Starting zip test" and nothing else. 现在每当我点击下载按钮时,我都会得到“开始拉链测试”的回声而没有别的。 No errors, and no zip file. 没有错误,也没有zip文件。 What do I need to know or what am I doing obviously wrong? 我需要知道什么或我做了什么显然是错的?

Thank you in advance for your help and advice. 提前感谢您的帮助和建议。

EDIT: Here's what I have after some advice from Derek. 编辑:这是我在德里克的一些建议后得到的。 This still produces a big nasty string of binary. 这仍然会产生一个令人讨厌的二进制字符串。

<?php
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="file.zip"');

require "./aws.phar";
use Aws\S3\S3Client;

$bucket = 'myBucket';

$client = S3Client::factory(array(
    'key'    => getenv('AWS_ACCESS_KEY_ID'),
    'secret' => getenv('AWS_SECRET_KEY')
));

$result = $client->getObject(array(
    'Bucket' => $bucket,
    'Key'    => 'test.txt',
    'SaveAs' => '/tmp/test.txt'
));

$Uri = $result['Body']->getUri();

$fp = popen('zip -r - '.$Uri, 'r');

$bufsize = 8192;
$buff = '';
while( !feof($fp) ) {
   $buff = fread($fp, $bufsize);
   echo $buff;
}
pclose($fp);
?>

Marc B's comment is correct. Marc B的评论是正确的。 The call to zip does not know what s3:// means. 对zip的调用不知道s3://是什么意思。

You will need to download the file from S3, then zip it locally. 您需要从S3下载文件,然后在本地压缩。 You have several options for downloading the file from S3. 您有几个从S3下载文件的选项。 For example, you could use: 例如,您可以使用:

$client->getObjectUrl($bucket, $key) 

to get a URL for your object, then use cUrl or wget to download from that URL, depending on your permissions settings. 要获取对象的URL,请使用cUrl或wget从该URL下载,具体取决于您的权限设置。

Once you have the file locally, update your zip command using the location where you saved the file in order to generate the .zip file. 在本地获得文件后,使用保存文件的位置更新zip命令,以生成.zip文件。

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

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