php - Laravel file downloaded from AWS S3 with Filesystem gets corrupted -


i uploading files amazon s3 laravel filesystem. upload process works great, however, when download files corrupted. have manually downloaded files s3 bucket , way files don't corrupted, figured problem not upload.

i uploading files this:

/**  * upload file amazon s3.  *  * @param uploadedfile $file  * @param $path  * @return $this|bool  */ protected function upload(uploadedfile $file, $path) {     $this->filename = $path . '/' . time() . '_' . str_replace(' ', '-', $file->getclientoriginalname());      $disk = storage::cloud();      if ($disk->put($this->filename, fopen($file, 'r+'))) {          $this->save();          return $this;     }      return false; } 

and download have tried this:

/**  * @param document $document  * @return response  */ public function download(document $document) {     $file = storage::cloud()->get($document->path);      $file_info = new finfo(fileinfo_mime_type);      return response($file, 200)->withheaders([         'content-type'        => $file_info->buffer($file),         'content-disposition' => 'inline; filename="' . $document->name . '"'     ]); } 

and this:

/**  * @param document $document  * @return response  */ public function download(document $document) {     $stream = storage::cloud()->getdriver()->readstream($document->path);      $file = stream_get_contents($stream);      $file_info = new finfo(fileinfo_mime_type);      return response($file, 200)->withheaders([         'content-type'        => $file_info->buffer($file),         'content-disposition' => 'inline; filename="' . $document->name . '"'     ]); } 

with both download functions files, become corrupted. appreciated!

the problem output buffer contained whitespace. using ob_end_clean() before returning response solved issue, upon finding whitespace on file before opening <?php tag, there no need use ob_end_clean().

here code without using presigned url:

/**  * download document s3.  *  * @param document $document  * @return response  */ public function download(document $document) {     $s3client = storage::cloud()->getadapter()->getclient();      $stream = $s3client->getobject([         'bucket' => 'bucket',         'key'    => $document->path     ]);      return response($stream['body'], 200)->withheaders([         'content-type'        => $stream['contenttype'],         'content-length'      => $stream['contentlength'],         'content-disposition' => 'inline; filename="' . $document->name . '"'     ]); } 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -