Transferring files between local machine and AWS instance

Author Maroje Macola
Category Development
Date Jun 02, 2021
2 min read

Recently, I needed to transfer files from local machine to the remote AWS instance. Since I haven’t done this before, after a quick googling, I stumbled upon a cool bash command scp (secure copy). After seeing secure keyword in its name, I didn’t waste time on looking for other solutions, although I believe there are many. Therefore, let’s take a look at how can we use it.

scp command

Generally, with this command, we are able to do 3 types of file transfers:

  • from local to remote machine
  • from remote to local machine
  • between two remote machines, by using local machine

In this post, we will cover first 2 cases.

To get a list of available options for scp command, we can run scp help:

$ scp help
usage: scp [-346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] 
[-l limit] [-o ssh_option] [-P port] [-S program] source ... target

A comprehensive list with detailed explanations can be found here.

[- i identity_file] parameter will be of use for our case of communicating with AWS instances, since we need to use private key which is also used for ssh connection with the server.

[- r Recursively copy entire directories] parameter will be of help to transfer whole directory.

Local -> remote file transfer

Scenario – transferring an image from the local machine’s Downloads folder to AWS server, which has ec2-user and IP address 192.168.1.1: 

scp -i ~/.ssh/key ~/Downloads/image.png ec2-user@192.168.1.1:~/deployment/current/public 

Let’s segment command for easier understanding:

  • -i ~/ .shh/key specifying path to the identity_file, key which you also use for ssh connection with your AWS instance
  • ~/Downloads/image.png specifying path to the file which we want to transfer from local machine
  • ec2-user@192.168.1.1 specifying AWS user and IP address of the remote instance
  • ~/deployment/current/public specifying path to the destination directory on the remote instance (where transferred file will exist after execution)

Remote -> local file transfer

Scenario – transferring src directory from AWS server, to the local Downloads folder:

scp -i ~/.ssh/key -r ec2-user@192.168.1.1:~/deployment/current/src ~/Downloads

Let’s segment command for easier understanding:

  • -i ~/.ssh/key specifying path to the identity_file, key which you also use for ssh connection with your AWS instance
  • -r ec2-user@192.168.1.1 specifying AWS user and IP address of the remote instance, this time with -r option we say that we want to transfer directory recursively
  • ~/deployment/current/src specifying path to the directory which we want to transfer to from AWS server
  • ~/Downloads specifying path to the destination directory on the local machine (where transferred file will exist after execution)

Conclusion

scp is a very powerful command which allows us to easily transfer files between local and remote AWS server. As mentioned in one of the paragraphs, it has many additional options to be used, but we only need a few of them to be able to do a basic data transfer.

_____
We’re available for partnerships and open for new projects.
If you have an idea you’d like to discuss, share it with our team!

Subscribe to our newsletter

Subscribe now and get our top news once a month.

100% development, design & strategy.

100% spam-free.

    You're now a part of our club!

    We got your e-mail address and you'll get our next newsletter!