Laravel -无法完成上载到S3



我试图通过我的Laravel应用程序本地上传图像到我的S3桶。但是,它总是返回Found 1 error while validating the input provided for the GetObject operation: [Key] expected string length to be >= 1, but found string length of 0。我从运行dd()得到的一些调试数据,它返回给我一个布尔值false,这是strange。我不知道为什么。

这是我正在运行的代码,以获得上传到S3的图像,并将路径存储在我的数据库中:

if ($request->has('avatar'))
{
// Get the path to the file
$path = $request->file('avatar')->store('attachments', 's3');

// Set visibility => public
$disk = Storage::disk('s3'); 
Storage::disk('s3')->setVisibility($path, 'public');
/** @var IlluminateFilesystemFilesystemManager $disk */
$url = $disk->url($path);
$inputFields['avatar'] = $url;
}
Image::create($inputFields);
return back();

This is my Route:

// Update user profile
Route::post('/users/{user}/upload/avatar', [UserController::class, 'store']);

这是我的上传表单:

<form action="/users/{{ $user->id }}/upload/avatar" method="POST" enctype="multipart/form-data" class="space-y-4">
@csrf
<div class="flex flex-col items-start space-y-1">
<label for="" class="font-semibold">Update profile picture</label>
<input type="file" name="avatar" class="border rounded-md w-full">
</div>
<button type="submit" class="px-4 py-2 rounded-md bg-blue-600 text-white">Upload</button>
</form>

我也试过重新创建存储桶,改变存储桶的访问权限,设置权限,并创建几个具有完全访问任何存储桶的用户。

任何建议,或提示,我会很谦卑地学习。我也希望在吸取教训后,帮助其他面临同样情况的人取得一些积极的成果。谢谢你!

您可以使用以下语法上传具有公共可见性的文件https://laravel.com/docs/10.x/filesystem#file-visibility

$path = $request->file('avatar')->storePublicly('attachments', 's3');
$url = Storage::disk('s3')->url($path);

最新更新