Path: blob/master/notebooks/tutorials/GCS_demo_v2.ipynb
1192 views
Authenticate in order to access the GCS
We don't need authentication to access public data in GCloud but in order to access protected data or write to a protected bucket, you need to set up credentials (authenticate)
Setting a project
We need to choose a project inorder to work with buckets, if you dont have any, create a project in Gcloud Console
First we need to set a default project using the project_id so that you are able to use commands which unless require specifying the project.
glcoud
can be used to set the default project.
Using gsutil- CLI for GCloud
Create
command to create a bucket
This will create a bucket with bucket_name
with default configurations.
Creating a local folder with a test file to upload to the bucket
Uploading the folder to the bucket
The object gs://{bucket_name}/test_folder/test_file.txt
is created.
Copying file:///tmp/test_folder/test_file.txt [Content-Type=text/plain]...
/ [1 files][ 38.0 B/ 38.0 B]
Operation completed over 1 objects/38.0 B.
Read
The contents of the uploaded file in the bucket can be read in this way
The whole folder/file from the bucket can be downloaded in this way.
Update
Updating a file
Edit the local copy and overwrite the file in the bucket.
If you want to update a folder in the bucket to be in sync with a local copy of the folder, use rsync
Making some changes to the test_folder
adding a new file test_file2.txt
You can check the contents of your bucket at any level using the ls
cmd.
bucket contents before updating.
bucket contents after updating.
Delete
Contents of a bucket can be deleted using rm
command
Removing gs://colab-sample-bucket-01de0f0e-dd79-11eb-90f2-0242ac1c0002/test_folder/test_file2.txt#1625480325741016...
/ [1 objects]
Operation completed over 1 objects.
Deleting a bucket
The rb command deletes a bucket. Buckets must be empty before you can delete them.
Making TFDS records
Using GC python API
Create
Creating a bucket
Uploading a object (a file with its path)
Read
To easily download all objects in a bucket or subdirectory, use the gsutil cp
command.
Update
Updating a file/object Its simply overwriting the existing copy of the object.
Delete
objects can be deleted easily by using blob.delete()
When a folder is empty it will be vanished.
Deleting a bucket
Using gcsfuse(mount) and bash
Another way is to mount the GCS bucket to the current session of colab and you can make any regular(CRUD) operations using bash commands(just like on any other directory)
In order to use this, you should first create a bucket you can do this using any of the methods described above)
gsutil mb
is used here
Create
Mounting the bucket to /content/{bucket_name}
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2537 100 2537 0 0 117k 0 --:--:-- --:--:-- --:--:-- 117k
OK
97 packages can be upgraded. Run 'apt list --upgradable' to see them.
The following NEW packages will be installed:
gcsfuse
0 upgraded, 1 newly installed, 0 to remove and 97 not upgraded.
Need to get 10.8 MB of archives.
After this operation, 23.1 MB of additional disk space will be used.
Selecting previously unselected package gcsfuse.
(Reading database ... 160772 files and directories currently installed.)
Preparing to unpack .../gcsfuse_0.35.1_amd64.deb ...
Unpacking gcsfuse (0.35.1) ...
Setting up gcsfuse (0.35.1) ...
2021/07/05 10:23:33.657719 Using mount point: /content/colab-sample-bucket-040005ec-dd7b-11eb-90f2-0242ac1c0002
2021/07/05 10:23:33.665572 Opening GCS connection...
2021/07/05 10:23:34.233955 Mounting file system "colab-sample-bucket-040005ec-dd7b-11eb-90f2-0242ac1c0002"...
2021/07/05 10:23:34.270062 File system has been successfully mounted.
Creating a folder
Creating a file/object in the folder just created.
Read
As the bucket is already mounted you can just access files by opening them using files
or just double clicking the file
Update
Update to file
Update can be done normally either by editing or by using python.
Update to folder
New files can be added either by colab's UI or by using python or any other way that works for regular dirs.
Delete
rm
cmd can be used to delete the objects of the bucket using the mounted path.
Deleting the file {bucket_name}/test_mount_folder/test_mount_file.txt
to delete the folder we can sue rm -r
or -rm -rf
to force remove
Deleting a bucket
you can't delete a bucekt using gcsfuse, but you can unmount the mounted path and then can delete using any of the above mentioned ways.
Unmount the bucket
After unmounting, any changes in the local path will not be rendered in the cloud bucket.
deleting the bucket using gsutil.
Using GC UI
Bucketa can be also be accessed directly through Gcloud Consoles's UI.
Create
Bucket can be created in the following way.
In the Storage browser, click
CREATE BUCKET
Fill in the name of the bucket,as it is a sample bucket, other configs can be left as default
To create a folder, click
CREATE FOLDER
in the corresponding bucket's page.To upload files files to this folder, you can use
UPLOAD FILES
Read
Files cannot be read directly using UI,but their metadata and permissions can be viewed
Through UI we can download the files easily through
DOWNLOAD
button after choosing the needed object
Update
To update the bucket, we can upload files through UPLOAD FILES
button at a given path.
Delete
To delete any object in the bucket, we can use
DELETE
option.To delete the bucket, go back to the your project's browser and select the checkbox of the bucket you want to delete and click
DELETE
.