Quickstart: Using the gsutil Tool

This page shows you how to perform basic tasks in Cloud Storage using the gsutil command-line tool.

Costs that you incur in Cloud Storage are based on the resources you use. This quickstart typically uses less than $0.01 USD worth of Cloud Storage resources.

Before you begin

  1. Faça login na sua Conta do Google.

    Se você ainda não tiver uma, inscreva-se.

  2. No Console do GCP, acesse a página "Gerenciar recursos" e crie um projeto.

    Acessar a página Gerenciar recursos

  3. Verifique se o faturamento foi ativado para o projeto.

    Saiba como ativar o faturamento

  4. Install the Cloud SDK.

    When prompted, choose the project that you created above.

  5. Install Python 2.7.

    If you are using Windows and you left the relevant checkbox selected when you installed the Cloud SDK, this was done automatically.

Create a bucket

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

To create a bucket:

  1. Open a terminal window.
  2. Use the gsutil mb command and a unique name to create a bucket:
    gsutil mb -l us-east1 gs://my-awesome-bucket/
    

    This uses a bucket named "my-awesome-bucket." You must choose your own, globally-unique, bucket name.

    If successful, the command returns:

    Creating gs://my-awesome-bucket/...
    
  3. You've just created a bucket where you can store your stuff!

Upload an object into your bucket

Image of a kitten.

  1. Right-click the image above and save it somewhere on your computer, such as on the desktop.

  2. Use the gsutil cp command to copy the image from the location where you saved it to the bucket you created:

    gsutil cp Desktop/kitten.png gs://my-awesome-bucket

    If successful, the command returns:

    Copying file://Desktop/kitten.png [Content-Type=image/png]...
    Uploading   gs://my-awesome-bucket/kitten.png:       0 B/164.3 KiB
    Uploading   gs://my-awesome-bucket/kitten.png:       164.3 KiB/164.3 KiB

    You've just stored an object in your bucket.

Download an object from your bucket

  1. Use the gsutil cp command to download the image you stored in your bucket to somewhere on your computer, such as the desktop:

    gsutil cp gs://my-awesome-bucket/kitten.png Desktop/kitten2.png

    If successful, the command returns:

    Copying gs://my-awesome-bucket/kitten.png...
    Downloading file://Desktop/kitten2.png:               0 B/164.3 KiB
    Downloading file://Desktop/kitten2.png:               164.3 KiB/164.3 KiB

    You've just downloaded something from your bucket.

Copy an object to a folder in the bucket

  1. Use the gsutil cp command to create a folder and copy the image into it:

    gsutil cp gs://my-awesome-bucket/kitten.png gs://my-awesome-bucket/just-a-folder/kitten3.png

    If successful, the command returns:

    Copying gs://my-awesome-bucket/kitten.png [Content-Type=image/png]...
    Copying     ...my-awesome-bucket/just-a-folder/kitten3.png: 164.3 KiB/164.3 KiB

    You've just copied your object into a new folder in your bucket.

List contents of a bucket or folder

  1. Use the gsutil ls command to list the contents at the top level of your bucket:

    gsutil ls gs://my-awesome-bucket

    If successful, the command returns a message similar to:

    gs://my-awesome-bucket/kitten.png
    gs://my-awesome-bucket/just-a-folder/

    You've just seen the contents at the top level of your bucket.

List details for an object

  1. Use the gsutil ls command, with the -l flag to get some details about an object:

    gsutil ls -l gs://my-awesome-bucket/kitten.png

    If successful, the command returns a message similar to:

    2638  2016-02-26T23:05:14Z  gs://my-awesome-bucket/kitten.png
    TOTAL: 1 objects, 168243.2 bytes (164.3 KiB)

    You've just obtained information about the object's size and date of creation.

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://my-awesome-bucket/kitten.png

    If successful, the command returns:

    Updated ACL on gs://my-awesome-bucket/kitten.png

    Now anyone can get your object.

  2. To remove this permission, use the command:

    gsutil acl ch -d AllUsers gs://my-awesome-bucket/kitten.png

    If successful, the command returns:

    Updated ACL on gs://my-awesome-bucket/kitten.png

    You have removed public access to this object.

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://my-awesome-bucket

    The command is successful if no error is returned.

    Now someone else can put things into and view what's in your bucket.

  2. To remove this permission, use the command:

    gsutil iam ch -d user:jane@gmail.com:objectCreator,objectViewer gs://my-awesome-bucket

    The command is successful if no error is returned.

    You have removed the user's access to this bucket.

Delete objects

  1. Use the gsutil rm command to delete an object:

    gsutil rm gs://my-awesome-bucket/kitten.png

    If successful, the command returns:

    Removing gs://my-awesome-bucket/kitten.png...

    This copy of the object is no longer stored on Cloud Storage (though the copy you made in the folder just-a-folder/ still exists).

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

  1. Open a terminal window (if not already open).
  2. Use the gsutil rm command with the -r flag to delete the bucket and anything inside of it:

    gsutil rm -r gs://my-awesome-bucket

    If successful, the command returns a message similar to:

    Removing gs://my-awesome-bucket/just-a-folder/cloud-storage.logo.png#1456530077282000...
    Removing gs://my-awesome-bucket/...

    Your bucket and its contents are deleted.

What's next

Esta página foi útil? Conte sua opinião sobre:

Enviar comentários sobre…

Precisa de ajuda? Acesse nossa página de suporte.