How to turn s3 object string into something useful when using laravel 5.1 file system

I'm at a loss. I am trying to display an object (image.jpg) that I successfully loaded into my s3 bucket.

I made sure the file is configured to publish. I am using Storage :: get (); a method that says doc "returns an object string". See here:

The get method can be used to retrieve the contents of this file. The contents of the original line of the file will be returned by the method:

$contents = Storage::get('file.jpg');

And of course, when my show method looks like this:

public function show($id)
{
    /* Get DB instance */
    $resource = Resource::findOrFail($id);

    /* Create s3 file path */
    $filePath = 'resource-'.$resource->id;

    /* Get object in s3 bucket */
    $file = Storage::get($filePath);

    return view('resource.show', compact('resource', 'file'));
}

It displays the following below, in my opinion, when I do {!! $file !!}

p s0wr? 4miq V? Pr; - ## ?? EM cA. {{ 1 Whuf | Ji { = 3 P 1 3 1Y W N / HntÏ [? 4Uԅ8uwXBr = F8ԟȰTT       ] Q: K ~ VVAlv7cOV7b`s MD, 6] մ ԕqJ X ቿ 3 > 甽 _4o ^ sӺ |?? # H9 "" " "" " "" " "" " "" " "" " " "" " "" " "" " "" " "" " "" " "" " ""

, . . ? , / ?

+4
2

, , , . , .

, , , URL- s3. , .

flysystem

  • flysystem , getMimetype, laravel.

Laravel

  • larvel- flysystem.

AWS Sdk PHP

  • , s3.

, s3

1. s3 config/filesystems.php. s3 , , .

return [

    'default' => 's3',

    'cloud' => 's3',

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root'   => storage_path().'/app',
        ],

        's3' => [
            'driver' => 's3',
            'key'    => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_REGION'),
            'bucket' => env('AWS_BUCKET'),
        ],

        'rackspace' => [
            'driver'    => 'rackspace',
            'username'  => 'your-username',
            'key'       => 'your-key',
            'container' => 'your-container',
            'endpoint'  => 'https://identity.api.rackspacecloud.com/v2.0/',
            'region'    => 'IAD',
        ],

    ],

];

2. : ResourceController@store

, - s3, . , "public" ( ), , . .

public function store(ResourceRequest $request)
{
    /* Store entry in DB */
    $resource = new Resource();
    $resource->title = $request->title;
    $resource->save();

    /* Prepare data needed for storage */
    $key = 'resource-'.$resource->id;
    $file = file_get_contents($request->file('resource'));
    $visibility = 'public';

    /* Store file */
    Storage::put($key, $file, $visibility);

    /* Success message */
    session()->flash('message', $request->title . ' uploaded!');
    return redirect()->route('resource-index');
}

3. : ResourceController@show

URL- aws s3, <img> <video>

public function show($id)
{
    /* Get DB instance */
    $resource = Resource::findOrFail($id);

    /* Set bucket */
    $bucket = env('AWS_BUCKET');

    /* Set file key */
    $key = 'resource-'.$resource->id;

    /* Build & push s3 url into array */
    $file['url']= 'https://s3.eu-central-1.amazonaws.com/'.$bucket.'/'.$key;

    /* Get & push mime type into array. */
    $file['type'] = Storage::getMimetype($key);

    return view('resource.show', compact('resource', 'file'));
}

4. , . mime, , .

@extends('layout.base')

@section('title') Show Resource @stop

@section('content')

    <h1>Show Resource</h1>

    @include('layout.partials.message')

    <h2>{{ $resource->title }}</h2>

    @if ($file['type'] == 'image/jpeg')
        <img src="{!! $file['url'] !!}" alt="">
    @else
        <video src="{!! $file['url'] !!}" controls></video>
    @endif

@stop

A cute hamster

s3 url: https://s3.eu-central-1.amazonaws.com/bucketname/resource-22

, URL- . , URL-, , .

, - . , d * mn url access...

+2

base64 img

 $file = Storage::get($filePath);

// , base64

$imgData = base64_encode($file);

// SRC: : {mime}; base64, {data};

$src = 'data: '.mime_content_type($img_file).';base64,'.$imgData;

//

echo '<img src="'.$src.'">';
0

All Articles