HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: /var/www/admin.javaapp.co.uk/app/Helpers/Aws.php
<?php

namespace App\Helpers;

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
use Aws\Sdk;
use Exception;

class Aws
{
    /**
     * Upload any file to aws s3 bucket
     *
     * @param  string  $filePath (like: example/example)
     * @param  file  $setImage Image you want to store
     * @param  string  $getImage optional Image uri from database
     * @return  string  uploaded file path
     */
    public static function uploadImageS3Bucket($filePath, $setImage, $getImage = '')
    {
        try {
            if ($getImage != '') {
                self::deleteImageS3Bucket($getImage);
            }
            $path = Storage::disk('s3')->put($filePath, $setImage, 'public');
            // $path = Storage::disk('s3')->put($filePath, $setImage, 'private');
            return Storage::disk('s3')->url($path);
        } catch (\Throwable $th) {
            throw new Exception($th);
        }
    }

    /**
     * delete any file from aws s3 bucket
     *
     * @param  string  $getImage
     */
    public static function deleteImageS3Bucket($getImage)
    {
        try {
            $uploadedImage = str_replace(env('AWS_URL') . '/', '', $getImage);
            // $uploadedImage = str_replace(config('filesystems.disks.s3.url') . '/', '', $getImage);
            if ($uploadedImage != '') {
                if (Storage::disk('s3')->exists($uploadedImage)) {
                    Storage::disk('s3')->delete($uploadedImage);
                }
            }
        } catch (\Throwable $th) {
            throw new Exception($th);
        }
    }

    /**
     * get presigned uri of any file from aws s3 bucket
     *
     * @param  string  $getImage
     * @return  string  ( presignedUri of file )
     */
    public static function presignedUri($getImage)
    {
        try {
            $s3 = Storage::disk('s3');
            $uploadedImage = str_replace(env('AWS_URL') . '/', '', $getImage);
            // $uploadedImage = str_replace(config('filesystems.disks.s3.url').'/', '', $getImage);
            if ($s3->exists($uploadedImage)) {
                // return $s3->temporaryUrl($uploadedImage, Carbon::now()->addMinutes(20)); // to get temporary Url
                $region = env('AWS_DEFAULT_REGION');
                // $region = config('filesystems.disks.s3.region');
                // 'version' => '2006-03-01',
                $keyId = env('AWS_ACCESS_KEY_ID');
                $secretKey = env('AWS_SECRET_ACCESS_KEY');
                $sdk = new Sdk([
                    'region' => $region,
                    'version' => 'latest',
                    'credentials' => [
                        'key'    => $keyId,
                        'secret' => $secretKey,
                    ],
                ]);
                $client = $sdk->createS3();

                $bucket = env('AWS_BUCKET');
                // $bucket = config('filesystems.disks.s3.bucket');
                $command = $client->getCommand('GetObject', [
                    'Bucket' => $bucket,
                    'Key' => $uploadedImage,
                ]);

                $request = $client->createPresignedRequest($command, '+10 minutes');

                return (string) $request->getUri();
            }

            return $getImage;
        } catch (\Throwable $th) {
            return asset('assets/img/400x300.jpg');
        }
    }
}