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:
1
DISKS = {
2
"default": "local",
3
"local": {
4
"driver": "file",
5
"path": os.path.join(os.getcwd(), "storage/framework/filesystem")
6
#
7
},
8
"s3": {
9
"driver": "s3",
10
"client": os.getenv("AWS_CLIENT"),
11
"secret": os.getenv("AWS_SECRET"),
12
"bucket": os.getenv("AWS_BUCKET"),
13
"path": "https://bucket.s3.us-east-2.amazonaws.com"
14
},
15
}
Copied!

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
driver
The driver to use for this disk
path
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
driver
The driver to use for this disk
client
The Amazon S3 client key
secret
The Amazon S3 secret key
bucket
The Amazon S3 bucket name
path
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
1
from masonite.filesystem import Storage
2
from masonite.requests import Request
3
4
def store(self, storage: Storage, request: Request):
5
storage.disk('local').put('errors/info.log', 'errors')
Copied!
Method
Description
exists(file_path)
Boolean to check if a file exists.
missing(file_path)
Boolean to check if a file does not exist.
stream
Creates a FileStream object to manage a file stream.
copy('/file1.jpg', '/file2,jpg')
Copies a file from 1 directory to another directory
move('/file1.jpg', '/file2,jpg')
Moves a file from 1 directory to another
prepend('file.log', 'content')
Prepends content to a file
append('file.log', 'content')
Appends content to a file
put('file.log', 'content')
Puts content to a file
put_file('directory', resource, "name")
Puts a file resource to a directory. Must be an instance of Masonite's UploadedFile. Takes an optional third name parameter to specify the file name

Uploading Form Files

When uploading files from a form you will find the put_file method more useful:
1
from masonite.filesystem import Storage
2
from masonite.requests import Request
3
4
def store(self, storage: Storage, request: Request):
5
path = storage.disk('local').put_file('avatars', request.input('avatar'))
Copied!
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:
1
storage.disk('local').put_file('avatars', request.input('avatar'), name="user1")
Copied!
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:
1
DISKS = {
2
"default": "local",
3
# "..",
4
"s3": {
5
"driver": "s3",
6
# "..",
7
"path": "https://bucket.s3.us-east-2.amazonaws.com"
8
},
9
}
Copied!
Then in your templates you can use the asset helper:
1
<img src="{{ asset('s3', user.avatar_url) }}">
Copied!
The signature is:
1
asset('disk', file_name)
Copied!

Multiple Paths

You may also specify multiple paths as a dictionary:
1
DISKS = {
2
"default": "local",
3
# "..",
4
"s3": {
5
"driver": "s3",
6
# "..",
7
"path": {
8
"logos": "https://bucket.s3.us-east-2.amazonaws.com/logos",
9
"invoices": "https://bucket.s3.us-east-2.amazonaws.com/invoices"
10
}
11
},
12
}
Copied!
Then inside your asset helper you can use dot notation to specify the path you want to use:
1
<img src="{{ asset('s3.logos', user.avatar_url) }}">
2
<a href="{{ asset('s3.invoices', invoice_url) }}"
Copied!