Sunday, January 27, 2019

How to copy files from GCP cloud bucket to local machine using gsutil


How to copy files from GCP cloud bucket to local machine using  gsutil


If you are trying to download files from a GCP bucket storage to your local machine using cloud shell, you may encounter an error like:

CommandException: Destination URL must name a directory, bucket, or bucket

Running gsutil command in cloud shell(ephemeral VM) will not of much help here and it works fine if we intend to copy from one storage bucket to another bucket.

However, our requirement is to copy the file to the local system. The steps will be:

  1. Download GoogleCloudSDKInstaller.exe and install it on your local system
  2. Then open Google cloud SDK and it will prompt you to connect to your GCP account
  3. Then it will ask the project name in which the intended file to be copied is present
  4. Command to pass:
     
            gsutil cp <source> <destination>

            For example:

                      gsutil cp gs://punamtestbucket/test.txt  F:\punam\GCP_1


Some other basic stuff around GCP bucket:

  • It give option to upload files and folder from local machine to bucket however download option is not available and for that we need to use the gsutil commands

                    

  • Create a bucket

            Buckets are the basic containers that hold your data in Cloud Storage

             gsutil mb -l us-east1 gs://punamtestbucket2


             If bucket name already exists then will get an exception as:
       
                          ServiceException: 409 Bucket my-awesome-bucket already exists.
                          Try again with a different bucket name.


  • List contents of a bucket or folder

            gsutil ls gs://punamtestbucket2

  
  • Make your object publicly accessible

  1. Use the gsutil acl ch command to grant all users read permission for the object stored in your bucket:

    gsutil acl ch -u AllUsers:R gs://punamtestbucket/test.txt
          If successful, the command returns:

                Updated ACL on gs://punamtestbucket/test.txt
          Now anyone can get your object.
  1. To remove this permission, use the command:

    gsutil acl ch -d AllUsers gs://punamtestbucket/test.txt

  • Give someone access to your bucket

  1. Use the gsutil iam ch command to give a specific email address permission to read and write objects in your bucket:

    gsutil iam ch user:jane@gmail.com:objectCreator,objectViewer gs://punamtestbucket/test.txt
          Now someone else can put things into and view what's in your bucket.
  1. To remove this permission:

    gsutil iam ch -d user:jane@gmail.com:objectCreator,objectViewer gs://punamtestbucket/test.txt


Point to be noted is:
Use ACL(Access Control List) when need to give permission at object level whereas use IAM(Identity Access Management) when bucket level permission needs to be provided

  • Delete objects
 
                      gsutil rm gs://punamtestbucket/test.txt

  • To delete bucket and its content

            gsutil rm -r  gs://punamtestbucket

No comments:

Post a Comment