{"id":76,"date":"2022-05-12T21:52:56","date_gmt":"2022-05-12T21:52:56","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/pimode\/?p=76"},"modified":"2022-05-12T21:52:56","modified_gmt":"2022-05-12T21:52:56","slug":"accessing-google-cloud-storage","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/pimode\/2022\/05\/12\/accessing-google-cloud-storage\/","title":{"rendered":"Accessing Google Cloud Storage"},"content":{"rendered":"<figure class=\"wp-block-post-featured-image\"><img loading=\"lazy\" decoding=\"async\" width=\"724\" height=\"603\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/CloudData.png\" class=\"attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" style=\"object-fit:cover;\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/CloudData.png 724w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/CloudData-300x250.png 300w\" sizes=\"auto, (max-width: 724px) 100vw, 724px\" \/><\/figure>\n\n\n<p>Last week, I wrote about setting up a Google Cloud Storage bucket. This week, I\u2019m writing about how to access the files in your bucket.<\/p>\n\n\n\n<p>Once you have a Google Cloud Storage bucket set up, you can access it through the browser. This is an easy way to upload and download files and folders <em>from<\/em> and <em>to<\/em> your computer, but it\u2019s not very developer friendly.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"412\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/storage-browser-upload-1024x412.png\" alt=\"\" class=\"wp-image-78\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/storage-browser-upload-1024x412.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/storage-browser-upload-300x121.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/storage-browser-upload-768x309.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/storage-browser-upload-1536x618.png 1536w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/storage-browser-upload.png 1964w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>The browser menu allows you to upload, download, and delete files and directories.<\/figcaption><\/figure><\/div>\n\n\n\n<p>Two other ways to easily access the bucket are through the command line or through a script with Python (or C++, C#, Go, Java, Node.js, PHP, or Ruby). Installing the Google Cloud CLI is a snap and I found using the Python library was also straightforward. The trickiest part is setting up the authentication, so I\u2019m going to walk through some of the basics to get you started.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>To use the CLI:<\/strong><\/h3>\n\n\n\n<p>First, install the Google Cloud CLI, by following the instructions <a href=\"https:\/\/cloud.google.com\/sdk\/docs\/install\">here<\/a>.<\/p>\n\n\n\n<p>Now, you\u2019ll need to initialize the CLI by following the instructions <a href=\"https:\/\/cloud.google.com\/sdk\/docs\/initializing\">here<\/a>.<\/p>\n\n\n\n<p>From the command line, you have two options for authorization:<\/p>\n\n\n\n<p class=\"has-text-align-center\"><kbd><strong>gcloud auth login<\/strong><\/kbd><\/p>\n\n\n\n<p>I would recommend this option if you don\u2019t think that you\u2019ll access the bucket often. This will just open a browser window where you\u2019ll sign into your Google account using single sign-on.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><kbd><strong>gcloud auth activate-service account<\/strong><\/kbd><\/p>\n\n\n\n<p>This requires setting up a service account, which takes more time, but once you\u2019re set up, it won\u2019t ask you again. <em>Details on setting up a service account will be outlined in the steps for accessing the bucket through Python.<\/em><\/p>\n\n\n\n<p>Either of these options should automatically connect you with your new storage bucket unless you have other gcloud projects out there. If you have other projects, you\u2019ll have the extra step of selecting the current project.<\/p>\n\n\n\n<p>Once you\u2019ve set up authorization and initialized the tools, you\u2019ll be able to use <kbd><strong>gsutil<\/strong><\/kbd>. The main documentation is <a href=\"https:\/\/cloud.google.com\/storage\/docs\/gsutil\">here<\/a>.<\/p>\n\n\n\n<p>Since you\u2019ll mostly want to use this for uploading and downloading files, we\u2019ll take a quick look at the <kbd><strong>cp<\/strong><\/kbd> command. More detailed instructions are <a href=\"https:\/\/cloud.google.com\/storage\/docs\/gsutil\/commands\/cp\">here<\/a>, but let\u2019s go over a simple upload and download scenario.<\/p>\n\n\n\n<p>I highly recommend using the <kbd>-m<\/kbd> flag if you are uploading or downloading an entire directory. Per Google\u2019s documentation, it <em>performs a parallel multi-threaded\/multiprocessing copy<\/em>, and from my experience, it is a lot faster.<\/p>\n\n\n\n<p><strong>To upload a directory (<kbd>-r<\/kbd> flag for directory files)<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ngsutil -m cp -r local-path gs:\/\/my-storage-bucket\/storage-directory-path\n<\/pre><\/div>\n\n\n<p><strong>To download a file (we just switch the local path with the bucket path):<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ngsutil -m cp -r gs:\/\/my-storage-bucket\/my-file local-directory-path\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>To access bucket files in your code (I\u2019m using Python):<\/strong><\/h3>\n\n\n\n<p>You\u2019ll need to set up a service account with Google Cloud Platform. We start by going to the Google Cloud console:<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/console.cloud.google.com\">https:\/\/console.cloud.google.com<\/a><\/p>\n\n\n\n<p>At the dashboard or home page, we want to select <strong>IAM &amp; Admin<\/strong>. We can do this by searching with the search bar or using the hamburger menu in the left-hand corner.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/google-menu-service-account-672x1024.png\" alt=\"\" class=\"wp-image-79\" width=\"404\" height=\"615\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/google-menu-service-account-672x1024.png 672w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/google-menu-service-account-197x300.png 197w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/google-menu-service-account-768x1170.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/google-menu-service-account.png 962w\" sizes=\"auto, (max-width: 404px) 100vw, 404px\" \/><figcaption>Use the hamburger menu to select IAM &amp; Admin and then select Service Accounts<\/figcaption><\/figure><\/div>\n\n\n\n<p>Click the <strong>Create Service Account<\/strong> button.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"352\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/create-service-account-1024x352.png\" alt=\"\" class=\"wp-image-81\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/create-service-account-1024x352.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/create-service-account-300x103.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/create-service-account-768x264.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/create-service-account-1536x527.png 1536w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/create-service-account.png 1788w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Click the Create Service Account button<\/figcaption><\/figure><\/div>\n\n\n\n<p>Add a service account name (the service account id will autofill). Then add a brief description and click <strong>Create and Continue<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/service-account-name-974x1024.png\" alt=\"\" class=\"wp-image-80\" width=\"414\" height=\"435\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/service-account-name-974x1024.png 974w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/service-account-name-285x300.png 285w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/service-account-name-768x808.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/service-account-name.png 1126w\" sizes=\"auto, (max-width: 414px) 100vw, 414px\" \/><figcaption>Give the new Service Account a name and description<\/figcaption><\/figure><\/div>\n\n\n\n<p>You\u2019ll select a role &#8212; probably Editor or Owner. Then click <strong>Continue<\/strong>. Optionally, you can grant others access to this service account. Click <strong>Done<\/strong>.<\/p>\n\n\n\n<p>You should see your new service account listed. If you are using this for gsutil (the command line tool), you can stop here. If you want to access your bucket through Python, keep reading.<\/p>\n\n\n\n<p>Click on the 3 dots to the right and select <strong>Manage Keys<\/strong> from the drop-down menu.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"231\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/3-dots-1024x231.png\" alt=\"\" class=\"wp-image-82\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/3-dots-1024x231.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/3-dots-300x68.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/3-dots-768x173.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/3-dots-1536x346.png 1536w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/3-dots-2048x461.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Click the 3 stacked dots under Actions<\/figcaption><\/figure><\/div>\n\n\n\n<p>Click the <strong>Add Key<\/strong> button and select <strong>Create new key<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/create-new-key-1024x675.png\" alt=\"\" class=\"wp-image-83\" width=\"462\" height=\"304\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/create-new-key-1024x675.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/create-new-key-300x198.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/create-new-key-768x506.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5421\/files\/2022\/05\/create-new-key.png 1044w\" sizes=\"auto, (max-width: 462px) 100vw, 462px\" \/><figcaption>Click the Add Key button and select Create new key<\/figcaption><\/figure><\/div>\n\n\n\n<p>Select the <strong>JSON<\/strong> key type and <strong>Create<\/strong>. This will automatically download the key locally. Save this key somewhere you\u2019ll remember. You\u2019ll need the path later!<\/p>\n\n\n\n<p>Use your favorite way of managing environment variables. I usually use a <a href=\"https:\/\/pypi.org\/project\/python-dotenv\/\">dot-env<\/a> file. You will need to set up an environment variable like this:<\/p>\n\n\n\n<p><code>GOOGLE_APPLICATION_CREDENTIALS='\/path\/to\/my\/json\/key\/apple-pi-1234b567t2b.json'<\/code><\/p>\n\n\n\n<p>You\u2019ll also need to install google-storage for your python project:<\/p>\n\n\n\n<p class=\"has-text-align-center\"><code>pip install google-cloud-storage<\/code><\/p>\n\n\n\n<p>And then import the storage library:<\/p>\n\n\n\n<p class=\"has-text-align-center\"><code><strong>from <\/strong>google.cloud <strong>import <\/strong>storage<\/code><\/p>\n\n\n\n<p>More information on how to use Google Storage in Python: <\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/cloud.google.com\/storage\/docs\/downloading-objects\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/cloud.google.com\/storage\/docs\/downloading-objects<\/a><\/p>\n\n\n\n<p>I find it\u2019s easier to search for what I want through Stack Overflow because the GCP docs can be hard to navigate. <\/p>\n\n\n\n<p>Now that you&#8217;re set up to use Cloud Storage, it&#8217;s time to get coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week, I wrote about setting up a Google Cloud Storage bucket. This week, I\u2019m writing about how to access the files in your bucket. Once you have a Google Cloud Storage bucket set up, you can access it through the browser. This is an easy way to upload and download files and folders from [&hellip;]<\/p>\n","protected":false},"author":12249,"featured_media":77,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-76","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/pimode\/wp-json\/wp\/v2\/posts\/76","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/pimode\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/pimode\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/pimode\/wp-json\/wp\/v2\/users\/12249"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/pimode\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/pimode\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":84,"href":"https:\/\/blogs.oregonstate.edu\/pimode\/wp-json\/wp\/v2\/posts\/76\/revisions\/84"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/pimode\/wp-json\/wp\/v2\/media\/77"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/pimode\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/pimode\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/pimode\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}