我正在使用托管在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);
这就是我用来称呼它的东西:
$(document).ready(function() {
$("#download_button").click(function() {
$.get("../php/ZipAndDownload.php", function(data){alert(data)});
return false;
});
});
我也尝试过:
$(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文件。我需要知道什么或者我在做什么显然是错误的?
预先感谢您的帮助和建议。
编辑:这是我根据德里克(Derek)的一些建议后得到的。这仍然会产生很大的二进制二进制字符串。
<?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文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句