jrdavidson's avatar

File Upload to s3 Queued Job

I'm asking to find out if I the Job code below is the correct use for it or not or if there's another level of extraction that is needed to separate the concerns from the Upload of the file to s3 to updating the URL for the database main_photo_url field property.

<?php

namespace App\Jobs;

use App\Models\Property;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class ProcessProperty implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $property;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Property $property)
    {
        $this->property = $property;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->property->update([
            'main_photo_url' => Storage::disk('s3')->put('properties/'.$this->property->id, request()->main_photo_url, 'public'),
        ]);
    }
}
0 likes
1 reply
shez1983's avatar

i dont think there is anything wrong with this -

Having said that some people might say what if you moved from S3 to another service? you could create a function (a dedicated class is an overkill) which does the Storage::disk('s3')->put('properties/'.$this->property->id, request()->main_photo_url (if you use S3 in other places then adapt this code to work with all instances) that way in future you could easily just change the 's3' to 'disk' or 'ftp' instead of doing search/replace all..

1 like

Please or to participate in this conversation.