Goal
This guide shows how to upload and download files to your application using sftp
and rsync
.
Assumptions
To complete this, you will need:
- A working application setup on a Platform.sh project
- The Platform.sh CLI tool installed
- Your
ssh
key loaded in your ssh agent and configured in the Platform.sh dashboard -
rsync
installed on your system - or
scp
installed on your system - or an
sftp
GUI client like FileZilla
Problems
Platform.sh provides upload and download mechanisms through the CLI but it can be handy to use native tools instead.
Steps
-
Add a mount point to your application
By default, all files deployed to Platform.sh through
git
are read-only. Only the folders defined asmounts
are writeable.Open
./.platform.app.yaml
and add amount
:mounts: 'web/uploads': source: local source_path: uploads
Please refer to the mounts documentation if needed.
Push your configuration change:
git add .platform.app.yaml git commit -m "Add mount" git push platform master
-
Check the mount point is writeable
platform ssh web@<environment id>:~$ touch web/uploads/test.txt
The command above shouldn’t trigger any error.
-
Get the environment
ssh
endpointBoth
rsync
andsftp
protocols usessh
as the transfer layer. Each Platform.sh environment inside a project has its ownssh
endpoint.If you have the Platform.sh CLI installed, you can run
platform ssh --pipe
to get the endpoint.If not, log into your project dashboard and click on the
SSH
button for the environment you want to get the endpoint:Our endpoint is
<project id>-master-7rqtwti--app@ssh.<region>.platform.sh
Note: All the next commands will be using platform ssh --pipe
as the endpoint. Feel free to replace with the full connection string if needed.
-
Create test files
Let’s create some files to play with:
mkdir -p web/uploads touch web/uploads/test{0001..0010}.txt
rsync
-
Upload a file or a directory
To upload a directory, make sure you don’t specify the source folder (
uploads
in this case) in the destination. Take a look atman rsync
to view all available options.rsync -avz web/uploads "$(platform ssh --pipe)":web/
The log should list all files that were uploaded:
sending incremental file list uploads/ uploads/test0001.txt ... uploads/test0010.txt sent 603 bytes received 210 bytes 325.20 bytes/sec total size is 0 speedup is 0.00
We could have synced only the files with the following command:
rsync -avz web/uploads/* rsync -avz web/uploads "$(platform ssh --pipe)":web/uploads/
As rsync is transferring only the differences between the source and destination, relaunching the same command will end up in an empty transfer.
-
Download a file or a directory
Remove your local files:
rm web/uploads/*
We can now download them from Platform.sh:
rsync -avz "$(platform ssh --pipe)":web/uploads web/
You can see in the output that all files are transferred back to our host.
scp
Remove the files on Platform.sh if you have followed the rsync
steps:
platform ssh "rm web/uploads/*"
-
Upload a file or a directory
To upload a file, specify the full path:
scp web/uploads/test0001.txt "$(platform ssh --pipe)":web/uploads
To upload a directory, add the
-r
argument:scp -r web/uploads "$(platform ssh --pipe)":web/
The output should list the 10 files being transferred. Note that
scp
is not using incremental transfer. All 10 files are being transferred even if they already exist at the destination. -
Download a file or a directory
Remove a local file first:
rm web/uploads/test0001.txt
To download a file:
scp "$(platform ssh --pipe)":web/uploads/test0001.txt web/uploads/
To download a directory:
scp -r "$(platform ssh --pipe)":web/uploads web/
sftp
Remove the files on Platform.sh if you have followed the previous steps:
platform ssh "rm web/uploads/*"
-
Get the SSH connection information with:
platform ssh --pipe
The returned URL is made from two distinct parts separated by the@
character.
<SSH_USER>@<SSH_ENDPOINT> -
Configure your client
Add a new bookmark to your client with the following configuration (based on our endpoint):
hostname: <SSH_ENDPOINT> port: 22 protocol: SFTP user: <SSH_USER> key/identity file: `/path/to/your/key
FileZilla needs to have a
.pem
key file to list it.Connect to the site. You should be presented with the content of the root folder of your app.
-
Upload a file or a directory
Drag and drop files and folders from your computer to the
mount
: -
Download a file or a directory
Drag and drop files and folders from the
mount
to your computer.
Conclusion
Transferring and managing files on your Platform.sh environments can be done using native tools that use ssh
, scp
, rsync
, or sftp
.