Using JSON keys with google cloud gsutil

google-cloud-platformgoogle-cloud-storagegsutil

I have a ssh private key in a key.json file, and I want to use this credential to access a storage bucket using gsutil.

I can't seem to find anything about how to include json keys as a authentication method, only "private" and "secret" fields.

The file structure is:

{
  "private_key_id":
  "private_key": "-----BEGIN PRIVATE KEY-- ...
   "client_email":
   "client_id":
  "type": "service_account"
}

How do I use gsutil with that file?

Best Answer

The short version is to run the following command and follow instructions:

gsutil config -e

The gsutil tool has built-in help which can be consulted for all kinds of options and modes of operation. When running gsutil help creds, one of the help options recommended when running gsutil alone, we can read the section on "OAuth2 Service Account" to see the instructions for using a service account's json key file:

OAuth2 Service Account:

This is the preferred type of credential to use when authenticating on
behalf of a service or application (as opposed to a user). For example, if
you will run gsutil out of a nightly cron job to upload/download data,
using a service account allows the cron job not to depend on credentials of
an individual employee at your company. This is the type of credential that
will be configured when you run "gsutil config -e".

It is important to note that a service account is considered an Editor by
default for the purposes of API access, rather than an Owner. In particular,
the fact that Editors have OWNER access in the default object and
bucket ACLs, but the canned ACL options remove OWNER access from
Editors, can lead to unexpected results. The solution to this problem is to
ensure the service account is an Owner in the Permissions tab for your
project. To find the email address of your service account, visit the
`Google Developers Console <https://cloud.google.com/console#/project>`_,
click on the project you're using, click "APIs & auth", and click
"Credentials".

To create a service account, visit the Google Developers Console and then:

   - Click "APIs & auth" in the left sidebar.

   - Click "Credentials".

   - Click "Create New Client ID".

   - Select "Service Account" as your application type.

   - Save the JSON private key or the .p12 private key and password
     provided.

For further information about account roles, see:
  https://developers.google.com/console/help/#DifferentRoles

For more details about OAuth2 service accounts, see:
  https://developers.google.com/accounts/docs/OAuth2ServiceAccount