Giter Site home page Giter Site logo

yii2-aws-s3's Introduction

Yii2 AWS S3

An Amazon S3Client wrapper as Yii2 component.

Yii2 AWS S3 can only work with one bucket per a component configuration.

The component currently supports CloudFront (getting a CDN url for an object in a S3 bucket).

License Latest Stable Version Total Downloads Latest Unstable Version

Installation

  1. Run the Composer command to install the latest stable version:

    composer require frostealth/yii2-aws-s3
    
  2. Add component to config/main.php

    'components' => [
        // ...
        's3bucket' => [
            'class' => \frostealth\yii2\aws\s3\Storage::className(),
            'region' => 'your region',
            'credentials' => [ // Aws\Credentials\CredentialsInterface|array|callable
                'key' => 'your aws s3 key',
                'secret' => 'your aws s3 secret',
            ],
            'bucket' => 'your aws s3 bucket',
            'cdnHostname' => 'http://example.cloudfront.net',
            'defaultAcl' => \frostealth\yii2\aws\s3\Storage::ACL_PUBLIC_READ,
            'debug' => true, // bool|array
        ],
        // ...
    ],

You can also use it with the dependency container:

Yii::$app->container->set('frostealth\yii2\aws\s3\StorageInterface', function () {
    return Yii::$app->get('s3bucket');
});

Usage

Uploading objects

// creating an object
$data = ['one', 'two', 'three'];
Yii::$app->get('s3bucket')->put('path/to/s3object.ext', Json::encode($data));

// uploading an object by streaming the contents of a stream
$resource = fopen('/path/to/local/file.ext', 'r+');
Yii::$app->get('s3bucket')->put('path/to/s3object.ext', $resource);

Uploading files

Yii::$app->get('s3bucket')->upload('path/to/s3object.ext', '/path/to/local/file.ext');

Uploading large files using asynchronous multipart uploads with custom options

$concurrency = 5;
$minPartSize = 536870912; // 512 MB

$promise = Yii::$app->get('s3bucket')->uploadAsync(
    'path/to/s3object.ext',
    '/path/to/local/file.ext',
    $concurrency,
    $minPartSize
);

// block until the result is ready
$promise->wait();

Reading objects

/** @var \Aws\Result $result */
$result = Yii::$app->get('s3bucket')->get('path/to/s3object.ext');
$data = $result['Body'];

Saving objects to a file

Yii::$app->get('s3bucket')->get('path/to/s3object.ext', '/path/to/local/file.ext');

Deleting objects

Yii::$app->get('s3bucket')->delete('path/to/s3object.ext');

Getting a plain URL

$url = Yii::$app->get('s3bucket')->getUrl('path/to/s3object.ext');

Creating a pre-signed URL

$url = Yii::$app->get('s3bucket')->getPresignedUrl('path/to/s3object.ext', '+10 minutes');

Getting a CDN URL

$url = Yii::$app->get('s3bucket')->getCdnUrl('path/to/s3object.ext');

Listing objects

$result = Yii::$app->get('s3bucket')->getList('path/');
foreach ($result['Contents'] as $object) {
    echo $object['Key'] . PHP_EOL;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.