How to upload and download files to your application (SFTP/RSYNC)

Goal

This guide shows how to upload and download files to your application using sftp and rsync.

Assumptions

To complete this, you will need:

Problems

Platform.sh provides upload and download mechanisms through the CLI but it can be handy to use native tools instead.

Steps

  1. Add a mount point to your application

    By default, all files deployed to Platform.sh through git are read-only. Only the folders defined as mounts are writeable.

    Open ./.platform.app.yaml and add a mount:

    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
    
  2. Check the mount point is writeable

    platform ssh
    web@<environment id>:~$ touch web/uploads/test.txt
    

    The command above shouldn’t trigger any error.

  3. Get the environment ssh endpoint

    Both rsync and sftp protocols use ssh as the transfer layer. Each Platform.sh environment inside a project has its own ssh 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:

    ssh-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.

  1. Create test files

    Let’s create some files to play with:

    mkdir -p web/uploads
    touch web/uploads/test{0001..0010}.txt
    

rsync

  1. 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 at man 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.

  2. 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/*"
  1. 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.

  2. 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/*"
  1. 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>

  2. 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.

    filezilla-dialog

    Connect to the site. You should be presented with the content of the root folder of your app.

    filezilla

  3. Upload a file or a directory

    Drag and drop files and folders from your computer to the mount:

    filezilla-up

  4. 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.

2 Likes