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);
}
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");
}
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);
}
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);
}
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);
}
Aggregations