mercredi 2 août 2023

How to implement aws cloudfront in my laravel application?

Here is the code already used in my laravel application. This code directly get image url from my s3 storage. But I need to implement cloudfront and generate signed url for to get and download images.

foreach($fileInfos['files'] as $file) {
       $dlFileName = $file['name'];
       $fileDLUrls[] = FileIO::getUrl(FILE_ORIGINAL_DIRECTORY . '/'.$file['path'], $effectiveTime, ['ResponseContentType' => 
'application/octet-stream','ResponseContentDisposition' => 
'attachment; filename*=UTF-8\'\''. rawurlencode($dlFileName)]);
}
    public static function getUrl($fullPathName, $expires = '+1440 minutes', $headers = [])
    {
        if ($fullPathName == '') {
            return null;
        }

        switch (Storage::getDefaultDriver()) {
            case 's3':
                $disk = Storage::disk('s3');
                $headers = array_merge([
                    'Bucket' => env('AWS_S3_BUCKET'),
                    'Key' => $fullPathName
                ], $headers);
                $command = $disk->getDriver()
                    ->getAdapter()
                    ->getClient()
                    ->getCommand('getObject', $headers);
                $request = $disk->getDriver()
                    ->getAdapter()
                    ->getClient()
                    ->createPresignedRequest($command, $expires);
                return (string) $request->getUri();
            case 'local':
                return URL::to('/' . env('AWS_LOCAL_UPLOADDIR')) .
                         '/' . $fullPathName;
            default:
                return null;
        }
    }

I have changed above code as below,

public static function getUrl($fullPathName, $expires = '+1440 minutes', $headers = [])
    {
        if ($fullPathName == '') {
            return null;
        }


        switch (Storage::getDefaultDriver()) {
            case 's3':
                $resourceKey = env('CLOUDFRONT_URL').$fullPathName;
                $expired = strtotime($expires);
                $privateKey = env('AWS_PRIVATE_KEY');
                $keyPairId = env('AWS_KEY_PAIR_ID');
                
                $cloudFrontClient = new CloudFrontClient([
                    'profile' => 'default',
                    'version' => '2014-11-06',
                    'region' => env('AWS_REGION')
                ]);
                    $result = $cloudFrontClient->getSignedUrl([
                        'url' => $resourceKey,
                        'expires' => $expired,
                        'private_key' => $privateKey,
                        'key_pair_id' => $keyPairId,
                        
                    ]);
                    
                    return $result;
            case 'local':
                return URL::to('/' . env('AWS_LOCAL_UPLOADDIR')) .
                    '/' . $fullPathName;
            default:
                return null;
        }
    }

But I didn't use the headers in the first code. So that am not able to download the images. Can anyone help me to know about the reason or any solutions?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire