tekas's avatar
Level 1

Upload base64 image to S3

Hi, I use Aws php sdk. when i want to upload base64 image, it says that:

RuntimeException in functions.php line 299: Unable to open �PNG

IHDR�3�Ve7IDATx�� t�ǗB��5mxyM���$"�$�˘r+0G ����6�!

$� %!5I^^���}�k ��l�1���;6�c�%߶dY�%��vF��xf��........

My code is: $data = base64_decode($imageFile);

$this->s3Client->putObject(array( 'Bucket' => $bucketName, 'Key' => "{$imageName}.png", 'SourceFile' => $data, 'ContentType' => 'image/png', 'ACL' => 'public-read', ));

Thanks

0 likes
1 reply
leolam2005's avatar

This is how I use Lumen upload to S3 and btw I am using League\Flysystem\Filesystem.

S3

use Aws\S3\S3Client;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Filesystem;

class S3 {

    public static function boot() {

        $client = new S3Client([
            'credentials' => [
                'key'    => getenv('S3_KEY'),
                'secret' => getenv('S3_SECRET')
            ],
            'region' => getenv('S3_REGION'),
            'version' => 'latest',
        ]);

        $adapter = new AwsS3Adapter($client, getenv('S3_BUCKET'));
        $filesystem = new Filesystem($adapter);

        return $filesystem;

    }

}

The second argument must be encoded image string.

                $cloud = S3::boot();
                $cloud->put($photo->path, file_get_contents($this->file));
                $cloud->put($photo->thumbnail_path, (new Thumbnail)->minimize($this->file->getRealPath()));

Please or to participate in this conversation.