Search in sources :

Example 66 with Storage

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

the class SetPublicAccessPreventionEnforced method setPublicAccessPreventionEnforced.

public static void setPublicAccessPreventionEnforced(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);
    // Enforces public access prevention for the bucket
    bucket.toBuilder().setIamConfiguration(BucketInfo.IamConfiguration.newBuilder().setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.ENFORCED).build()).build().update();
    System.out.println("Public access prevention is set to enforced for " + bucketName);
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 67 with Storage

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

the class ChangeObjectCSEKtoKMS method changeObjectFromCSEKtoKMS.

public static void changeObjectFromCSEKtoKMS(String projectId, String bucketName, String objectName, String decryptionKey, String kmsKeyName) {
    // 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 Base64 encoded decryption key, which should be the same key originally used to encrypt
    // the object
    // String decryptionKey = "TIbv/fjexq+VmtXzAlc63J4z5kFmWJ6NdAPQulQBT7g=";
    // The name of the KMS key to manage this object with
    // String kmsKeyName =
    // "projects/your-project-id/locations/global/keyRings/your-key-ring/cryptoKeys/your-key";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    BlobId blobId = BlobId.of(bucketName, objectName);
    Storage.CopyRequest request = Storage.CopyRequest.newBuilder().setSource(blobId).setSourceOptions(Storage.BlobSourceOption.decryptionKey(decryptionKey)).setTarget(blobId, Storage.BlobTargetOption.kmsKeyName(kmsKeyName)).build();
    storage.copy(request);
    System.out.println("Object " + objectName + " in bucket " + bucketName + " is now managed by the KMS key " + kmsKeyName + " instead of a customer-supplied encryption key");
}
Also used : Storage(com.google.cloud.storage.Storage) BlobId(com.google.cloud.storage.BlobId)

Example 68 with Storage

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

the class ComposeObject method composeObject.

public static void composeObject(String bucketName, String firstObjectName, String secondObjectName, String targetObjectName, String projectId) {
    // 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 the first GCS object to compose
    // String firstObjectName = "your-first-object-name";
    // The ID of the second GCS object to compose
    // String secondObjectName = "your-second-object-name";
    // The ID to give the new composite object
    // String targetObjectName = "new-composite-object-name";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Storage.ComposeRequest composeRequest = Storage.ComposeRequest.newBuilder().addSource(firstObjectName, secondObjectName).setTarget(BlobInfo.newBuilder(bucketName, targetObjectName).build()).build();
    Blob compositeObject = storage.compose(composeRequest);
    System.out.println("New composite object " + compositeObject.getName() + " was created by combining " + firstObjectName + " and " + secondObjectName);
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage)

Example 69 with Storage

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

the class DeleteOldVersionOfObject method deleteOldVersionOfObject.

public static void deleteOldVersionOfObject(String projectId, String bucketName, String objectName, long generationToDelete) {
    // 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 generation of objectName to delete
    // long generationToDelete = 1579287380533984;
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    storage.delete(BlobId.of(bucketName, objectName, generationToDelete));
    System.out.println("Generation " + generationToDelete + " of object " + objectName + " was deleted from " + bucketName);
}
Also used : Storage(com.google.cloud.storage.Storage)

Example 70 with Storage

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

the class DeleteObject method deleteObject.

public static void deleteObject(String projectId, String bucketName, String objectName) {
    // 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";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    storage.delete(bucketName, objectName);
    System.out.println("Object " + objectName + " was deleted from " + bucketName);
}
Also used : Storage(com.google.cloud.storage.Storage)

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