Search in sources :

Example 46 with Storage

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

the class StorageSnippets method getUniformBucketLevelAccess.

/**
 * Example of how to get uniform bucket-level access metadata for a bucket
 */
public Bucket getUniformBucketLevelAccess(String bucketName) throws StorageException {
    // [START storage_get_uniform_bucket_level_access]
    // 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.IAMCONFIGURATION));
    BucketInfo.IamConfiguration iamConfiguration = bucket.getIamConfiguration();
    Boolean enabled = iamConfiguration.isUniformBucketLevelAccessEnabled();
    Date lockedTime = new Date(iamConfiguration.getUniformBucketLevelAccessLockedTime());
    if (enabled != null && enabled) {
        System.out.println("Uniform bucket-level access is enabled for " + bucketName);
        System.out.println("Bucket will be locked on " + lockedTime);
    } else {
        System.out.println("Uniform bucket-level access is disabled for " + bucketName);
    }
    // [END storage_get_uniform_bucket_level_access]
    return bucket;
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) BucketInfo(com.google.cloud.storage.BucketInfo) Date(java.util.Date)

Example 47 with Storage

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

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

the class CreateAndListBucketsAndBlobs method main.

public static void main(String... args) {
    // Create a service object
    // Credentials are inferred from the environment.
    Storage storage = StorageOptions.getDefaultInstance().getService();
    // Create a bucket
    // Change this to something unique
    String bucketName = "my_unique_bucket";
    Bucket bucket = storage.create(BucketInfo.of(bucketName));
    // Upload a blob to the newly created bucket
    Blob blob = bucket.create("my_blob_name", "a simple blob".getBytes(UTF_8), "text/plain");
    // Read the blob content from the server
    String blobContent = new String(blob.getContent(), UTF_8);
    // List all your buckets
    System.out.println("My buckets:");
    for (Bucket currentBucket : storage.list().iterateAll()) {
        System.out.println(currentBucket);
    }
    // List the blobs in a particular bucket
    System.out.println("My blobs:");
    for (Blob currentBlob : bucket.list().iterateAll()) {
        System.out.println(currentBlob);
    }
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 49 with Storage

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

the class UploadObjectFromMemory method uploadObjectFromMemory.

public static void uploadObjectFromMemory(String projectId, String bucketName, String objectName, String contents) throws IOException {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";
    // The ID of your GCS object
    // String objectName = "your-object-name";
    // The string of contents you wish to upload
    // String contents = "Hello world!";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    BlobId blobId = BlobId.of(bucketName, objectName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
    byte[] content = contents.getBytes(StandardCharsets.UTF_8);
    storage.createFrom(blobInfo, new ByteArrayInputStream(content));
    System.out.println("Object " + objectName + " uploaded to bucket " + bucketName + " with contents " + contents);
}
Also used : Storage(com.google.cloud.storage.Storage) ByteArrayInputStream(java.io.ByteArrayInputStream) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId)

Example 50 with Storage

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

the class GetBucketRpo method getBucketRpo.

public static void getBucketRpo(String projectId, String bucketName) {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Bucket bucket = storage.get(bucketName);
    String rpo = bucket.getRpo().toString();
    System.out.println("The RPO setting of bucket " + bucketName + " is " + rpo);
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Aggregations

Storage (com.google.cloud.storage.Storage)140 Bucket (com.google.cloud.storage.Bucket)45 Blob (com.google.cloud.storage.Blob)44 Test (org.junit.Test)30 BlobId (com.google.cloud.storage.BlobId)24 BlobInfo (com.google.cloud.storage.BlobInfo)14 TestRunner (org.apache.nifi.util.TestRunner)12 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)11 Policy (com.google.cloud.Policy)9 ArrayList (java.util.ArrayList)8 Acl (com.google.cloud.storage.Acl)7 Date (java.util.Date)7 HashMap (java.util.HashMap)7 BucketInfo (com.google.cloud.storage.BucketInfo)6 HmacKeyMetadata (com.google.cloud.storage.HmacKey.HmacKeyMetadata)6 MockFlowFile (org.apache.nifi.util.MockFlowFile)6 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)5 Binding (com.google.cloud.Binding)5 WriteChannel (com.google.cloud.WriteChannel)4 IOException (java.io.IOException)4