Introduction
Laravel Hyperf provides a powerful filesystem abstraction thanks to the wonderful Flysystem PHP package by Frank de Jonge. The Laravel Hyperf Flysystem integration provides simple drivers for working with local filesystems, SFTP, Amazon S3 and Google Cloud Storage. Even better, it's amazingly simple to switch between these storage options between your local development machine and production server as the API remains the same for each system.
Configuration
Laravel Hyperf's filesystem configuration file is located at config/filesystems.php
. Within this file, you may configure all of your filesystem "disks". Each disk represents a particular storage driver and storage location. Example configurations for each supported driver are included in the configuration file so you can modify the configuration to reflect your storage preferences and credentials.
The local
driver interacts with files stored locally on the server running the Laravel Hyperf application while the s3
driver is used to write to Amazon's S3 cloud storage service.
Note
You may configure as many disks as you like and may even have multiple disks that use the same driver.
The Local Driver
When using the local
driver, all file operations are relative to the root
directory defined in your filesystems
configuration file. By default, this value is set to the storage/app
directory. Therefore, the following method would write to storage/app/example.txt
:
use SwooleTW\Hyperf\Support\Facades\Storage;
Storage::disk('local')->put('example.txt', 'Contents');
Driver Pool
Laravel Hyperf provides an elegant way to create object pools for your disk drivers. Object pools can keep your drivers safe in coroutines and help you control the concurrency number for each disk.
s3
and gcs
drivers are configured as object pools by default. You can customize the pool config for your disks in the filesystems.php
config:
's3' => [
'driver' => 's3',
// ...
'pool' => [
'min_objects' => 1,
'max_objects' => 10,
'wait_timeout' => 3.0,
'max_lifetime' => 60.0,
],
],
You will get a RuntimeException
if your driver instances exceeds the max_objects
in the config while handling concurrent requests. You should configure these values according to your needs.
Note
Pool config is not supported in ftp
, sftp
and local
drivers. They are coroutine-safe with singletons.
Driver Prerequisites
S3 Driver Configuration
Before using the S3 driver, you will need to install the Flysystem S3 package via the Composer package manager:
composer require league/flysystem-aws-s3-v3 "^3.0" --with-all-dependencies
The S3 driver configuration information is located in your config/filesystems.php
configuration file. This file contains an example configuration array for an S3 driver. You are free to modify this array with your own S3 configuration and credentials. For convenience, these environment variables match the naming convention used by the AWS CLI.
FTP Driver Configuration
Before using the FTP driver, you will need to install the Flysystem FTP package via the Composer package manager:
composer require league/flysystem-ftp "^3.0"
Laravel Hyperf 's Flysystem integrations work great with FTP; however, a sample configuration is not included with the framework's default filesystems.php
configuration file. If you need to configure an FTP filesystem, you may use the configuration example below:
'ftp' => [
'driver' => 'ftp',
'host' => env('FTP_HOST'),
'username' => env('FTP_USERNAME'),
'password' => env('FTP_PASSWORD'),
// Optional FTP Settings...
// 'port' => env('FTP_PORT', 21),
// 'root' => env('FTP_ROOT'),
// 'passive' => true,
// 'ssl' => true,
// 'timeout' => 30,
],
SFTP Driver Configuration
Before using the SFTP driver, you will need to install the Flysystem SFTP package via the Composer package manager:
composer require league/flysystem-sftp-v3 "^3.0"
Laravel Hyperf's Flysystem integrations work great with SFTP; however, a sample configuration is not included with the framework's default filesystems.php
configuration file. If you need to configure an SFTP filesystem, you may use the configuration example below:
'sftp' => [
'driver' => 'sftp',
'host' => env('SFTP_HOST'),
// Settings for basic authentication...
'username' => env('SFTP_USERNAME'),
'password' => env('SFTP_PASSWORD'),
// Settings for SSH key based authentication with encryption password...
'privateKey' => env('SFTP_PRIVATE_KEY'),
'passphrase' => env('SFTP_PASSPHRASE'),
// Settings for file / directory permissions...
'visibility' => 'private', // `private` = 0600, `public` = 0644
'directory_visibility' => 'private', // `private` = 0700, `public` = 0755
// Optional SFTP Settings...
// 'hostFingerprint' => env('SFTP_HOST_FINGERPRINT'),
// 'maxTries' => 4,
// 'passphrase' => env('SFTP_PASSPHRASE'),
// 'port' => env('SFTP_PORT', 22),
// 'root' => env('SFTP_ROOT', ''),
// 'timeout' => 30,
// 'useAgent' => true,
],
Scoped and Read-Only Filesystems
Scoped disks allow you to define a filesystem where all paths are automatically prefixed with a given path prefix. Before creating a scoped filesystem disk, you will need to install an additional Flysystem package via the Composer package manager:
composer require league/flysystem-path-prefixing "^3.0"
You may create a path scoped instance of any existing filesystem disk by defining a disk that utilizes the scoped
driver. For example, you may create a disk which scopes your existing s3
disk to a specific path prefix, and then every file operation using your scoped disk will utilize the specified prefix:
's3-videos' => [
'driver' => 'scoped',
'disk' => 's3',
'prefix' => 'path/to/videos',
],
"Read-only" disks allow you to create filesystem disks that do not allow write operations. Before using the read-only
configuration option, you will need to install an additional Flysystem package via the Composer package manager:
composer require league/flysystem-read-only "^3.0"
Next, you may include the read-only
configuration option in one or more of your disk's configuration arrays:
's3-videos' => [
'driver' => 's3',
// ...
'read-only' => true,
],
Amazon S3 Compatible Filesystems
By default, your application's filesystems
configuration file contains a disk configuration for the s3
disk. In addition to using this disk to interact with Amazon S3, you may use it to interact with any S3 compatible file storage service such as MinIO or DigitalOcean Spaces.
Typically, after updating the disk's credentials to match the credentials of the service you are planning to use, you only need to update the value of the endpoint
configuration option. This option's value is typically defined via the AWS_ENDPOINT
environment variable:
'endpoint' => env('AWS_ENDPOINT', 'https://minio:9000'),
MinIO
In order for Laravel Hyperf's Flysystem integration to generate proper URLs when using MinIO, you should define the AWS_URL
environment variable so that it matches your application's local URL and includes the bucket name in the URL path:
AWS_URL=http://localhost:9000/local
Warning
Generating temporary storage URLs via the temporaryUrl
method is not supported when using MinIO.
Obtaining Disk Instances
The Storage
facade may be used to interact with any of your configured disks. For example, you may use the put
method on the facade to store an avatar on the default disk. If you call methods on the Storage
facade without first calling the disk
method, the method will automatically be passed to the default disk:
use SwooleTW\Hyperf\Support\Facades\Storage;
Storage::put('avatars/1', $content);
If your application interacts with multiple disks, you may use the disk
method on the Storage
facade to work with files on a particular disk:
Storage::disk('s3')->put('avatars/1', $content);
On-Demand Disks
Sometimes you may wish to create a disk at runtime using a given configuration without that configuration actually being present in your application's filesystems
configuration file. To accomplish this, you may pass a configuration array to the Storage
facade's build
method:
use SwooleTW\Hyperf\Support\Facades\Storage;
$disk = Storage::build([
'driver' => 'local',
'root' => '/path/to/root',
]);
$disk->put('image.jpg', $content);
Retrieving Files
The get
method may be used to retrieve the contents of a file. The raw string contents of the file will be returned by the method. Remember, all file paths should be specified relative to the disk's "root" location:
$contents = Storage::get('file.jpg');
If the file you are retrieving contains JSON, you may use the json
method to retrieve the file and decode its contents:
$orders = Storage::json('orders.json');
The exists
method may be used to determine if a file exists on the disk:
if (Storage::disk('s3')->exists('file.jpg')) {
// ...
}
The missing
method may be used to determine if a file is missing from the disk:
if (Storage::disk('s3')->missing('file.jpg')) {
// ...
}
Downloading Files
The download
method may be used to generate a response that forces the user's browser to download the file at the given path. The download
method accepts a filename as the second argument to the method, which will determine the filename that is seen by the user downloading the file. Finally, you may pass an array of HTTP headers as the third argument to the method:
return Storage::download('file.jpg');
return Storage::download('file.jpg', $name, $headers);
File URLs
You may use the url
method to get the URL for a given file. If you are using the local
driver, this will typically just prepend /storage
to the given path and return a relative URL to the file. If you are using the s3
driver, the fully qualified remote URL will be returned:
use SwooleTW\Hyperf\Support\Facades\Storage;
$url = Storage::url('file.jpg');
When using the local
driver, all files that should be publicly accessible should be placed in the storage/app/public
directory. Furthermore, you should create a symbolic link at public/storage
which points to the storage/app/public
directory.
Warning
When using the local
driver, the return value of url
is not URL encoded. For this reason, we recommend always storing your files using names that will create valid URLs.
URL Host Customization
If you would like to pre-define the host for URLs generated using the Storage
facade, you may add a url
option to the disk's configuration array:
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
Temporary URLs
Using the temporaryUrl
method, you may create temporary URLs to files stored using the s3
driver. This method accepts a path and a DateTime
instance specifying when the URL should expire:
use SwooleTW\Hyperf\Support\Facades\Storage;
$url = Storage::temporaryUrl(
'file.jpg', now()->addMinutes(5)
);
If you need to specify additional S3 request parameters, you may pass the array of request parameters as the third argument to the temporaryUrl
method:
$url = Storage::temporaryUrl(
'file.jpg',
now()->addMinutes(5),
[
'ResponseContentType' => 'application/octet-stream',
'ResponseContentDisposition' => 'attachment; filename=file2.jpg',
]
);
If you need to customize how temporary URLs are created for a specific storage disk, you can use the buildTemporaryUrlsUsing
method. For example, this can be useful if you have a controller that allows you to download files stored via a disk that doesn't typically support temporary URLs. Usually, this method should be called from the boot
method of a service provider:
<?php
namespace App\Providers;
use DateTime;
use SwooleTW\Hyperf\Support\Facades\Storage;
use SwooleTW\Hyperf\Support\Facades\URL;
use SwooleTW\Hyperf\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Storage::disk('local')->buildTemporaryUrlsUsing(
function (string $path, DateTime $expiration, array $options) {
return URL::temporarySignedRoute(
'files.download',
$expiration,
array_merge($options, ['path' => $path])
);
}
);
}
}
Temporary Upload URLs
Warning
The ability to generate temporary upload URLs is only supported by the s3
driver.
If you need to generate a temporary URL that can be used to upload a file directly from your client-side application, you may use the temporaryUploadUrl
method. This method accepts a path and a DateTime
instance specifying when the URL should expire. The temporaryUploadUrl
method returns an associative array which may be destructured into the upload URL and the headers that should be included with the upload request:
use SwooleTW\Hyperf\Support\Facades\Storage;
['url' => $url, 'headers' => $headers] = Storage::temporaryUploadUrl(
'file.jpg', now()->addMinutes(5)
);
This method is primarily useful in serverless environments that require the client-side application to directly upload files to a cloud storage system such as Amazon S3.
File Metadata
In addition to reading and writing files, Laravel Hyperf can also provide information about the files themselves. For example, the size
method may be used to get the size of a file in bytes:
use SwooleTW\Hyperf\Support\Facades\Storage;
$size = Storage::size('file.jpg');
The lastModified
method returns the UNIX timestamp of the last time the file was modified:
$time = Storage::lastModified('file.jpg');
The MIME type of a given file may be obtained via the mimeType
method:
$mime = Storage::mimeType('file.jpg');
File Paths
You may use the path
method to get the path for a given file. If you are using the local
driver, this will return the absolute path to the file. If you are using the s3
driver, this method will return the relative path to the file in the S3 bucket:
use SwooleTW\Hyperf\Support\Facades\Storage;
$path = Storage::path('file.jpg');
Storing Files
The put
method may be used to store file contents on a disk. You may also pass a PHP resource
to the put
method, which will use Flysystem's underlying stream support. Remember, all file paths should be specified relative to the "root" location configured for the disk:
use SwooleTW\Hyperf\Support\Facades\Storage;
Storage::put('file.jpg', $contents);
Storage::put('file.jpg', $resource);
Failed Writes
If the put
method (or other "write" operations) is unable to write the file to disk, false
will be returned:
if (! Storage::put('file.jpg', $contents)) {
// The file could not be written to disk...
}
If you wish, you may define the throw
option within your filesystem disk's configuration array. When this option is defined as true
, "write" methods such as put
will throw an instance of League\Flysystem\UnableToWriteFile
when write operations fail:
'public' => [
'driver' => 'local',
// ...
'throw' => true,
],
Prepending and Appending To Files
The prepend
and append
methods allow you to write to the beginning or end of a file:
Storage::prepend('file.log', 'Prepended Text');
Storage::append('file.log', 'Appended Text');
Copying and Moving Files
The copy
method may be used to copy an existing file to a new location on the disk, while the move
method may be used to rename or move an existing file to a new location:
Storage::copy('old/file.jpg', 'new/file.jpg');
Storage::move('old/file.jpg', 'new/file.jpg');
Automatic Streaming
Streaming files to storage offers significantly reduced memory usage. If you would like Laravel to automatically manage streaming a given file to your storage location, you may use the putFile
or putFileAs
method. This method accepts either a path string or SwooleTW\Hyperf\Http\UploadedFile
instance and will automatically stream the file to your desired location:
use SwooleTW\Hyperf\Support\Facades\Storage;
// Automatically generate a unique ID for filename...
$path = Storage::putFile('photos', '/path/to/photo'));
// Manually specify a filename...
$path = Storage::putFileAs('photos', '/path/to/photo', 'photo.jpg');
There are a few important things to note about the putFile
method. Note that we only specified a directory name and not a filename. By default, the putFile
method will generate a unique ID to serve as the filename. The file's extension will be determined by examining the file's MIME type. The path to the file will be returned by the putFile
method so you can store the path, including the generated filename, in your database.
The putFile
and putFileAs
methods also accept an argument to specify the "visibility" of the stored file. This is particularly useful if you are storing the file on a cloud disk such as Amazon S3 and would like the file to be publicly accessible via generated URLs:
Storage::putFile('photos', '/path/to/photo', 'public');
File Uploads
In web applications, one of the most common use-cases for storing files is storing user uploaded files such as photos and documents. Laravel Hyperf makes it very easy to store uploaded files using the store
method on an uploaded file instance. Call the store
method with the path at which you wish to store the uploaded file:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use SwooleTW\Hyperf\Http\Request;
class UserAvatarController extends Controller
{
/**
* Update the avatar for the user.
*/
public function update(Request $request): string
{
$path = $request->file('avatar')->store('avatars');
return $path;
}
}
There are a few important things to note about this example. Note that we only specified a directory name, not a filename. By default, the store
method will generate a unique ID to serve as the filename. The file's extension will be determined by examining the file's MIME type. The path to the file will be returned by the store
method so you can store the path, including the generated filename, in your database.
You may also call the putFile
method on the Storage
facade to perform the same file storage operation as the example above:
$path = Storage::putFile('avatars', $request->file('avatar'));
Specifying a File Name
If you do not want a filename to be automatically assigned to your stored file, you may use the storeAs
method, which receives the path, the filename, and the (optional) disk as its arguments:
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
You may also use the putFileAs
method on the Storage
facade, which will perform the same file storage operation as the example above:
$path = Storage::putFileAs(
'avatars', $request->file('avatar'), $request->user()->id
);
Warning
Unprintable and invalid unicode characters will automatically be removed from file paths. Therefore, you may wish to sanitize your file paths before passing them to Laravel Hyperf's file storage methods. File paths are normalized using the League\Flysystem\WhitespacePathNormalizer::normalizePath
method.
Specifying a Disk
By default, this uploaded file's store
method will use your default disk. If you would like to specify another disk, pass the disk name as the second argument to the store
method:
$path = $request->file('avatar')->store(
'avatars/'.$request->user()->id, 's3'
);
If you are using the storeAs
method, you may pass the disk name as the third argument to the method:
$path = $request->file('avatar')->storeAs(
'avatars',
$request->user()->id,
's3'
);
Other Uploaded File Information
If you would like to get the original name and extension of the uploaded file, you may do so using the getClientOriginalName
and getClientOriginalExtension
methods:
$file = $request->file('avatar');
$name = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
However, keep in mind that the getClientOriginalName
and getClientOriginalExtension
methods are considered unsafe, as the file name and extension may be tampered with by a malicious user. For this reason, you should typically prefer the hashName
and extension
methods to get a name and an extension for the given file upload:
$file = $request->file('avatar');
$name = $file->hashName(); // Generate a unique, random name...
$extension = $file->extension(); // Determine the file's extension based on the file's MIME type...
File Visibility
In Laravel Hyperf's Flysystem integration, "visibility" is an abstraction of file permissions across multiple platforms. Files may either be declared public
or private
. When a file is declared public
, you are indicating that the file should generally be accessible to others. For example, when using the S3 driver, you may retrieve URLs for public
files.
You can set the visibility when writing the file via the put
method:
use SwooleTW\Hyperf\Support\Facades\Storage;
Storage::put('file.jpg', $contents, 'public');
If the file has already been stored, its visibility can be retrieved and set via the getVisibility
and setVisibility
methods:
$visibility = Storage::getVisibility('file.jpg');
Storage::setVisibility('file.jpg', 'public');
When interacting with uploaded files, you may use the storePublicly
and storePubliclyAs
methods to store the uploaded file with public
visibility:
$path = $request->file('avatar')->storePublicly('avatars', 's3');
$path = $request->file('avatar')->storePubliclyAs(
'avatars',
$request->user()->id,
's3'
);
Local Files and Visibility
When using the local
driver, public
visibility translates to 0755
permissions for directories and 0644
permissions for files. You can modify the permissions mappings in your application's filesystems
configuration file:
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'permissions' => [
'file' => [
'public' => 0644,
'private' => 0600,
],
'dir' => [
'public' => 0755,
'private' => 0700,
],
],
],
Deleting Files
The delete
method accepts a single filename or an array of files to delete:
use SwooleTW\Hyperf\Support\Facades\Storage;
Storage::delete('file.jpg');
Storage::delete(['file.jpg', 'file2.jpg']);
If necessary, you may specify the disk that the file should be deleted from:
use SwooleTW\Hyperf\Support\Facades\Storage;
Storage::disk('s3')->delete('path/file.jpg');
Directories
Get All Files Within a Directory
The files
method returns an array of all of the files in a given directory. If you would like to retrieve a list of all files within a given directory including all subdirectories, you may use the allFiles
method:
use SwooleTW\Hyperf\Support\Facades\Storage;
$files = Storage::files($directory);
$files = Storage::allFiles($directory);
Get All Directories Within a Directory
The directories
method returns an array of all the directories within a given directory. Additionally, you may use the allDirectories
method to get a list of all directories within a given directory and all of its subdirectories:
$directories = Storage::directories($directory);
$directories = Storage::allDirectories($directory);
Create a Directory
The makeDirectory
method will create the given directory, including any needed subdirectories:
Storage::makeDirectory($directory);
Delete a Directory
Finally, the deleteDirectory
method may be used to remove a directory and all of its files:
Storage::deleteDirectory($directory);
Testing
The Storage
facade's fake
method allows you to easily generate a fake disk that, combined with the file generation utilities of the SwooleTw\Hyperf\Http\UploadedFile
class, greatly simplifies the testing of file uploads. For example:
<?php
namespace Tests\Feature;
use SwooleTw\Hyperf\Http\UploadedFile;
use SwooleTW\Hyperf\Support\Facades\Storage;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function test_albums_can_be_uploaded(): void
{
Storage::fake('photos');
$response = $this->json('POST', '/photos', [
UploadedFile::fake()->image('photo1.jpg'),
UploadedFile::fake()->image('photo2.jpg')
]);
// Assert one or more files were stored...
Storage::disk('photos')->assertExists('photo1.jpg');
Storage::disk('photos')->assertExists(['photo1.jpg', 'photo2.jpg']);
// Assert one or more files were not stored...
Storage::disk('photos')->assertMissing('missing.jpg');
Storage::disk('photos')->assertMissing(['missing.jpg', 'non-existing.jpg']);
// Assert that a given directory is empty...
Storage::disk('photos')->assertDirectoryEmpty('/wallpapers');
}
}
By default, the fake
method will delete all files in its temporary directory. If you would like to keep these files, you may use the "persistentFake" method instead. For more information on testing file uploads, you may consult the HTTP testing documentation's information on file uploads.
Warning
The image
method requires the GD extension.
Custom Filesystems
Laravel Hyperf's Flysystem integration provides support for several "drivers" out of the box; however, Flysystem is not limited to these and has adapters for many other storage systems. You can create a custom driver if you want to use one of these additional adapters in your Laravel Hyperf application.
In order to define a custom filesystem you will need a Flysystem adapter. Let's add a community maintained Dropbox adapter to our project:
composer require spatie/flysystem-dropbox
Next, you can register the driver within the boot
method of one of your application's service providers. To accomplish this, you should use the extend
method of the Storage
facade:
<?php
namespace App\Providers;
use SwooleTW\Hyperf\Foundation\Contracts\Application;
use SwooleTW\Hyperf\Filesystem\FilesystemAdapter;
use SwooleTW\Hyperf\Support\Facades\Storage;
use SwooleTW\Hyperf\Support\ServiceProvider;
use League\Flysystem\Filesystem;
use Spatie\Dropbox\Client as DropboxClient;
use Spatie\FlysystemDropbox\DropboxAdapter;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
// ...
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Storage::extend('dropbox', function (Application $app, array $config) {
$adapter = new DropboxAdapter(new DropboxClient(
$config['authorization_token']
));
return new FilesystemAdapter(
new Filesystem($adapter, $config),
$adapter,
$config
);
}, true);
}
}
The first argument of the extend
method is the name of the driver and the second is a closure that receives the $app
and $config
variables. The closure must return an instance of SwooleTW\Hyperf\Filesystem\FilesystemAdapter
. The $config
variable contains the values defined in config/filesystems.php
for the specified disk. The third argument determines if this driver supports object pooling, and it's set as true
by default.
Warning
Most of the time you should set them poolable for keeping them safe in coroutines.
Once you have created and registered the extension's service provider, you may use the dropbox
driver in your config/filesystems.php
configuration file.