2016-11-21 3 views
3

AWS S3 버킷에서 콘텐츠를 스트리밍하려면 the PHP Flysystem 패키지를 사용하고 있습니다. 특히 $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. 가 완전히 다음 클라이언트로 전송받을 내 서버의 RAM으로 다운로드받을를, 또는
  2. 그것을 수행 청크로 저장되어 버퍼에 저장되고 클라이언트로 전송됩니까?

기본적으로 수십 GB의 데이터가 스트리밍되는 경우 서버가 부담을 겪고 있습니까?

답변

2

현재 directory/file.jpg의 원시 내용을 zip으로 덤프하고 있습니다 (jpg는 zip이 아닙니다). 이러한 내용으로 zip 파일을 만들어야합니다.

대신

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

사용하여 그 자리에서 다음을 시도해보십시오 Zip extension : tmpfilestream_copy_to_stream를 사용

// 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); 

, 그것은 디스크에 임시 파일에 청크로 다운로드하고 RAM에 없습니다.

+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

관련 문제