Craft CMS assets handling

# Install and deploy run down

  1. Install Craft CMS locally
  2. Setup an app at fortrabbit
  3. Deploy code via Git
  4. Database syncing
  5. Assets syncing ← you are here

# About Craft assets

Craft CMS "assets" are user generated stuff: uploaded files, mostly images that lives in "volumes" — also see the official Craft docs on that.

# Assets and Git

When creating asset volumes in the Craft CMS control panel, their contents are by default excluded from Git, so that code and content are separated. However, if you've already created a directory in your web folder and you now reference that directory as the webroot of a volume you're creating, you will need to manually configure Git to ignore the files in it. To do this, add a file .gitignore in the volume's directory, with the following contents:

*
!.gitignore
gitignore

Another reason that assets should be managed and deployed independently from Git deployment, is that files uploaded to the environment would not be represented in Git, as Git deployment is a one way street.

# Options to sync assets

There are two directions you want your assets to flow:

  • UP — When you first developed Craft locally, you may want to upload all the images you already have locally to the environment;
  • DOWN — When the environment is running in production for some time and the clients or editors have created content on the environment you want to download that, so your local version is in sync.

# 1 — Manage assets with rsync

Give the good old rsync command line tool a try. It's easier than you think:

# Sync UP local assets with remote
$ rsync -av ./web/{your-asset-folder} {{app-env-id}}@ssh.{{region}}.frbit.app:web/
shell

Replace the path and your individual folder name with your own settings. Our rsync tutorial covers many useful rsync options, like excludes, --dry-run and --delete.

# 2 — Manage assets with SFTP

Good old SFTP access is another valid way to to do it. Just fire up your SFTP client, log in to the App and manage the assets manually. Many SFTP clients are offering sync options, to keep local and remote files up-to-date.

# 3 — Outsource assets to a cloud storage

The assets are not stored on the file system of the App any more; once the user uploads files, they get uploaded to external cloud storage like S3 (or the Object Storage here). Then, within the templates you hot-link all the files to the external storage. This more professional design helps to reduce load on the App. Look out for S3 Craft plugins to get started with this. Only use this if you really need it. Don't over-engineer.

Found a tpyo?Edit