2016-11-21 180 views
3

我使用the PHP Flysystem包處理來自我的AWS S3存儲桶的內容。特別是,我正在使用$filesystem->readStream如何將文件流式傳輸到AWS S3中的一個Zip中

我的問題

當我流的文件,它在myzip.zip結束和大小是正確的,但如果將它解壓縮,它成爲myzip.zip.cpgz。這是我的原型:

header('Pragma: no-cache'); 
header('Content-Description: File Download'); 
header('Content-disposition: attachment; filename="myZip.zip"'); 
header('Content-Type: application/octet-stream'); 
header('Content-Transfer-Encoding: binary'); 
$s3 = Storage::disk('s3'); // Laravel Syntax 
echo $s3->readStream('directory/file.jpg'); 

我在做什麼錯?

邊問

當我流的文件這樣的,做的:

  1. 得到完全下載到我的服務器的內存,然後得到轉移到客戶端,或
  2. 做它在緩衝區中保存 - 分塊 - 然後傳送到客戶端?

基本上被負擔我的服務器,如果我有有幾十個GB的數據被傳輸的內容?

回答

2

您目前正在將directory/file.jpg的原始內容作爲zip(其中jpg不是zip)轉儲。您需要創建一個包含這些內容的zip文件。

而不是

echo $s3->readStream('directory/file.jpg'); 

嘗試使用在其位以下的Zip extension

// use a temporary file to store the Zip file 
$zipFile = tmpfile(); 
$zipPath = stream_get_meta_data($zipFile)['uri']; 
$jpgFile = tmpfile(); 
$jpgPath = stream_get_meta_data($jpgFile)['uri']; 

// Download the file to disk 
stream_copy_to_stream($s3->readStream('directory/file.jpg'), $jpgFile); 

// Create the zip file with the file and its contents 
$zip = new ZipArchive(); 
$zip->open($zipPath); 
$zip->addFile($jpgPath, 'file.jpg'); 
$zip->close(); 

// export the contents of the zip 
readfile($zipPath); 

使用tmpfilestream_copy_to_stream,它將在塊下載到臨時文件在磁盤上,沒有進入內存

+0

什麼是tmpfile()在這種情況下?這應該是獲取臨時文件的路徑嗎? –

+1

@ mark.inman PHP的[tmpfile()](http://php.net/manual/en/function.tmpfile.php)函數 「以讀寫(w +)模式創建具有唯一名稱的臨時文件,並且返回一個文件句柄。「 要獲取臨時文件的路徑,請執行[stream_get_meta_data()](http://php.net/manual/en/function.stream-get-meta-data.php)函數並訪問其返回值的' uri'用'$ zipPath'和'$ jpgPath'顯示 – bradynpoulsen

相關問題