I am uploading files to Amazon S3 with Laravel filesystem. The upload process works great, however, when I download the files get corrupted. I have manually downloaded the files from the S3 bucket and that way the files don't get corrupted, so I figured that the problem is not the upload.
I am uploading the files like this:
/**
 * Upload the file to 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 to download I 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 I get the files, however they become corrupted. Any help is appreciated!