singh's avatar
Level 1

Laravel 7 - The image object from S3 is fetched but cannot be displayed

Added more information based on L7 docs at the bottom.

I am successfully able to upload the file to S3 and get the object URI. However when the URI is put in the image tag, it shows a broken image. I can print the raw URI in my view as well as using dd. The $logoname below is pulled from a database query.

    $logoname = $company->logo;  
        $destinationPath = "logos/";
        $value = $destinationPath.$logoname;
        $disk = Storage::disk('s3');
        if (isset($logoname)) {
        	if ($disk->exists($value)) {
        	$command = $disk->getDriver()->getAdapter()->getClient()->getCommand('GetObject', [
        		'Bucket'                     => env('S3_BUCKET'),
        		'Key'                        => $value
         		]);
       
        $request = $disk->getDriver()->getAdapter()->getClient()->createPresignedRequest($command, '+200 minutes');
        $myuri = (string) $request->getUri();

   return view('company', compact(['myuri']
   }
   }

Based on Laravel 7 docs - as suggested by @bobbybouwmann below I have also attempted the following:

$myuri = Storage::get($value);

$myuri = $disk->get($value);

$myuri = Storage::disk('s3')->get($value);

$myuri = Storage::disk('s3')->url($value);

$myuri = Storage::disk('s3')->temporaryUrl( $value, now()->addMinutes(5), ['ResponseContentType' => 'application/octet-stream'] );

I have even set the bucket to public access.

Error if the URL pasted in browser:

<Error>

<Code>NoSuchKey</Code>

<Message>The specified key does not exist.</Message>

<Key>logos/phpSOzViN1598729897lyoDFyBoLJssn7Dbrf9ORHTV44fyr8xpOwmDSUzN6AgJDVtyZvqs4MRw2dNKFJgPvdl2UpUu6AORscvJ.jpg</Key>

<RequestId>a small string </RequestId>

<HostId>a long string with a host id</HostId>

</Error>

0 likes
3 replies
Snapey's avatar

open the S3 console

navigate to your bucket, and to the correct folder

click on the file

Look at the list of properties. At the bottom is Object URL

compare this string to what you get with Storage::disk('s3')->url($value);

They should be identical

singh's avatar
Level 1

For now I ended up making the file public at the time of storage by using the following:

Storage::disk('s3')->put($filePath, file_get_contents($file), 'public');

However, there should be a way to keep the files private and make them accessible only to the application. I was successfully able to do that with Laravel 5.1 but for some reason the same S3 drive with same API keys is not working for Laravel 7.

Please or to participate in this conversation.