nzo/s3-aws-bundle

NzoS3AwsBundle 是一个用于处理 AWS S3 文件上传的 Symfony Bundle。

安装: 534

依赖者: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

v2.1.0 2021-12-06 08:27 UTC

This package is auto-updated.

Last update: 2024-09-06 14:09:05 UTC


README

Build Status Latest Stable Version

NzoS3AwsBundle 是一个用于处理 AWS S3 文件上传的 Symfony Bundle。

兼容 Symfony >= 4.4

安装

通过 Composer

$ composer require nzo/s3-aws-bundle

在 config/bundles.php 中注册该 Bundle(不使用 Flex)

// config/bundles.php

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

配置您的应用配置文件 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:

用法

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
        }
    }

许可证

此 Bundle 采用 MIT 许可证。在 Bundle 中查看完整的许可证。

查看 LICENSE