Tuesday, January 23, 2018

How to store a file on the local storage system

Leave a Comment

I'm trying to upload a file to a temporary location with Laravel Storage Facade and its saying that the file doesn't exist. I'm trying to put the file into ./storage/app/roofing/projects/{id}/then the file. Not sure what I'm doing wrong here.

foreach ($files as $file) {     $path = Storage::putFile('contract-assets', new File('../storage/app/roofing/projects/'. $project->id.'/'. $file)); } 

I am having to send the file after being stored on my local server to AWS S3 because of the time it takes to try and upload straight to S3 and with my case it times due to the amount of files needing to be stored. As of right now all I get back is true in my dd(). What could be the casue of this.

if (!empty($request->contract_asset)) {     $files = $request->file('contract_asset');     foreach ($files as $file) {         Storage::putFile(Carbon::now()->toDateString().'/roofing/projects/'. $project->id.'/contract-assets', $file);     } }  foreach (Storage::files(Carbon::now()->toDateString().'/roofing/projects/'.$project->id.'/contract-assets') as $file) {     dispatch(new ProcessRoofingProjectContractAssets($file, $project)); } 

My Job file.

/**  * Execute the job.  *  * @return void  */ public function handle() {     $path = Storage::disk('s3')->put('contract-assets', $this->asset, 'public');     dd($path);     $this->project->addContractAsset($path);     } 

UPDATE:

This is my current changes and I am receiving the following error message. Failed because Unable to JSON encode payload. Error code: 5

foreach ($files as $file) {     $returnedStoredFile = Storage::putFile('/roofing/projects/' . $project->id . '/contract-assets/'.Carbon::now()->toDateString(), $file);     dispatch(new ProcessRoofingProjectContractAssets(Storage::get($returnedStoredFile), $project)); } 

1 Answers

Answers 1

You shouldn't be passing an entire file as parameter to your Job. Laravel will serialize it, and it would be extremely memory inefficient. (It's probably during this serialization that you are having this issue now - Failed because Unable to JSON encode payload. Error code: 5)

I'm assuming that you're not having problems to upload in the temporary folder, and you are having problems to dispatch the job (or executing the job).

Edit your job to receive a file path as parameter. Only within the execution of the job you'll go to disk and read it.

Dispatching the job:

foreach ($files as $file) {     $returnedStoredFile = Storage::putFile('/roofing/projects/' . $project->id . '/contract-assets/'.Carbon::now()->toDateString(), $file);     dispatch(new ProcessRoofingProjectContractAssets($returnedStoredFile, $project)); } 

And executing it:

/**  * Execute the job.  *  * @return void  */ public function handle() {     $file = Storage::get($this->asset);     $path = Storage::disk('s3')->put('contract-assets', $file, 'public');     dd($path);     $this->project->addContractAsset($path);     } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment