Filesystem and Uploading
Masonite comes with a simple way to upload files to different file systems.
In Masonite, a Filesystem is a place where assets are stored. These locations could be somewhere local on the server or in a cloud storage service like Amazon S3.
Configuration
The configuration for the filesystem feature is broken up into "disks". These "disks" or connection configurations can be any name you want.
Here is an example configuration:
Default
The default configuration is the name of the disk you want to be used as the default when using the Filesystem features.
Local Driver
The local driver is used for local filesystems like server directories. All files are stored and managed "locally" on the server.
Option | Description |
---|---|
| The driver to use for this disk |
| The base path to use for this disk |
S3 Driver
The S3 driver is used for connecting to Amazon's S3 cloud service.
Option | Description |
---|---|
| The driver to use for this disk |
| The Amazon S3 client key |
| The Amazon S3 secret key |
| The Amazon S3 bucket name |
| A path to be used for displaying resources |
Uploading Files
Uploading files is simple. You will have to use the Masonite Storage
class.
The first and most simplest method is taking a file and putting text into it. For this we can use the put
method
Method | Description |
---|---|
| Boolean to check if a file exists. |
| Boolean to check if a file does not exist. |
| Creates a |
| Copies a file from 1 directory to another directory |
| Moves a file from 1 directory to another |
| Prepends content to a file |
| Appends content to a file |
| Puts content to a file |
| Gets content of a file |
| Puts a file resource to a directory. Must be an instance of Masonite's |
Uploading Form Files
When uploading files from a form you will find the put_file
method more useful:
The put_file
method will return the relative path to the file so you can save it to the database and fetch it later.
By default, a file name will be auto generated for you using a UUID4 string. You can specify your own name by using a name
parameter:
You do not need to specify the extension in the name as the extension will be pulled from the resource object.
Asset Helper
Displaying Files
When uploading images to something like an AWS bucket, you may want to display the images. You may use a combination of the asset helper and setting a path in your filesystem config. This mainly just provides a nice interface for combining 2 strings
When using Amazon S3, you will need to set your bucket permissions and policies appropriately.
First, set a path in your filesystem config:
Then in your templates you can use the asset helper:
The signature is:
Multiple Paths
You may also specify multiple paths as a dictionary:
Then inside your asset helper you can use dot notation to specify the path you want to use:
Last updated