jrdavidson's avatar

Asset Save to Local and then S3

I'm trying to figure out what I'm doing wrong. I'm trying to attempt the following.

  1. Save files to local storage and store the location of the file to DB.
  2. Send Job to upload an image to AWS S3 and return back and update DB with the new location of the file.
  3. When the request is redirected to next page show images from DB regardless of their location whether it be from local or S3 whatever the URL is.

I have even recreated a symbolic link with the php artisan storage:link comamnd.

The problem I'm facing is making sure that the images are viewable after redirect from the view from the right location whether it be from S3 bucket or local storage.

foreach ($request->file('contract_asset') as $file) {
    $path = Storage::disk('local')->putFile(Carbon::now()->toDateString().'/roofing/projects/'.$project->id.'/contract-assets', $file, 'public');
    $project->addContractAsset('contract', $path);               
}

$contractAssets = Storage::allFiles(Carbon::now()->toDateString().'/roofing/projects/'.$project->id.'/contract-assets');

ProcessRoofingProjectAssets::dispatch('contract', $contractAssets, $project);
public function addContractAsset($category, $asset)
    {
        $contract_asset_urls = json_decode($this->contract_asset_urls, true);
        $contract_asset_urls[] = ['category' => $category, 'url' => $asset];
        $this->update([
            'contract_asset_urls' => json_encode($contract_asset_urls)
        ]);

        if ($category == 'approval') {
            event(new AssociationApprovalAssetUploaded($this));
        }
    }
@if( $file_info["extension"] == 'pdf')

    <a href="{{ asset($asset->url) }}" target="_blank"><i class="fa fa-file-pdf-o" style="font-size: 40px"></i></a>
@else
<a href="{{ asset($asset->url) }}" 
     data-lightbox="contract-lightbox"
     data-title="<a href='{{ Storage::url($asset->url)}}' target='_blank' class='btn btn-xs btn-info no-mobile-change' style='color: #FFF'>View Full Size</a>"><img
    src="{{ Storage::url($asset->url) }}"
    class="img-fluid"/>
</a>
@endif
0 likes
1 reply
simioluwatomi's avatar

Where exactly are you not making a headway? Are the files not being uploaded at all to local storage? Or are they not being uploaded to s3?

Which is it?

Please or to participate in this conversation.