PHP code example of spatie / flysystem-google-cloud-storage

1. Go to this page and download the library: Download spatie/flysystem-google-cloud-storage 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/ */

    

spatie / flysystem-google-cloud-storage example snippets


use Google\Cloud\Storage\StorageClient;
use League\Flysystem\Filesystem;
use Spatie\GoogleCloudStorageAdapter\GoogleCloudStorageAdapter;

$storageClient = new StorageClient([
    'projectId' => 'your-project-id',
    // The credentials can manually be specified by passing in a keyFilePath.
    // 'keyFilePath' => '/path/to/service-account.json',
]);

$bucket = $storageClient->bucket('your-bucket-name');

$adapter = new GoogleCloudStorageAdapter($storageClient, $bucket);

$filesystem = new Filesystem($adapter);

$filesystem->write('path/to/file.txt', 'contents');
$filesystem->update('path/to/file.txt', 'new contents');
$contents = $filesystem->read('path/to/file.txt');
$exists = $filesystem->has('path/to/file.txt');
$filesystem->delete('path/to/file.txt');
$filesystem->rename('filename.txt', 'newname.txt');
$filesystem->copy('filename.txt', 'duplicate.txt');
$filesystem->deleteDir('path/to/directory');

use Google\Cloud\Storage\StorageClient;
use League\Flysystem\Filesystem;
use Spatie\GoogleCloudStorageAdapter\GoogleCloudStorageAdapter;

$storageClient = new StorageClient([
    'projectId' => 'your-project-id',
]);
$bucket = $storageClient->bucket('your-bucket-name');
$adapter = new GoogleCloudStorageAdapter($storageClient, $bucket);

// URI defaults to "https://storage.googleapis.com":
$filesystem = new Filesystem($adapter);
$filesystem->getUrl('path/to/file.txt');
// "https://storage.googleapis.com/your-bucket-name/path/to/file.txt"

// Using custom storage uri:
$adapter->setStorageApiUri('http://example.com');
$filesystem = new Filesystem($adapter);
$filesystem->getUrl('path/to/file.txt');
// "http://example.com/path/to/file.txt"

// You can also prefix the file path if needed:
$adapter->setStorageApiUri('http://example.com');
$adapter->setPathPrefix('extra-folder/another-folder/');
$filesystem = new Filesystem($adapter);
$filesystem->getUrl('path/to/file.txt');
// "http://example.com/extra-folder/another-folder/path/to/file.txt"