user3419778
user3419778

Reputation: 866

How To upload base64 image in S3 bucket in Laravel 5.5

I am working laravel api application and I have to upload a base64 encoded image to AWS S3 bucket.

I can directly upload an image by

$this->request->imageInput->store('public');//in local server
Storage::disk('s3')->put('FILENAME', file_get_contents('imageInput'));//in S3 server

How can I upload the base64 encoded image to AWS S3 bucket and also get the response where we can get Image Information?

Upvotes: 3

Views: 13667

Answers (5)

Ajeet
Ajeet

Reputation: 286

Upload base64 image in S3 using Laravel Filesystem

Make sure your project has the Flysystem S3 driver is installed:

composer require league/flysystem-aws-s3-v3 ~1.0

In config/filesystems.php

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

In .env file

AWS_ACCESS_KEY_ID=[YOURKEY]
AWS_SECRET_ACCESS_KEY=[YOURSECRETACCESSKEY]
AWS_DEFAULT_REGION=[YOURREGION]
AWS_BUCKET=[YOURBUKET]
AWS_URL=[AWSURL]

In your controller file

The storage facade is used to interact with our s3 disks configured. In the controller file, we need to add

use Illuminate\Support\Facades\Storage;

In your method

$image = $request->image;  // your base64 encoded
$data = explode( ',', $image );
$current_timestamp = Carbon::now()->timestamp;
$imageName = rand().'jpg';
$filenametostore='uploads/'. $imageName;
Storage::disk('s3')->put($filenametostore, base64_decode($data[1]), 'public');

Upvotes: 1

Nyein Chan Aung
Nyein Chan Aung

Reputation: 121

Upload base64 image in S3 bucket in Laravel 5.7

Make sure your project has the Flysystem S3 driver is installed:

composer require league/flysystem-aws-s3-v3

In config/filesystems.php

'spaces' => [
  'driver' => 's3',
  'key' => env('DO_SPACES_KEY'),
  'secret' => env('DO_SPACES_SECRET'),
  'endpoint' => env('DO_SPACES_ENDPOINT'),
  'region' => env('DO_SPACES_REGION'),
  'bucket' => env('DO_SPACES_BUCKET'),
 ],

In .env file

DO_SPACES_KEY=[YOURKEY]
DO_SPACES_SECRET=[YOURSECRET]
DO_SPACES_ENDPOINT=https://nyc3.digitaloceanspaces.com
DO_SPACES_REGION=nyc3
DO_SPACES_BUCKET=[YOURBUCKET]

In your controller file

$image = request('image');//base64 string
$file_path = 'product/'.str_random(30).time().'.jpg';
Storage::disk('spaces')->put($file_path, base64_decode($image), 'public');
return Storage::disk('spaces')->url($file_path);

Upvotes: 4

Prince Kumar Shivach
Prince Kumar Shivach

Reputation: 41

$base64String= "base64 string";

$image = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '',$base64String));

$imageName = str_random(30) . '.png';

$p = Storage::disk('s3')->put('' . $imageName, $image, 'public'); 

$image_url = Storage::disk()->url($imageName);

Upvotes: 4

Azole
Azole

Reputation: 286

list($baseType, $image) = explode(';', $base64);
list(, $image) = explode(',', $image);
$image = base64_decode($image);

$imageName = rand(111111111, 999999999) . '.png';
$p = Storage::disk('s3')->put('filepath/' . $imageName, $image, 'public'); // old : $file

The key point is that you need to give filename specifically.

The adapter can’t get the real path of base64 data. I guess it's because base64 data is not a file object.

Upvotes: 8

Vincent Floriot
Vincent Floriot

Reputation: 89

You need probably to decode this image using :

$tmp = base64_decode($base64);

And store it after on amazon s3

Upvotes: -2

Related Questions