laravel - Laravel 分段上传到 AWS S3(错误 500)
问题描述
如何正确设置 fopen?由于我不知道初始文件位置,因此在尝试上传时出现 500 错误。
- 我需要使用分段上传来优化传输速度。
- 我的网站将是无服务的,并且将在 Vapor 上运行,因此在上传到 S3 之前我无法将文件保存到本地。
- 我想将文件流式上传到 S3 存储桶,文件是使用表单选择的,我不知道它们在客户端计算机上的初始本地路径……AWS 文档上显示了一些替代方案,但他们都假设你会知道已经是文件的初始位置...
$s3Client = new S3Client([
'profile' => 'default',
'region' => 'us-east-2',
'version' => 'latest'
]);
$bucket = 'your-bucket';
$key = 'my-file.zip';
// Using stream instead of file path
$source = fopen('/path/to/large/file.zip', 'rb');
$uploader = new ObjectUploader(
$s3Client,
$bucket,
$key,
$source
);
do {
try {
$result = $uploader->upload();
if ($result["@metadata"]["statusCode"] == '200') {
print('<p>File successfully uploaded to ' . $result["ObjectURL"] . '.</p>');
}
print($result);
} catch (MultipartUploadException $e) {
rewind($source);
$uploader = new MultipartUploader($s3Client, $source, [
'state' => $e->getState(),
]);
}
} while (!isset($result));
fclose($source);
解决方案
推荐阅读
- java - 意图动画在 onresponse 中不起作用
- node.js - 可以在fork中使用exec吗?
- ffmpeg - 无法使用 ffmpeg 包将双鱼眼图像转换为 equirectangular 图像
- javascript - 相同输入值的计数
- c++ - Qt:可以在ctor中创建小部件,但不能在mousePressEvent中创建
- c# - C# 中的错误:类型或命名空间名称 > `FTDI_Mgt' 找不到/缺少程序集引用
- php - 在 DB:raw 查询中使用时区
- python - 无法将“位”安装到 conda 环境中。错误:无法为使用 PEP 517 且无法直接安装的 coincurve 构建轮子
- reactjs - 如何使用 Axios 获取数据并将其存储在 Redux 存储中?
- amazon-web-services - 如何将日志从 cloudwatch 发送到 ES