2016-09-04 18 views
0

S3からストリームコンテンツを圧縮するための優れたソリューションをチェックしていますが、s3-bucket-stream-zip-php APIで使用されているZipStream-PHP APIに出くわしましたが、コアPHPクラスZipArchiveの助けを借りて機能ZipArchive::addFromString我々は同じを達成することができます。PHPコアZipArchiveとZipStream-PHP

私のクエリはZipStream-PHP APIです。それでは、ZipArchiveはS3からのストリームコンテンツを圧縮しますか他のクラウドサービスですか?

+0

誰かが投票ダウンしている場合は、同様のコメントにあなたの理由を追加してください。 –

+2

私はこのトピックについていくつかの助けを探していました!誰かが自分の経験に基づいて提案することができれば助けになるだろう!! – jas

+0

ちょうどこれを見て、あなたに以下の解決策を与えました。 – Forer

答えて

0

aws-sdk-phpを使用して、s3client経由でS3上のオブジェクトにアクセスし、registerStreamWrapper()を有効にすることをお勧めします。その後、fopenを使ってS3からオブジェクトをストリーミングし、そのストリームをZipStreamのaddFileFromStream()関数に直接送り、そこからZipStreamに渡します。 ZipArchiveなし、大量のメモリオーバーヘッドなし、サーバーでのジップの作成、またはWebサーバー上のS3からのファイルの複製は、後でジップをストリームするために使用されません。だから、

//... 

$s3Client->registerStreamWrapper(); //required 

//test files on s3 
$s3keys = array(
    "ziptestfolder/file1.txt", 
    "ziptestfolder/file2.txt" 
); 

// Define suitable options for ZipStream Archive. 
$opt = array(
      'comment' => 'test zip file.', 
      'content_type' => 'application/octet-stream' 
      ); 

//initialise zipstream with output zip filename and options. 
$zip = new ZipStream\ZipStream('test.zip', $opt); 

//loop keys useful for multiple files 
foreach ($s3keys as $key) { 

     // Get the file name in S3 key so we can save it to the zip 
     //file using the same name. 
     $fileName = basename($key); 

     //concatenate s3path. 
     $bucket = 'bucketname'; 
     $s3path = "s3://" . $bucket . "/" . $key;   

     //addFileFromStream 
     if ($streamRead = fopen($s3path, 'r')) { 
      $zip->addFileFromStream($fileName, $streamRead);   
     } else { 
      die('Could not open stream for reading'); 
     } 
} 

$zip->finish(); 

そして、あなたはsymfonyのコントローラのアクションでZipStreamを使用している場合は、あまりにもこの答えを参照してください。https://stackoverflow.com/a/44706446/136151

関連する問題