is the 10-15 seconds the upload from your dev environment or from a public server?
Aug 2, 2021
6
Level 5
Upload large files to S3 using Laravel 5
I have a file with a size of more than 4MB uploaded to S3 while the user registers in my app.
Here are my ways to upload:
- Upload directly to S3 without any job queue as follow:
$data = $request->all();
if ($image = $request->file('image')) {
Storage::disk('s3')->put('uploads/user/', $image, 'public');
$data['image'] = $image->hashName();
}
$user = User::create($data);
With the above approach, it takes 3 to 4 seconds to upload!
- I said let's try to create a job queue to make the request faster, so my controller is changed to
$data = $request->all();
$user = User::create($data);
if ($file = $request->file('image')) {
$file->move(public_path() . '/temporary_images', $imageName = uniqid());
$extension = $file->getClientOriginalExtension();
dispatch(new RegisterJob($user, $imageName, $extension));
}
Then in my job:
class RegisterJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $user, $imageName, $extension;
public function __construct($user, $imageName, $extension)
{
$this->user = $user;
$this->imageName = $imageName;
$this->extension = $extension;
}
public function handle()
{
$imageName = $this->imageName . '.' . $this->extension;
$imagePath = public_path() . '/temporary_images/' . $this->imageName;
if (Storage::disk('s3')->put('uploads/user/' . $imageName, fopen($imagePath, 'r+'), 'public')) {
$this->user->update(['image' => $imageName]);
File::delete($imagePath);
}
}
}
With this approach, it takes 10 to 15 seconds to upload the file with 4MB!!!
What is the best approach to upload an image using job and s3!?
Please or to participate in this conversation.