HmacKey

HmacKey

An HmacKey object contains metadata of an HMAC key created from a service account through the Storage client using Storage#createHmacKey.

See HMAC keys documentation

Constructor

new HmacKey(storage, accessId, options)

Constructs an HmacKey object.

Note: this only create a local reference to an HMAC key, to create an HMAC key, use Storage#createHmacKey.

Parameters:
Name Type Description
storage Storage

The Storage instance this HMAC key is attached to.

accessId string

The unique accessId for this HMAC key.

options HmacKeyOptions

Constructor configurations.

Example
```
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const hmacKey = storage.hmacKey('access-id');
```

Members

metadata

The API-formatted resource description of the HMAC key.

Note: This is not guaranteed to be up-to-date when accessed. To get the latest record, call the getMetadata() method.

Methods

delete(optionsopt, callbackopt) → {Promise.<DeleteHmacKeyResponse>}

Deletes an HMAC key. Key state must be set to INACTIVE prior to deletion. Caution: HMAC keys cannot be recovered once you delete them.

The authenticated user must have storage.hmacKeys.delete permission for the project in which the key exists.

Parameters:
Name Type Attributes Description
options DeleteHmacKeyOptions <optional>

Configuration options.

callback DeleteHmacKeyCallback <optional>

Callback function.

Returns:
Type Description
Promise.<DeleteHmacKeyResponse>
Example
```
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();

//-
// Delete HMAC key after making the key inactive.
//-
const hmacKey = storage.hmacKey('ACCESS_ID');
hmacKey.setMetadata({state: 'INACTIVE'}, (err, hmacKeyMetadata) => {
    if (err) {
      // The request was an error.
      console.error(err);
      return;
    }
    hmacKey.delete((err) => {
      if (err) {
        console.error(err);
        return;
      }
      // The HMAC key is deleted.
    });
  });

//-
// If the callback is omitted, a promise is returned.
//-
const hmacKey = storage.hmacKey('ACCESS_ID');
hmacKey
  .setMetadata({state: 'INACTIVE'})
  .then(() => {
    return hmacKey.delete();
  });
```

get(optionsopt, callbackopt) → {Promise.<GetHmacKeyResponse>}

Retrieves and populate an HMAC key's metadata, and return this HmacKey instance.

HmacKey.get() does not give the HMAC key secret, as it is only returned on creation.

The authenticated user must have storage.hmacKeys.get permission for the project in which the key exists.

Parameters:
Name Type Attributes Description
options GetHmacKeyOptions <optional>

Configuration options.

callback GetHmacKeyCallback <optional>

Callback function.

Returns:
Type Description
Promise.<GetHmacKeyResponse>
Example
```
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();

//-
// Get the HmacKey's Metadata.
//-
storage.hmacKey('ACCESS_ID')
  .get((err, hmacKey) => {
    if (err) {
      // The request was an error.
      console.error(err);
      return;
    }
    // do something with the returned HmacKey object.
  });

//-
// If the callback is omitted, a promise is returned.
//-
storage.hmacKey('ACCESS_ID')
  .get()
  .then((data) => {
    const hmacKey = data[0];
  });
```

getMetadata(optionsopt, callbackopt) → {Promise.<HmacKeyMetadataResponse>}

Retrieves and populate an HMAC key's metadata, and return the HMAC key's metadata as an object.

HmacKey.getMetadata() does not give the HMAC key secret, as it is only returned on creation.

The authenticated user must have storage.hmacKeys.get permission for the project in which the key exists.

Parameters:
Name Type Attributes Description
options GetHmacKeyMetadataOptions <optional>

Configuration options.

callback HmacKeyMetadataCallback <optional>

Callback function.

Returns:
Type Description
Promise.<HmacKeyMetadataResponse>
Example
```
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();

//-
// Get the HmacKey's metadata and populate to the metadata property.
//-
storage.hmacKey('ACCESS_ID')
  .getMetadata((err, hmacKeyMetadata) => {
    if (err) {
      // The request was an error.
      console.error(err);
      return;
    }
    console.log(hmacKeyMetadata);
  });

//-
// If the callback is omitted, a promise is returned.
//-
storage.hmacKey('ACCESS_ID')
  .getMetadata()
  .then((data) => {
    const hmacKeyMetadata = data[0];
    console.log(hmacKeyMetadata);
  });
```

setMetadata(metadata, optionsopt, callbackopt) → {Promise.<HmacKeyMetadataResponse>}

Updates the state of an HMAC key. See SetHmacKeyMetadata for valid states.

Parameters:
Name Type Attributes Description
metadata SetHmacKeyMetadata

The new metadata.

options SetHmacKeyMetadataOptions <optional>

Configuration options.

callback HmacKeyMetadataCallback <optional>

Callback function.

Returns:
Type Description
Promise.<HmacKeyMetadataResponse>
Example
```
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();

const metadata = {
  state: 'INACTIVE',
};

storage.hmacKey('ACCESS_ID')
  .setMetadata(metadata, (err, hmacKeyMetadata) => {
    if (err) {
      // The request was an error.
      console.error(err);
      return;
    }
    console.log(hmacKeyMetadata);
  });

//-
// If the callback is omitted, a promise is returned.
//-
storage.hmacKey('ACCESS_ID')
  .setMetadata(metadata)
  .then((data) => {
    const hmacKeyMetadata = data[0];
    console.log(hmacKeyMetadata);
  });
```