File Storage
Last updated
Last updated
When dealing with scalable Cloud Systems, everyone needs different File Storages. butterfly provides an abstraction layer for you to upload your static images / files.
Butterfly uses for abstracting file systems.
You can use multiple storages at the same time. Storages are managed under System Settings > CMS - File Storages
page. You can add / edit / remove storages.
File Storage Drivers are:
Local Folder
Amazon S3
Azure Blob Storage
Google Cloud Storage
SFTP
FTP
This adapter ships with Butterfly by default.
Installation
Usage Google Cloud Storage requires Service Account Credentials, which can be generated in the Cloud Console. Read more in the official documentation.
The credentials will be auto-loaded by the Google Cloud Client.
The client will first look at the GOOGLE_APPLICATION_CREDENTIALS env var. You can use putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
to set the location of your credentials file.
The client will look for the credentials file at the following paths:
windows: %APPDATA%/gcloud/application_default_credentials.json
others: $HOME/.config/gcloud/application_default_credentials.json
If running in Google App Engine, the built-in service account associated with the application will be used. If running in Google Compute Engine, the built-in service account associated with the virtual machine instance will be used.
Installation
Installation
This adapter ships with Butterfly by default.
Allowed extensions for images: jpg, jpeg, gif, svg, png
You can upload / crop an image from Admin Panel if it's defined as Widget Spec, Setting or Object Spec; but you may need to download an image programatically.
When you need to download an image, you can use the following code example:
You can define Image Aliases from System Settings
> Image Uploads
.
successful response:
failed response:
If you need to upload an image programatically else than Admin Panel, you can do it easily using the following example:
UPLOAD_KEY designates the key in $_FILES array which means the input name of the Upload. You can define Image Aliases from System Settings
> Image Uploads
.
successful response:
failed response:
You can upload images using Butterfly REST Api.
For api/upload/image
endpoint, available options:
alias: Butterfly image upload alias, default: content
sub_folder: Image upload path, default: null
files: Array of Images
Sample request:
Note: Response includes webp params only when it was enabled.
Sample success response:
Sample error response:
When you use image uploads, only filenames are kept in the Database. Although full paths of the images for all possible sizes are saved in Elastic Search Index (Data Pools), you may want to get data from Database.
You can get the full path of an image alias like the following:
Getting image full path by alias from PHP Code:
Getting image full path by alias from Twig Templates:
Allowed file upload extensions are defined from Admin panel.
Restricted File Types: php, php5, sh
You can upload / crop an image from Admin Panel if it's defined as Widget Spec, Setting or Object Spec; but you may need to download an image programatically.
When you need to download an image, you can use the following code example:
You can define Image Aliases from System Settings
> Image Uploads
.
successful response:
failed response:
If you need to upload an image programatically else than Admin Panel, you can do it easily using the following example:
UPLOAD_KEY designates the key in $_FILES array which means the input name of the Upload. You can define Image Aliases from System Settings
> Image Uploads
.
successful response:
failed response:
You can upload files using Butterfly REST Api.
For api/upload/file
endpoint, available options:
alias: Butterfly file upload alias, default: content
sub_folder: File upload path, default: null
files: Array of Files
Sample request:
Sample success response:
Sample error response:
When you use file uploads, only filenames are kept in the Database. Although full path of the file upload saved in Elastic Search Index (Data Pools), you may want to get data from Database.
You can get the full path of an file alias like the following:
Getting file full path by alias from PHP Code:
Getting image full path by alias from Twig Templates:
By default, butterfly
creates subfolders in year-month/day format. Which means that, when you upload a file named x.jpg
your file will be uploaded to 21-05/01/x.jpg if date is 1th of May in 2021.
You can change this behaviour by updating Sub Folder Format on File Storage Admin. If you leave this field empty, it will default to default behaviour.
If you set /
to this setting, it will upload file to the root.
If you want to use different dates, you can use strftime function format. You can checkout [https://www.php.net/strftime](this link)
When you let users to upload files or images, files with same filename is always something you need to consider. You can set Overwrite Strategy option under File Storages
for uploaded files with same name.
If you choose this option, unix time will be added to filename if there is already a file with same name.
For example: Let's say that you uploaded a file named test.png
. If you upload another file with the same name, it will be saved as test-1624119416.png
.
Advantages:
Existing files with the same name used by other records will not be affected.
Since the name of the file is changed, CDN Caches automatically becomes invalid for updated images.
Simple: It will replace the old file if a file with same name is added.
[!WARNING] Don't choose
overwrite
option if you really don't need it. It may lead data loss since old files with same name is overridden. And if you need this option just for some Upload Config, create additional File Storage for these Upload Configs.
When you upload a file or image to Butterfly, it's a good practice to replace white-spaces and special characters in the filename to prevent errors on browsers etc, but sometimes you may want to leave filename as-is.
Sanitize Option is enabled by default in File Storage
settings, but if you uncheck this box, then it will leave the filename and spaces as-is.
Example:
If you upload a file named Test Filename ÇÖĞ.jpg
Sanitized version will be test-filename-cog.jpg
Not Sanitized version Test Filename ÇÖĞ.jpg