我使用PHP託管在亞馬遜Elastic Beanstalk上創建我的第一個Web應用程序,我有點在我的腦海中做什麼。我的任務是與AWS S3雲中的最終客戶指定的文件進行聯繫,將其壓縮,最後提供下載鏈接到生成的zip文件。我做了很多狩獵活動以找到我正在嘗試做什麼的實例,但是我對php的缺乏經驗一直在決定某種解決方案是否適合我。從亞馬遜s3使用PHP編碼和下載文件
我發現這個問題和回答here,看到它似乎解決一般意義上的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測試」的回聲,沒有別的。沒有錯誤,也沒有壓縮文件。我需要知道什麼或者我做什麼明顯錯誤?
非常感謝您的幫助和建議。
編輯: 這是我從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);
?>
你正在使用popen,這意味着你正在執行一個外部程序。命令行應用程序將絕對** NO **理解什麼是's3:// etc ...'意味着什麼。他們不處理網址。他們處理文件系統路徑。充其量你可以使用NFS或UNC路徑,如果你在unix/windows上。 – 2014-10-06 20:42:41