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

KDh*_*yne 5 php zip amazon-s3 amazon-web-services

我正在使用托管在 Amazon Elastic Beanstalk 上的 php 创建我的第一个 Web 应用程序,我有点不知所措。我的任务是访问 AWS S3 云中最终客户指定的文件,将它们压缩,最后提供指向生成的 zip 文件的下载链接。我已经做了很多搜索来找到我正在尝试做的事情的实例,但是我对 php 的缺乏经验阻碍了确定某个解决方案是否适合我。

我在这里找到了这个问题和回复,并且看到它似乎在一般意义上解决了 php 和 zip 下载,我想我可以根据我的需要调整它。以下是我在 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);
Run Code Online (Sandbox Code Playgroud)

这是我用来称呼它的:

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

我也试过:

$(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;
        });
    });
Run Code Online (Sandbox Code Playgroud)

现在,每当我单击下载按钮时,我都会收到“正在启动 zip 测试”的回声,而没有其他提示。没有错误,也没有zip文件。我需要知道什么或者我做错了什么?

预先感谢您的帮助和建议。

编辑:这是我在听了德里克的一些建议后得到的。这仍然会产生一大串令人讨厌的二进制字符串。

<?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);
?>
Run Code Online (Sandbox Code Playgroud)

Der*_*rth 4

马克 B 的评论是正确的。对 zip 的调用不知道 s3:// 的含义。

您需要从 S3 下载该文件,然后将其压缩到本地。您可以通过多种方式从 S3 下载文件。例如,您可以使用:

$client->getObjectUrl($bucket, $key) 
Run Code Online (Sandbox Code Playgroud)

获取对象的 URL,然后使用 cUrl 或 wget 从该 URL 下载,具体取决于您的权限设置。

在本地获得该文件后,请使用保存该文件的位置更新 zip 命令以生成 .zip 文件。