如何使用Laravel 5和文件系统将流中的大(视频)文件上传到AWS S3? [英] How do I upload big (video) files in streams to AWS S3 with Laravel 5 and filesystem?

查看:461
本文介绍了如何使用Laravel 5和文件系统将流中的大(视频)文件上传到AWS S3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将大视频文件上传到我的AWS S3存储桶.几个小时后,我终于设法配置了 php.ini nginx.conf 文件,因此它们允许更大的文件.

I want to upload a big video file to my AWS S3 bucket. After a good deal of hours, I finally managed to configure my php.ini and nginx.conf files, so they allowed bigger files.

但是后来我得到了"Fatal Error: Allowed Memory Size of XXXXXXXXXX Bytes Exhausted".一段时间后,我发现应该使用fopen()fwrite()fclose()将较大的文件与流一起上传.

But then I got a "Fatal Error: Allowed Memory Size of XXXXXXXXXX Bytes Exhausted". After some time I found out larger files should be uploaded with streams using fopen(),fwrite(), and fclose().

由于我使用的是Laravel 5,因此文件系统会处理很多此类工作.除了我无法正常工作.

Since I'm using Laravel 5, the filesystem takes care of much of this. Except that I can't get it to work.

我当前的ResourceController@store看起来像这样:

My current ResourceController@store looks like this:

public function store(ResourceRequest $request)
{
    /* Prepare data */
    $resource = new Resource();
    $key = 'resource-'.$resource->id;
    $bucket = env('AWS_BUCKET');
    $filePath = $request->file('resource')->getRealPath();

    /* Open & write stream */
    $stream = fopen($filePath, 'w');
    Storage::writeStream($key, $stream, ['public']);

    /* Store entry in DB */
    $resource->title = $request->title;
    $resource->save();

    /* Success message */
    session()->flash('message', $request->title . ' uploadet!');
    return redirect()->route('resource-index');
}

但是现在我得到了一个很长的错误:

But now I get this long error:

SignatureV4.php第148行中的

CouldNotCreateChecksumException:

CouldNotCreateChecksumException in SignatureV4.php line 148:

无法为提供的上载主体计算sha256校验和,因为它不可寻找.为防止此错误,您可以1)在请求中包含ContentMD5或ContentSHA256参数,2)对主体使用可搜索的流,或3)将不可搜索的流包装在GuzzleHttp \ Stream \ CachingStream对象中.但是,您应该小心并记住CachingStream使用PHP临时流.这意味着流将被临时存储在本地磁盘上.

A sha256 checksum could not be calculated for the provided upload body, because it was not seekable. To prevent this error you can either 1) include the ContentMD5 or ContentSHA256 parameters with your request, 2) use a seekable stream for the body, or 3) wrap the non-seekable stream in a GuzzleHttp\Stream\CachingStream object. You should be careful though and remember that the CachingStream utilizes PHP temp streams. This means that the stream will be temporarily stored on the local disk.

所以我目前完全迷路了.我不知道自己是否走在正确的轨道上.这是我尝试理解的资源:

So I am currently completely lost. I can't figure out if I'm even on the right track. Here are the resource I try to make sense of:

  • AWS SDK guide for PHP: Stream Wrappers
  • AWS SDK introduction on stream wrappers
  • Flysystem original API on stream wrappers

让我更加困惑的是,似乎还有另一种上传大文件而不是流的方法:所谓的.我实际上以为那是所有的溪流……

And just to confuse me even more, there seems to be another way to upload large files other than streams: The so called "multipart" upload. I actually thought that was what the streams where all about...

有什么区别?

推荐答案

流媒体部分适用于下载.

the streaming part applies to downloads.

对于上传,您需要知道内容的大小.对于大文件,分段上传是必经之路.

for uploads you need to know the content size. for large files multipart uploads are the way to go.

这篇关于如何使用Laravel 5和文件系统将流中的大(视频)文件上传到AWS S3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆