How to copy files from google compute engine to local directory
Asked Answered
H

10

59

I am trying to copy files from my instance to my local directory using following command

gcloud compute scp <instance-name>:~/<file-name> ~/Documents/

However, it is showing error as mentioned below

$USER/Documents/: Is a directory

ERROR: (gcloud.compute.scp) [/usr/bin/scp] exited with return code [1].

Copying from local directory to GCE works fine.

I have checked Stanford's tutorial and Google's documentation as well.

I have one another instance where there is no issue like this.

I somewhat believe it might be issue with SSH keys.

What might have gone wrong?

Humerus answered 8/7, 2017 at 3:53 Comment(2)
is there a link to the stanford tutorial that you've mentioned?Fodder
And you might as well link to whatever Google documentation you've been reading.Parse
D
58

Your command is correct if your source and destination paths are correct

The command as you've posted in your question works for me when copying a file from the Google Compute Engine VM to my local machine.

$ gcloud compute scp vm1:~/.bashrc ~/Documents/
.bashrc                                          100% 3515     3.4KB/s   00:00

I also tried doing the copy from other side (i.e. from my local machine to GCE VM) and it works:

$ gcloud compute scp ~/Documents/.bashrc vm1:~/temp/
.bashrc                                          100% 3515     3.4KB/s   00:00

$ gcloud compute scp ~/Documents/.bashrc vm1:~/.bashrc-new
.bashrc                                          100% 3515     3.4KB/s   00:00

gcloud relies on the scp executable present in your PATH. The arguments you provide to the gcloud scp command are passed through to the scp binary. Assuming your source and destination paths are correct, it should work.

Recursive copying using scp

Based on your particular error message though, I've seen that variation only appear when the source path you're trying to copy from is a directory instead of file. For that particular case, you can pass a --recurse argument (similar to the -r argument supported by regular scp) which will recursively copy all files and directories under the specified directory.

gcloud compute scp --recurse SRC_PATH DEST_PATH

Dives answered 9/7, 2017 at 2:30 Comment(5)
It appears that the flag is --recurse, not -r per the docs, and I couldn't get -r to work. That's an SSH flag, not a gcloud compute scp flag.Bamboozle
Thanks @MishaBrukman - yes I had the -r argument supported by regular scp. With gcloud compute scp it is --recurse instead. Updated the answer.Dives
How to specify the zoneFoulard
@Foulard you can specify zone by --zone=your_instance_zoneUnsegregated
Example scp command to download a file from VM to local system: gcloud compute scp --zone "europe-west1-c" --project "projectName" "vmName":/path/to/file/on/vm/301232-PNG.tar.gz local_file_name.tar.gzLibertylibia
E
35

To copy files from VM to your desktop you can simply SSH into the VM and on top right corner there is a settings button, there you will find the download file option just enter the path of file.

If it is folder then first zip the folder then download it.

Errata answered 10/11, 2018 at 8:2 Comment(1)
Had no idea you could do that. Thanks!Vaginitis
H
13

Everything was perfect except I was trying to run these commands on the terminal connected to GCE instead of local terminal.

oyashi@oyashi-torch-instance:~$ gcloud compute scp oyashi-torch-instance:~/spring1617_assignment1.zip ~/Documents/

/home/oyashi/Documents/: Is a directory ERROR: (gcloud.compute.scp) [/usr/bin/scp] exited with return code [1].

But when I tried this one on my local terminal. This happened.

oyashi@oyashi:~/Documents$ gcloud compute scp oyashi-torch-instance:~/spring1617_assignment1.zip ~/Documents/

spring1617_assignment1.zip 100% 42KB 42.0KB/s 00:00

Thank you everyone for their comments and help. I know its a silly mistake from my end. But I posted this answer so that others might learn from my silliness.

Humerus answered 17/7, 2017 at 17:18 Comment(0)
D
6

If you need to pass the information of zone, project name you may like to do as it worked for me: the instance name is the name you chose in the GCP instances.

gcloud beta compute scp --project "project_name" --zone "zone_name" instance_name:~jupyter/file_name /home/Downloads
Difficile answered 18/5, 2020 at 15:8 Comment(0)
B
1

I met the same problem. The point is you should run the scp command from a local terminal, rather than cloud terminal.

Bern answered 17/10, 2020 at 5:36 Comment(0)
H
0

For copying file to local machine from Ubuntu vmware

For ex: you have instance by name : bhk

Houseline answered 22/6, 2020 at 15:48 Comment(0)
W
0

Run a basic nginx server and copy all the files in
/var/www/html (nginx serving dir)
and then from your local machine simple run
wget <vm's IP>/<your file path>

For example If my vm's IP is 1.2.3.4 and I want to copy /home/me/myFolder/myFile , then simply copy this file in /var/www/html

then run wget 1.2.3.4/myfile

Walrus answered 10/9, 2020 at 6:33 Comment(0)
M
0

this works for me:

gcloud compute scp --project "my-project" ./my-file.zip user@instance-1:~

--project - google cloud project name

my-file.zip - local file to send to VM

user - vm linux username

instance-1 - instance name (vm name)

~ - instance destination path

Mcgary answered 9/1, 2022 at 7:20 Comment(0)
G
0

I use below script to upload directory from local to remote directory

gcloud compute scp --recurse myweb-app/www/* user@instant-name:/var/www/html/sub-sites/myweb-app/www/
Gambrill answered 20/1, 2022 at 8:1 Comment(0)
I
0

You can use the below command to copy the directory from GCP VM to Local: gcloud compute scp --recurse --zone "zone-name" VM-name:/tmp/directory or file name . --project "project-id" but firstly move the required file to /tmp folder that needs to be copied or scp to local.

Incomprehensible answered 18/2 at 18:40 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Decolorant

© 2022 - 2024 — McMap. All rights reserved.