Add Metadata, headers (Expires, CacheControl) to a file uploaded to Amazon S3 using the Laravel 5.0 Storage facade



PHP Snippet 1:

Storage::disk('s3')->getDriver()->put('/index4.txt', 'test', [ 'visibility' => 'public', 'Expires' => 'Expires, Fri, 30 Oct 1998 14:19:41 GMT']);

PHP Snippet 2:

s3' => [
    'driver' => 's3',
    'key' => env('AWS_KEY'),
    'secret' => env('AWS_SECRET'),
    'region' => env('AWS_REGION'),
    'bucket' => env('AWS_BUCKET'),
    'options' => ['CacheControl' => 'max-age=315360000, no-transform, public', 
                  'ContentEncoding' => 'gzip']
],

PHP Snippet 3:

Storage::put(
    'image.jpg',
    file_get_contents($path),
    [
        'visibility' => 'public',
        'Metadata' => [
            'thumb' => '320-180',
        ],
    ]
);

PHP Snippet 4:

'options'     => [
    'Expires' => gmdate('D, d M Y H:i:s GMT', strtotime('+1 month')),
    >>> WRONG visibility' => 'public', WRONG <<<
]

PHP Snippet 5:

'visibility'  => 'public',
'options'     => ['Expires' => gmdate('D, d M Y H:i:s GMT', strtotime('+1 month'))]

PHP Snippet 6:

Storage::put($directory . '/' . $imageName, 
            $image, [
              'visibility' => 'public',
              'Expires' => gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60 * 24 * 7)),
              'CacheControl' => 'max-age=315360000, no-transform, public',
        ]);

PHP Snippet 7:

Storage::disk('s3')->put('/index.txt', 'file content', [
    // S3 Object ACL
    'visibility' => 'public', // or 'private',

    // HTTP Headers
    'CacheControl' => 'public,max-age=315360000',
    'ContentDisposition' => 'attachment; filename="index.txt"',
    'Expires' => 'Thu, 12 Feb 2032 08:24:43 GMT',

    // Metadata or other S3 options
    'MetadataDirective' => 'REPLACE'
    'Metadata' => [
        'Custom-Key' => 'test',
    ],
])

PHP Snippet 8:

public const AVAILABLE_OPTIONS = [
    'ACL',
    'CacheControl',
    'ContentDisposition',
    'ContentEncoding',
    'ContentLength',
    'ContentType',
    'Expires',
    'GrantFullControl',
    'GrantRead',
    'GrantReadACP',
    'GrantWriteACP',
    'Metadata',
    'MetadataDirective',
    'RequestPayer',
    'SSECustomerAlgorithm',
    'SSECustomerKey',
    'SSECustomerKeyMD5',
    'SSEKMSKeyId',
    'ServerSideEncryption',
    'StorageClass',
    'Tagging',
    'WebsiteRedirectLocation',
];

PHP Snippet 9:

<?php namespace App\Providers;
use Storage;
use League\Flysystem\Filesystem;
use Aws\S3\S3Client;
use League\Flysystem\AwsS3v2\AwsS3Adapter as S3Adapter;
use Illuminate\Support\ServiceProvider;

class CustomS3Filesystem extends ServiceProvider {

public function boot()
{
    Storage::extend('s3_custom', function($app, $config)
    {
        $s3Config = array_only($config, ['key', 'region', 'secret', 'signature', 'base_url']);
        $flysystemConfig = ['mimetype' => 'text/xml'];
        $metadata['cache_control']='max-age=0, no-cache, no-store, must-revalidate';
        return new Filesystem(new S3Adapter(S3Client::factory($s3Config), $config['bucket'], null, ['mimetype' => 'text/xml', 'Metadata' => $metadata]), $flysystemConfig);
    });
}
public function register()
{
    //
}
}

PHP Snippet 10:

'App\Providers\CustomS3Filesystem',

PHP Snippet 11:

's3-new' => [
            'driver' => 's3_custom',
            'key'    => 'XXX',
            'secret' => 'XXX',
            'bucket' => 'XXX',
        ],

PHP Snippet 12:

Storage::disk('s3-new')->put(filename, file_get_contents($file), public);

PHP Snippet 13:

's3static' => [
            'driver'     => 'awss3',
            'key'        => 'my-key',
            'secret'     => 'my-secret',
            'bucket'     => 'my-bucket',
            // 'region'     => 'your-region',
            // 'base_url'   => 'your-url',
            'options'    => array(
                'CacheControl' => 'max_age=2592000'
            ),
            // 'prefix'     => 'your-prefix',
            // 'visibility' => 'public',
            // 'eventable'  => true,
            // 'cache'      => 'foo'
        ],

PHP Snippet 14:

namespace App\Providers;

use Illuminate\Support\Arr;
use Storage;
use League\Flysystem\Filesystem;
use Aws\S3\S3Client;
use League\Flysystem\AwsS3v3\AwsS3Adapter as S3Adapter;
use Illuminate\Support\ServiceProvider;

class CustomS3Filesystem extends ServiceProvider
{

    /**
     * Format the given S3 configuration with the default options.
     *
     * @param  array  $config
     * @return array
     */
    protected function formatS3Config(array $config)
    {
        $config += ['version' => 'latest'];

        if ($config['key'] && $config['secret']) {
            $config['credentials'] = Arr::only($config, ['key', 'secret']);
        }

        return $config;
    }

    /**
     * Bootstrap a custom filesystem
     *
     * @return void
     */
    public function boot()
    {
        Storage::extend('s3_custom', function($app, $config)
        {
            $s3Config = $this->formatS3Config($config);
            return new Filesystem(
                new S3Adapter(
                    new S3Client($s3Config),
                    $config['bucket'],
                    null,
                    [
                        'CacheControl'  => 'max-age=432000'
                    ]
                )
            );
        });
    }

    public function register()
    {
        //
    }
}

PHP Snippet 15:

            $fileID = $fileData['FileId'];
            $fileExt = $fileData['FileExtension'];
            $fileUnique = $fileData['UniqueFileId'];
            $isImage = $fileData['IsImage'];
            $isDefault = $fileData['IsDefaultImage'];
            $filePath = $fileUnique . "." . $fileExt;
            $file = $mp->fileID($fileID)->get();
            
            if (Storage::disk('s3')->missing('img/' . $filePath)) {
                Storage::disk('s3')->put(
                    'img/' . $filePath, 
                    $file,
                    [
                        // Metadata or other S3 options
                        'MetadataDirective' => 'REPLACE',
                        'Metadata' => [
                            'is-image' => strval($isImage),
                            'is-default' => strval($isDefault),
                            'unique-file-id' => strval($fileUnique),
                            'file-extension' => strval($fileExt),
                        ]
                    ]
                );
                    echo nl2br('uploading file: ' . $filePath . "\n");
            } else {
                echo nl2br('file already exists:' . $filePath . "\n");
            }