How to copy files from colab or cloud storage to google drive?
Asked Answered
G

2

6

I read some code on this problem, but i cannot make sense of the code. Could anybody help explaining the code to me?

# mount your drive
from google.colab import drive
drive.mount('/content/drive')
!gsutil -q -m cp -r gs://my-bucket-name drive /content/drive/My\ Drive/

I want to transfer files from colab or google cloud storage to gdrive. What 'drive /content/drive/My\ Drive/' stands for in the code? how should I parse this piece of code. If it works for directory, how should I modify this piece of code to make it work for a single file?

Groundage answered 13/6, 2019 at 10:40 Comment(0)
H
4

gsutil cp -r does a recursive copy from one or more source files/dirs to some destination directory. E.g. to copy one or more directories into another directory, you'd do:

gsutil cp -r src_folder1/ src_folder2/ dst_folder/

So, let's explain what all is happening in your example above:

  • You first mount the contents of your Google Drive, using some file system adapter magic under the hood via drive.mount(), under the local directory at /content/drive.
  • You then run a gsutil command. gsutil sees the argument "drive" as another source file (or directory) that it should copy to the directory "/content/drive/My Drive/". If the file/dir "drive" doesn't exist, gsutil skips it and complains that it didn't exist (but gsutil will still copy the other source arguments to the destination, due to the -m flag which causes it to continue (where possible) upon encountering a problem).

So, if you wanted to copy an object named "my-object-name" from your bucket to the root of your Google Drive, the command would look something like this:

!gsutil -q -m cp gs://my-bucket-name/my-object-name /content/drive/My\ Drive/

or, to copy the object and name it something different:

!gsutil -q -m cp gs://my-bucket-name/my-object-name /content/drive/My\ Drive/some-new-name

To read more about gsutil, its top-level flags, and its cp command, check out the web docs:

Howling answered 13/6, 2019 at 23:36 Comment(1)
Thank you so much for such a detailed explanation!Groundage
I
7
!cp "colab-path" -r "drive-path"
Iphigenia answered 24/6, 2020 at 8:10 Comment(2)
why -r ? I tried without -r and it works fineOuabain
Recursive copy for directory instead of single file.Prognosis
H
4

gsutil cp -r does a recursive copy from one or more source files/dirs to some destination directory. E.g. to copy one or more directories into another directory, you'd do:

gsutil cp -r src_folder1/ src_folder2/ dst_folder/

So, let's explain what all is happening in your example above:

  • You first mount the contents of your Google Drive, using some file system adapter magic under the hood via drive.mount(), under the local directory at /content/drive.
  • You then run a gsutil command. gsutil sees the argument "drive" as another source file (or directory) that it should copy to the directory "/content/drive/My Drive/". If the file/dir "drive" doesn't exist, gsutil skips it and complains that it didn't exist (but gsutil will still copy the other source arguments to the destination, due to the -m flag which causes it to continue (where possible) upon encountering a problem).

So, if you wanted to copy an object named "my-object-name" from your bucket to the root of your Google Drive, the command would look something like this:

!gsutil -q -m cp gs://my-bucket-name/my-object-name /content/drive/My\ Drive/

or, to copy the object and name it something different:

!gsutil -q -m cp gs://my-bucket-name/my-object-name /content/drive/My\ Drive/some-new-name

To read more about gsutil, its top-level flags, and its cp command, check out the web docs:

Howling answered 13/6, 2019 at 23:36 Comment(1)
Thank you so much for such a detailed explanation!Groundage

© 2022 - 2024 — McMap. All rights reserved.