/NzoS3AwsBundle

The NzoS3AwsBundle is a Symfony Bundle used to handle AWS S3 Files Upload.

Primary LanguagePHPMIT LicenseMIT

NzoS3AwsBundle

Build Status Latest Stable Version

The NzoS3AwsBundle is a Symfony Bundle used to handle AWS S3 Files Upload.

Compatible with Symfony >= 4.4

Installation

Through Composer:

$ composer require nzo/s3-aws-bundle

Register the bundle in config/bundles.php (without Flex):

// config/bundles.php

return [
    // ...
    Nzo\S3AwsBundle\NzoS3AwsBundle::class => ['all' => true],
];

Configure your application's config.yml:

# config/packages/nzo_s3_aws.yaml

nzo_s3_aws:
    aws_access_key: '%env(AWS_ACCESS_KEY)%'  required: 
    aws_secret:     '%env(AWS_SECRET)%'      required: 
    aws_region:     '%env(AWS_REGION)%'      required:  (example: eu-west-1)
    aws_bucket:     '%env(AWS_BUCKET)%'      required:  (the bucket name)
    aws_endpoint:   '%env(AWS_ENDPOINT)%'    optional:

Usage

use Nzo\S3AwsBundle\S3\S3AwsHandler;
    
    private $s3AwsHandler;

    public function __construct(S3AwsHandler $s3AwsHandler)
    {
        $this->s3AwsHandler = $s3AwsHandler;
        
        // without autowiring use: $this->get('nzo_s3_aws')
    }

    public function uploadFilesToS3Aws(string $key, string $filePath)
    {
        try {

            // $key: it represent the file name and path in the S3 Bucket. Example: "my_folder/my-image.jpg"
            // $filePath: is the absolute path to the file. Example: "/var/www/my-image.jpg"

            $awsResult = $this->s3AwsHandler->uploadFile($key, $filePath);


            // Public ACL (default 'private')
            $acl = 'public-read';
            $awsResult = $this->s3AwsHandler->uploadFile($key, $filePath, $acl);

        } catch (\Exception $e) {
            // Unable to upload the file to AWS S3
        }
    }

License

This bundle is under the MIT license. See the complete license in the bundle:

See LICENSE