Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a file on the local storage system

I'm trying to upload a file to a temporary location with Laravel Storage Facade and it says that the file doesn't exist. I'm trying to upload the file into ./storage/app/roofing/projects/{id}/then the file. Not sure on where exactly I'm going wrong here.

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

I have 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 in my case it times due to the number of files needing to be stored. As of now, all I get back is true in my dd(). What could be the cause 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:

These are 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));
}
like image 733
user3732216 Avatar asked Jan 10 '18 19:01

user3732216


1 Answers

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);    
}
like image 184
Diogo Sgrillo Avatar answered Sep 29 '22 05:09

Diogo Sgrillo