Choose Amazon S3 storage class using Laravel Filesystem

Viewed 1021

Amazon S3 has different storage classes, with different price brackets.

I was wondering if there's a way I can choose a storage class in Laravel's Filesystem / Cloud Storage solution?

It would be good to choose a class on a per upload basis so I can choose throughout my application, not just once in a configuration file.

3 Answers

This can be used in Laravel 7

Storage::disk('s3')->put(
    'file path',
    $request->file('file'),
    [
        'StorageClass' => 'STANDARD|REDUCED_REDUNDANCY|STANDARD_IA|ONEZONE_IA|INTELLIGENT_TIERING|GLACIER|DEEP_ARCHIVE',
    ]
);

You can use putFileAs() Method As Well Like Below

Storage::disk('s3')->putFileAs(
        'file path',
        $request->file('file'),
        'file name',
        [
            'StorageClass' => 'STANDARD|REDUCED_REDUNDANCY|STANDARD_IA|ONEZONE_IA|INTELLIGENT_TIERING|GLACIER|DEEP_ARCHIVE',
        ]
    );

I can't really find this answer on the internet. Hope it helps someone else. If you want to set StorageClass on the disk level (once for every upload).

You can change it on the config\filesystems.php

's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
            'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
            'throw' => false,
            'options' => [
                'StorageClass' => 'INTELLIGENT_TIERING'
            ]
        ],

Other possible options...

        'ACL',
        'CacheControl',
        'ContentDisposition',
        'ContentEncoding',
        'ContentLength',
        'ContentType',
        'Expires',
        'GrantFullControl',
        'GrantRead',
        'GrantReadACP',
        'GrantWriteACP',
        'Metadata',
        'RequestPayer',
        'SSECustomerAlgorithm',
        'SSECustomerKey',
        'SSECustomerKeyMD5',
        'SSEKMSKeyId',
        'ServerSideEncryption',
        'StorageClass',
        'Tagging',
        'WebsiteRedirectLocation',

Ref: thephpleague/flysystem-aws-s3-v3

Related