It seems like the issue you're encountering is related to the S3 key being empty when you're trying to upload an image to S3. This can happen if the image path is not being set correctly. To resolve this issue, you need to ensure that you're using the correct disk configuration for S3 and that you're specifying the disk when you call the storeAs method.
Here's a revised version of your uploadRender method that explicitly sets the disk to s3:
public function uploadRender(UploadRenderRequest $request): JsonResponse
{
try {
$validated = $request->validated();
$image = $validated['image'];
$modelClass = 'App\Models\' . $validated['parentModelName']; // Make sure to escape the backslash
$model = $modelClass::findOrFail($validated['id']);
// Specify the disk as 's3' when storing the image
$imagePath = $image->storeAs(
'renders',
$image->getClientOriginalName(),
's3' // Specify the disk here
);
$projectImage = $model->images()->create([
'path' => $imagePath,
'type' => ImageTypeEnum::RENDER_IMAGE,
]);
return response()->json([
'message' => 'Render successfully uploaded',
'image' => ImageResource::make($projectImage)->getImagePath(),
]);
} catch (\Exception $e) {
return response()->json(['error' => $e->getMessage()], 500);
}
}
Make sure that you have the S3 disk configured correctly in your config/filesystems.php file:
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
// Add other necessary configuration options here
],
Also, ensure that your .env file contains the correct AWS credentials and bucket information:
AWS_ACCESS_KEY_ID=your_access_key_id
AWS_SECRET_ACCESS_KEY=your_secret_access_key
AWS_DEFAULT_REGION=your_region
AWS_BUCKET=your_bucket_name
By specifying the disk as s3 and ensuring that your AWS credentials and configuration are correct, you should be able to upload images to S3 without encountering the error you mentioned.