'Choose Amazon S3 storage class using Laravel Filesystem

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.



Solution 1:[1]

To pass additional options to flysystem you have to use getDriver()

Storage::disk('s3')->getDriver()->put(
    'sample.txt',
    'This is a demo',
    [
        'StorageClass' => 'REDUCED_REDUNDANCY'
    ]
);

Solution 2:[2]

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',
        ]
    );

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adam
Solution 2
Solution 3 Kidd Tang