Hands-On Machine Learning on Google Cloud Platform
上QQ阅读APP看书,第一时间看更新

Google Cloud Storage gsutil

We have seen that using the Google Cloud Storage browser is simple and intuitive. But in some cases it is necessary to operate through a command window. In such cases, as we have seen in previous sections, we can use gsutil. Previously, we had already seen a series of commands via gsutil.

To create a bucket using gsutil, use the mb command. For example, use the following command:

gsutil mb gs://NameBucket1

This creates a bucket named NameBucket1. It is important to remember that bucket names must be unique within the entire Google Storage namespace. If another user has already created a bucket with the name we want to use, we must choose another name.

Once the buckets are created, we can import the objects within them. With the cp command, we can copy files from our computer to Google Storage:

gsutil cp figure.jpg gs://NameBucket1/fig.jpg

This copies the image figure.jpg from the folder in which we are positioned with the shell in the bucket NameBucket1 renaming the object in fig.jpg.

To list our buckets or the objects contained in them you must use the ls command.

The ls command is used in Unix-like operating systems to display information about files and directories. If we open a terminal and type ls, we get the list of files and directories of the current directory.

The following command lists all the buckets we have created:

gsutil ls

 While this command lists the objects contained in NameBucket1:

gsutil ls gs: // NameBucket1

You can also use the -L option of the ls command to get more information about objects and buckets. The following command provides information about the size of objects, the date of the last modification, the data type, and the Access Control Lists (ACL) of all objects contained in the NameBucket1 bucket:

gsutil ls -L gs: // NameBucket1

While the following command provides information on our buckets, such as the number of objects contained, the total size, and the ACL of all our buckets:

gsutil ls -L

Finally, to transfer an object from one bucket to another, use the mv command, this command can also be used to rename an object. For example, the following command transfers the image figure.png from the NameBucket1 bucket to the NameBucket2 bucket:

gsutil mv gs://NameBucket1/figure.png gs:// NameBucket2/

 While this command renames the image figure.png to fig.png:

gsutil mv gs://NameBucket1/figure.png gs://NameBucket1/fig.png