Search in sources :

Example 1 with ServiceAccount

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

the class StorageSnippets method createHmacKey.

/**
 * Example of creating an HMAC key for a service account *
 */
public HmacKey createHmacKey(String serviceAccountEmail, String projectId) throws StorageException {
    // [START storage_create_hmac_key]
    // Instantiate a Google Cloud Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();
    // The service account email for which the new HMAC key will be created.
    // String serviceAccountEmail = "service-account@iam.gserviceaccount.com";
    // 
    // The ID of the project to which the service account belongs.
    // String projectId = "project-id";
    ServiceAccount account = ServiceAccount.of(serviceAccountEmail);
    HmacKey hmacKey = storage.createHmacKey(account, Storage.CreateHmacKeyOption.projectId(projectId));
    String secret = hmacKey.getSecretKey();
    HmacKeyMetadata metadata = hmacKey.getMetadata();
    System.out.println("The Base64 encoded secret is: " + secret);
    System.out.println("Do not miss that secret, there is no API to recover it.");
    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_create_hmac_key]
    return hmacKey;
}
Also used : HmacKeyMetadata(com.google.cloud.storage.HmacKey.HmacKeyMetadata) ServiceAccount(com.google.cloud.storage.ServiceAccount) Storage(com.google.cloud.storage.Storage) HmacKey(com.google.cloud.storage.HmacKey) Date(java.util.Date)

Example 2 with ServiceAccount

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

the class GetServiceAccount method getServiceAccount.

public static void getServiceAccount(String projectId) {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    ServiceAccount serviceAccount = storage.getServiceAccount(projectId);
    System.out.println("The GCS service account for project " + projectId + " is: " + serviceAccount.getEmail());
}
Also used : ServiceAccount(com.google.cloud.storage.ServiceAccount) Storage(com.google.cloud.storage.Storage)

Aggregations

ServiceAccount (com.google.cloud.storage.ServiceAccount)2 Storage (com.google.cloud.storage.Storage)2 HmacKey (com.google.cloud.storage.HmacKey)1 HmacKeyMetadata (com.google.cloud.storage.HmacKey.HmacKeyMetadata)1 Date (java.util.Date)1