PHP code example of fypyuhu / l5-gcs

1. Go to this page and download the library: Download fypyuhu/l5-gcs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fypyuhu / l5-gcs example snippets


'providers' => [
    // ...
    fypyuhu\GCSProvider\GoogleCloudStorageServiceProvider::class,
]

'gcs' => [
    'driver' => 'gcs',
    'projectId' => env('GCS_PROJECT_ID'),
    'bucket' => env('GCS_BUCKET'),
    'keyFilePath' => storage_path(env('GCS_KEY_FILE')),
    'url' => env('GCS_CUSTOM_URL'), // optional: for custom url only
],

$disk = Storage::disk('gcs');

// Put a private file on the 'gcs' disk which is a Google Cloud Storage bucket
$disk->put('test.png', file_get_contents(storage_path('app/test.png')));

// Put a public-accessible file on the 'gcs' disk which is a Google Cloud Storage bucket
$disk->put(
    'test-public.png',
    file_get_contents(storage_path('app/test-public.png')),
    \Illuminate\Contracts\Filesystem\Filesystem::VISIBILITY_PUBLIC
);

// Put a public-accessible file with metadata on the 'gcs' disk which is a Google Cloud Storage bucket 
$disk->put(
    'test.png',
    file_get_contents(storage_path('app/test.png')),
    [
        'metadata' => [
            'contentDisposition' => 'attachment; filename=file.png',
            'contentType' => 'image/png',
        ],
        'visibility' => \Illuminate\Contracts\Filesystem\Filesystem::VISIBILITY_PUBLIC
    ]
);

// Retrieve a file
$file = $disk->get('test.png');

// Get a temporary url for 1 hour
$disk->temporaryUrl('test.png', time() + 3600);

// Get a temporary url for 1 hour with other options
// see other option: https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.45.1/storage/storageobject?method=signedUrl
$disk->temporaryUrl('test.pdf', time() + 3600, ['saveAsName' => 'file.png']);