Search in sources :

Example 26 with Storage

use of com.google.cloud.storage.Storage 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 27 with Storage

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

the class StorageSnippets method getDefaultEventBasedHold.

/**
 * Example of how to get default event-based hold for a bucket
 */
public Bucket getDefaultEventBasedHold(String bucketName) throws StorageException {
    // [START storage_get_default_event_based_hold]
    // Instantiate a Google Cloud Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();
    // The name of a bucket, e.g. "my-bucket"
    // String bucketName = "my-bucket";
    Bucket bucket = storage.get(bucketName, BucketGetOption.fields(BucketField.DEFAULT_EVENT_BASED_HOLD));
    if (bucket.getDefaultEventBasedHold() != null && bucket.getDefaultEventBasedHold()) {
        System.out.println("Default event-based hold is enabled for " + bucketName);
    } else {
        System.out.println("Default event-based hold is not enabled for " + bucketName);
    }
    // [END storage_get_default_event_based_hold]
    return bucket;
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 28 with Storage

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

the class StorageSnippets method deactivateHmacKey.

/**
 * Example of deactivating an HMAC key. *
 */
public HmacKeyMetadata deactivateHmacKey(String accessId, String projectId) throws StorageException {
    // [START storage_deactivate_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.INACTIVE);
    System.out.println("The HMAC key is now inactive.");
    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_deactivate_hmac_key]
    return newMetadata;
}
Also used : HmacKeyMetadata(com.google.cloud.storage.HmacKey.HmacKeyMetadata) Storage(com.google.cloud.storage.Storage) Date(java.util.Date)

Example 29 with Storage

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

the class StorageSnippets method setTemporaryHold.

/**
 * Example of how to set a temporary hold for a blob
 */
public Blob setTemporaryHold(String bucketName, String blobName) throws StorageException {
    // [START storage_set_temporary_hold]
    // Instantiate a Google Cloud Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();
    // The name of a bucket, e.g. "my-bucket"
    // String bucketName = "my-bucket";
    // The name of a blob, e.g. "my-blob"
    // String blobName = "my-blob";
    BlobId blobId = BlobId.of(bucketName, blobName);
    Blob blob = storage.update(BlobInfo.newBuilder(blobId).setTemporaryHold(true).build());
    System.out.println("Temporary hold was set for " + blobName);
    // [END storage_set_temporary_hold]
    return blob;
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) BlobId(com.google.cloud.storage.BlobId)

Example 30 with Storage

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

the class GenerateSignedPostPolicyV4 method generateSignedPostPolicyV4.

/**
 * Generating a signed POST policy requires Credentials which implement ServiceAccountSigner.
 * These can be set explicitly using the Storage.PostPolicyV4Option.signWith(ServiceAccountSigner)
 * option. If you don't, you could also pass a service account signer to StorageOptions, i.e.
 * StorageOptions().newBuilder().setCredentials(ServiceAccountSignerCredentials). In this example,
 * neither of these options are used, which means the following code only works when the
 * credentials are defined via the environment variable GOOGLE_APPLICATION_CREDENTIALS, and those
 * credentials are authorized to sign a policy. See the documentation for
 * Storage.generateSignedPostPolicyV4 for more details.
 */
public static void generateSignedPostPolicyV4(String projectId, String bucketName, String blobName) {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    // The ID of the GCS bucket to upload to
    // String bucketName = "your-bucket-name"
    // The name to give the object uploaded to GCS
    // String blobName = "your-object-name"
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    PostPolicyV4.PostFieldsV4 fields = PostPolicyV4.PostFieldsV4.newBuilder().AddCustomMetadataField("test", "data").build();
    PostPolicyV4 policy = storage.generateSignedPostPolicyV4(BlobInfo.newBuilder(bucketName, blobName).build(), 10, TimeUnit.MINUTES, fields);
    StringBuilder htmlForm = new StringBuilder("<form action='" + policy.getUrl() + "' method='POST' enctype='multipart/form-data'>\n");
    for (Map.Entry<String, String> entry : policy.getFields().entrySet()) {
        htmlForm.append("  <input name='" + entry.getKey() + "' value='" + entry.getValue() + "' type='hidden' />\n");
    }
    htmlForm.append("  <input type='file' name='file'/><br />\n");
    htmlForm.append("  <input type='submit' value='Upload File'/><br />\n");
    htmlForm.append("</form>\n");
    System.out.println("You can use the following HTML form to upload an object to bucket " + bucketName + " for the next ten minutes:");
    System.out.println(htmlForm.toString());
}
Also used : PostPolicyV4(com.google.cloud.storage.PostPolicyV4) Storage(com.google.cloud.storage.Storage) Map(java.util.Map)

Aggregations

Storage (com.google.cloud.storage.Storage)341 Blob (com.google.cloud.storage.Blob)123 Bucket (com.google.cloud.storage.Bucket)91 BlobId (com.google.cloud.storage.BlobId)74 Test (org.junit.Test)65 BlobInfo (com.google.cloud.storage.BlobInfo)57 IOException (java.io.IOException)26 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)23 ArrayList (java.util.ArrayList)23 Before (org.junit.Before)22 PrintStream (java.io.PrintStream)18 Acl (com.google.cloud.storage.Acl)16 Path (java.nio.file.Path)16 StorageException (com.google.cloud.storage.StorageException)14 StorageOptions (com.google.cloud.storage.StorageOptions)13 TestRunner (org.apache.nifi.util.TestRunner)12 WriteChannel (com.google.cloud.WriteChannel)11 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)10 File (java.io.File)10