ahmedeid46's avatar

Two queues to upload files

I want to create two queues to upload a large group of images, which will be uploaded to my server and then uploaded to OneDrive I want the following to be uploaded sequentially, and when a user finishes uploading photos, he executes a specific command

0 likes
1 reply
LaryAI's avatar
Level 58

To achieve the sequential upload of images to your server and then to OneDrive using queues, you can use Laravel's queue system. Here's a step-by-step solution:

  1. Create Jobs for Uploading: You'll need two jobs: one for uploading to your server and another for uploading to OneDrive.

  2. Configure Queues: Make sure you have your queue system configured in config/queue.php. You can use the database, Redis, or any other driver that Laravel supports.

  3. Create the First Job (Upload to Server):

    php artisan make:job UploadToServer
    

    In the UploadToServer job, handle the file upload logic to your server.

    <?php
    
    namespace App\Jobs;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Foundation\Bus\Dispatchable;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Queue\SerializesModels;
    
    class UploadToServer implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
        protected $image;
    
        public function __construct($image)
        {
            $this->image = $image;
        }
    
        public function handle()
        {
            // Logic to upload image to your server
        }
    }
    
  4. Create the Second Job (Upload to OneDrive):

    php artisan make:job UploadToOneDrive
    

    In the UploadToOneDrive job, handle the file upload logic to OneDrive.

    <?php
    
    namespace App\Jobs;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Foundation\Bus\Dispatchable;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Queue\SerializesModels;
    
    class UploadToOneDrive implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
        protected $image;
    
        public function __construct($image)
        {
            $this->image = $image;
        }
    
        public function handle()
        {
            // Logic to upload image to OneDrive
        }
    }
    
  5. Dispatch Jobs Sequentially: When a user finishes uploading photos, dispatch the UploadToServer job. Once the first job is completed, dispatch the UploadToOneDrive job.

    $image = 'path/to/image.jpg'; // Replace with actual image path or object
    
    // Dispatch the job to upload to the server
    UploadToServer::dispatch($image)->chain([
        // Dispatch the job to upload to OneDrive after the first job is successful
        new UploadToOneDrive($image)
    ]);
    
  6. Handle Failed Jobs (Optional): You may want to handle cases where a job fails. You can define a failed method within your job class to handle such scenarios.

    public function failed(Exception $exception)
    {
        // Handle the failure, log it, send notifications, etc.
    }
    
  7. Run the Queue Worker: To process the jobs, you need to run the queue worker. You can do this by running the following command:

    php artisan queue:work
    

Remember to replace the placeholder logic in the handle methods with the actual logic for uploading files to your server and OneDrive. Also, ensure that you have set up the necessary configurations and authentication for interacting with OneDrive's API.

Please or to participate in this conversation.