Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php从AmazonS3压缩和下载文件_Php_Amazon Web Services_Amazon S3_Zip - Fatal编程技术网

使用php从AmazonS3压缩和下载文件

使用php从AmazonS3压缩和下载文件,php,amazon-web-services,amazon-s3,zip,Php,Amazon Web Services,Amazon S3,Zip,我正在使用托管在Amazon Elastic Beanstalk上的php创建我的第一个web应用程序,我有点不知所措。我的任务是接触AWS S3云中的最终客户指定的文件,将它们压缩,最后提供一个到生成的压缩文件的下载链接。我花了很多时间四处寻找我想要做的事情的实例,但是我对php的缺乏经验阻碍了我决定某个解决方案是否适合我 我找到了这个问题和答案,看到它似乎在一般意义上解决了php和zip下载问题,我想我可能能够根据自己的需要对它进行调整。以下是我在php中的内容: <?php erro

我正在使用托管在Amazon Elastic Beanstalk上的php创建我的第一个web应用程序,我有点不知所措。我的任务是接触AWS S3云中的最终客户指定的文件,将它们压缩,最后提供一个到生成的压缩文件的下载链接。我花了很多时间四处寻找我想要做的事情的实例,但是我对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);
我也试过:

$(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;
        });
    });
现在,每当我点击下载按钮时,我都会听到“开始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);
?>

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

您需要从S3下载该文件,然后在本地压缩它。从S3下载文件有几个选项。例如,您可以使用:

$client->getObjectUrl($bucket, $key) 
要获取对象的URL,请使用cUrl或wget从该URL下载,具体取决于您的权限设置


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

您使用的是popen,这意味着您正在执行外部程序。命令行应用程序完全不知道heck
s3://etc.
的意思。它们不处理URL。它们处理文件系统路径。如果您在unix/windows上,最多可以使用NFS或UNC路径。谢谢您的提示,德里克。我已经更改了脚本,使用S3的getObject()并选择将文件保存在本地存储中。引用本地目录中文件的位置似乎像我希望的那样创建了一个zip包,但是当我回显结果时,我得到了一个巨大的二进制字符串,表示我的zip文件,而不是下载它的提示。我尝试将内容类型标题更改为application/zip,但没有效果。我还可能遗漏什么?您是否回显了标题('Content-type:…')行之前的任何内容?或者,如果没有,您的页面在仍然没有骰子之前是否有任何字符(甚至换行符)。我已经把我的剧本缩减到必要的部分,并尝试了你的建议。我已经用我现在拥有的编辑了我的原始帖子。我能想到的唯一一件事是,也许是我用来调用php脚本的jquery造成的?如果你看不出它有什么问题,我可能不得不寻找另一种方法。谢谢你的帮助。我想问题在于这句话:echo“开始zip测试”;您将在内容标题之前向浏览器发送响应,因此标题将被忽略。我已在原始帖子下方添加了编辑。在你第一次发表评论后,我取出了回音声明。很抱歉给你带来了困惑。
$client->getObjectUrl($bucket, $key)