PHP code example of andreinocenti / laravel-file-s3-like

1. Go to this page and download the library: Download andreinocenti/laravel-file-s3-like 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/ */

    

andreinocenti / laravel-file-s3-like example snippets


'spaces-disk' => [
    'driver' => 's3',
    'key' => env('SPACES_ACCESS_KEY_ID'),
    'secret' => env('SPACES_SECRET_ACCESS_KEY'),
    'region' => env('SPACES_DEFAULT_REGION'),
    'bucket' => env('SPACES_BUCKET'),
    'url' => env('SPACES_URL'),
    'endpoint' => env('SPACES_ENDPOINT'),
    'folder' => env('SPACES_FOLDER'), // This will be the default directory used. It can be empty, if so the default directory will be the bucket root
    'cdn_endpoint' => env('SPACES_CDN_ENDPOINT'), // at Digital Ocean Spaces the CDN is auto set when a file is uploaded. So set here the cdn_endpoint (edge)
    'use_path_style_endpoint' => env('SPACES_USE_PATH_STYLE_ENDPOINT', false),
    'throw' => false,
],

'gcs' => [
    'driver' => 'gcs',
    'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'),
    'key_file_path' => env('GOOGLE_APPLICATION_CREDENTIALS'), // Path to service account json file
    'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket'),
    'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null), // Optional: prefix for all files
    'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // Optional: custom API URI
    'api_endpoint' => env('GOOGLE_CLOUD_STORAGE_API_ENDPOINT', null), // Optional: custom API endpoint
    'visibility_handler' => \League\Flysystem\GoogleCloudStorage\UniformBucketLevelAccessVisibility::class, // for uniform bucket level access
    'throw' => false,
],

use AndreInocenti\LaravelFileS3Like\Facades\FileS3LikeSpaces;

FileS3LikeSpaces::disk('spaces-disk')
    ->directory('images')
    ->upload($file, 'new-test');

use AndreInocenti\LaravelFileS3Like\Facades\FileS3Like;

FileS3Like::repository('spaces')
    ->disk('spaces-disk')
    ->directory('images')
    ->upload($file, 'new-test');

use AndreInocenti\LaravelFileS3Like\Facades\FileS3Like;

FileS3Like::repository('gcs')
    ->disk('gcs') // The disk name configured in config/filesystems.php
    ->directory('uploads')
    ->upload($file, 'profile-picture');

use AndreInocenti\LaravelFileS3Like\Facades\FileS3Like;

$stream = fopen(storage_path('app/large-video.mp4'), 'r');

$diskFile = FileS3Like::repository('spaces') // or 'gcs'
    ->disk('spaces')
    ->directory('videos')
    ->uploadStream($stream, 'large-video.mp4', 'video/mp4');

fclose($stream);

use AndreInocenti\LaravelFileS3Like\Facades\FileS3LikeSpaces;

$stream = fopen(storage_path('app/banner.png'), 'r');

$diskFile = FileS3LikeSpaces::disk('spaces-disk')
    ->directory('images')
    ->saveStream($stream, 'banner.png', 'image/png');

fclose($stream);

use AndreInocenti\LaravelFileS3Like\Facades\FileS3Like;

$upload = FileS3Like::repository('gcs') // or 'spaces'
    ->disk('gcs')
    ->directory('uploads')
    ->presignedUrl(
        filepath: 'avatars',         // directory inside the configured folder
        filename: 'profile.png',     // optional; defaults to UUID
        expiration: 600,             // seconds
        fileType: 'image/png',       // mime or extension; sets Content-Type
        public: true,                // optional; false keeps it private
    );

return response()->json($upload);