Reputation: 5113
I'm trying to set up a Job that uses the Storage
facade. Unfortunately this is not working and is throwing an Exception.
Serialization of 'Symfony\Component\HttpFoundation\File\File' is not allowed
class MoveMusicToRemoteStorage extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, DispatchesJobs;
private $audio;
public function __construct(AudioFile $audio)
{
$this->audio = $audio;
}
public function handle()
{
Storage::disk('s3')->put('/audio/' . $this->audio->file()->getFilename(), file_get_contents($this->audio->getPath()));
}
}
How can I use a Facade within the job?
EDIT
I've read this post but I do not own the File class so I cannot make it Serializable.
Upvotes: 2
Views: 624
Reputation: 1829
The problem is that you are passing a File
object to the job constructor and the file cannot be serialized. It has nothing to do with the façade. You can use the façade as you are, but to get around the problem, you need to pass a reference to the file you want (like its file name/path) and then retrieve the file via the File
façade within your handle()
method:
class MoveMusicToRemoteStorage extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, DispatchesJobs;
private $audio_file_path;
public function __construct($audio_file_path)
{
$this->audio_file_path = $audio_file_path;
}
public function handle()
{
$audio_file = Storage::get($this->audio_file_path);
Storage::disk('s3')->put('/audio/' . $audio_file->file()->getFilename(), file_get_contents($audio_file->getPath()));
}
}
If you are doing this with a file upload, you will probably want to not use a job. File uploads are stored in the temporary directory and the file could disappear before you have a chance to move it to S3 if the job were ever to be queued.
Upvotes: 2