Search in sources :

Example 6 with HmacKeyMetadata

use of com.google.cloud.storage.HmacKey.HmacKeyMetadata in project google-cloud-java by GoogleCloudPlatform.

the class StorageSnippets method getHmacKey.

/**
 * Example of retrieving the metadata of an existing HMAC key. *
 */
public HmacKeyMetadata getHmacKey(String accessId, String projectId) throws StorageException {
    // [START storage_get_hmac_key]
    // Instantiate a Google Cloud Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();
    // The access ID of the HMAC key, e.g. "GOOG0234230X00"
    // String accessId = "GOOG0234230X00";
    // 
    // The ID of the project to which the service account belongs.
    // String projectId = "project-id";
    HmacKeyMetadata metadata = storage.getHmacKey(accessId, Storage.GetHmacKeyOption.projectId(projectId));
    System.out.println("The HMAC key metadata is:");
    System.out.println("ID: " + metadata.getId());
    System.out.println("Access ID: " + metadata.getAccessId());
    System.out.println("Project ID: " + metadata.getProjectId());
    System.out.println("Service Account Email: " + metadata.getServiceAccount().getEmail());
    System.out.println("State: " + metadata.getState().toString());
    System.out.println("Time Created: " + new Date(metadata.getCreateTime()).toString());
    System.out.println("Time Updated: " + new Date(metadata.getUpdateTime()).toString());
    System.out.println("ETag: " + metadata.getEtag());
    // [END storage_get_hmac_key]
    return metadata;
}
Also used : HmacKeyMetadata(com.google.cloud.storage.HmacKey.HmacKeyMetadata) Storage(com.google.cloud.storage.Storage) Date(java.util.Date)

Example 7 with HmacKeyMetadata

use of com.google.cloud.storage.HmacKey.HmacKeyMetadata in project google-cloud-java by GoogleCloudPlatform.

the class StorageSnippets method activateHmacKey.

/**
 * Example of activating a previously deactivated HMAC key. *
 */
public HmacKeyMetadata activateHmacKey(String accessId, String projectId) throws StorageException {
    // [START storage_activate_hmac_key]
    // Instantiate a Google Cloud Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();
    // The access ID of the HMAC key, e.g. "GOOG0234230X00"
    // String accessId = "GOOG0234230X00";
    // 
    // The ID of the project to which the service account belongs.
    // String projectId = "project-id";
    HmacKeyMetadata metadata = storage.getHmacKey(accessId, Storage.GetHmacKeyOption.projectId(projectId));
    HmacKeyMetadata newMetadata = storage.updateHmacKeyState(metadata, HmacKeyState.ACTIVE);
    System.out.println("The HMAC key is now active.");
    System.out.println("The HMAC key metadata is:");
    System.out.println("ID: " + newMetadata.getId());
    System.out.println("Access ID: " + newMetadata.getAccessId());
    System.out.println("Project ID: " + newMetadata.getProjectId());
    System.out.println("Service Account Email: " + newMetadata.getServiceAccount().getEmail());
    System.out.println("State: " + newMetadata.getState().toString());
    System.out.println("Time Created: " + new Date(newMetadata.getCreateTime()).toString());
    System.out.println("Time Updated: " + new Date(newMetadata.getUpdateTime()).toString());
    System.out.println("ETag: " + newMetadata.getEtag());
    // [END storage_activate_hmac_key]
    return newMetadata;
}
Also used : HmacKeyMetadata(com.google.cloud.storage.HmacKey.HmacKeyMetadata) Storage(com.google.cloud.storage.Storage) Date(java.util.Date)

Example 8 with HmacKeyMetadata

use of com.google.cloud.storage.HmacKey.HmacKeyMetadata in project google-cloud-java by GoogleCloudPlatform.

the class StorageSnippets method listHmacKeys.

public Page<HmacKeyMetadata> listHmacKeys(String projectId) throws StorageException {
    // [START storage_list_hmac_keys]
    // Instantiate a Google Cloud Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();
    // The ID of the project to which the service account belongs.
    // String projectId = "project-id";
    Page<HmacKeyMetadata> page = storage.listHmacKeys(ListHmacKeysOption.projectId(projectId));
    for (HmacKeyMetadata metadata : page.iterateAll()) {
        System.out.println("Service Account Email: " + metadata.getServiceAccount().getEmail());
        System.out.println("Access ID: " + metadata.getAccessId());
    }
    // [END storage_list_hmac_keys]
    return page;
}
Also used : HmacKeyMetadata(com.google.cloud.storage.HmacKey.HmacKeyMetadata) Storage(com.google.cloud.storage.Storage)

Example 9 with HmacKeyMetadata

use of com.google.cloud.storage.HmacKey.HmacKeyMetadata in project google-cloud-java by GoogleCloudPlatform.

the class StorageSnippets method deleteHmacKey.

/**
 * Example of deleting an existing inactive HMAC key. *
 */
public void deleteHmacKey(String accessId, String projectId) throws StorageException {
    // [START storage_delete_hmac_key]
    // Instantiate a Google Cloud Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();
    // The access ID of the HMAC key, e.g. "GOOG0234230X00"
    // String accessId = "GOOG0234230X00";
    // 
    // The ID of the project to which the service account belongs.
    // String projectId = "project-id";
    HmacKeyMetadata metadata = storage.getHmacKey(accessId, Storage.GetHmacKeyOption.projectId(projectId));
    storage.deleteHmacKey(metadata);
    System.out.println("The key is deleted, though it will still appear in getHmacKeys() results given showDeletedKey is true.");
// [END storage_delete_hmac_key]
}
Also used : HmacKeyMetadata(com.google.cloud.storage.HmacKey.HmacKeyMetadata) Storage(com.google.cloud.storage.Storage)

Example 10 with HmacKeyMetadata

use of com.google.cloud.storage.HmacKey.HmacKeyMetadata in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageHmacKeySnippets method testDeactivateHmacKey.

@Test
public void testDeactivateHmacKey() {
    HmacKey hmacKey = storage.createHmacKey(ServiceAccount.of(HMAC_KEY_TEST_SERVICE_ACCOUNT));
    HmacKeyMetadata metadata = storageSnippets.deactivateHmacKey(hmacKey.getMetadata().getAccessId(), PROJECT_ID);
    assertEquals(HmacKeyState.INACTIVE, metadata.getState());
}
Also used : HmacKeyMetadata(com.google.cloud.storage.HmacKey.HmacKeyMetadata) HmacKey(com.google.cloud.storage.HmacKey) Test(org.junit.Test)

Aggregations

HmacKeyMetadata (com.google.cloud.storage.HmacKey.HmacKeyMetadata)11 Storage (com.google.cloud.storage.Storage)6 HmacKey (com.google.cloud.storage.HmacKey)5 Test (org.junit.Test)5 Date (java.util.Date)4 ServiceAccount (com.google.cloud.storage.ServiceAccount)1