Laravel Vapor- 允许未经身份验证的用户执行上传
Laravel Vapor- Allow unauthenticated user perform an upload
默认情况下Laravel Vapor 需要对用户进行身份验证才能执行上传。我需要允许未经身份验证的用户执行上传。我该怎么做?
谢谢
我想如果你在你的 UserPolicy 中添加这样的东西,vapor 应该可以工作
public function before($user)
{
if (Auth::guest()) {
return true;
}
}
我们通过覆盖 the default vapor/signed-storage-url
route's handling.
来处理这个问题
在routes/web.php
.
Route::post('vapor/signed-storage-url', [Controllers\SignedStorageUrlController::class, 'store']);
在我们的 SignedStorageUrlController
中,我们使用删除了此检查的 store
函数:
Gate::authorize('uploadFiles', [
$request->user(),
$bucket = $request->input('bucket') ?: $_ENV['AWS_BUCKET'],
]);
导致:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Laravel\Vapor\Http\Controllers\SignedStorageUrlController as VaporSignedStorageUrlController;
class SignedStorageUrlController extends VaporSignedStorageUrlController
{
public function store(Request $request)
{
$this->ensureEnvironmentVariablesAreAvailable($request);
$bucket = $request->input('bucket') ?: $_ENV['AWS_BUCKET'];
$client = $this->storageClient();
$uuid = (string) Str::uuid();
$signedRequest = $client->createPresignedRequest(
$this->createCommand($request, $client, $bucket, $key = ('tmp/'.$uuid)),
'+5 minutes'
);
$uri = $signedRequest->getUri();
return response()->json([
'uuid' => $uuid,
'bucket' => $bucket,
'key' => $key,
'url' => 'https://'.$uri->getHost().$uri->getPath().'?'.$uri->getQuery(),
'headers' => $this->headers($request, $signedRequest),
], 201);
}
}
默认情况下Laravel Vapor 需要对用户进行身份验证才能执行上传。我需要允许未经身份验证的用户执行上传。我该怎么做?
谢谢
我想如果你在你的 UserPolicy 中添加这样的东西,vapor 应该可以工作
public function before($user)
{
if (Auth::guest()) {
return true;
}
}
我们通过覆盖 the default vapor/signed-storage-url
route's handling.
在routes/web.php
.
Route::post('vapor/signed-storage-url', [Controllers\SignedStorageUrlController::class, 'store']);
在我们的 SignedStorageUrlController
中,我们使用删除了此检查的 store
函数:
Gate::authorize('uploadFiles', [
$request->user(),
$bucket = $request->input('bucket') ?: $_ENV['AWS_BUCKET'],
]);
导致:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Laravel\Vapor\Http\Controllers\SignedStorageUrlController as VaporSignedStorageUrlController;
class SignedStorageUrlController extends VaporSignedStorageUrlController
{
public function store(Request $request)
{
$this->ensureEnvironmentVariablesAreAvailable($request);
$bucket = $request->input('bucket') ?: $_ENV['AWS_BUCKET'];
$client = $this->storageClient();
$uuid = (string) Str::uuid();
$signedRequest = $client->createPresignedRequest(
$this->createCommand($request, $client, $bucket, $key = ('tmp/'.$uuid)),
'+5 minutes'
);
$uri = $signedRequest->getUri();
return response()->json([
'uuid' => $uuid,
'bucket' => $bucket,
'key' => $key,
'url' => 'https://'.$uri->getHost().$uri->getPath().'?'.$uri->getQuery(),
'headers' => $this->headers($request, $signedRequest),
], 201);
}
}