use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class ConfigureBucketCors method configureBucketCors.
public static void configureBucketCors(String projectId, String bucketName, String origin, String responseHeader, Integer maxAgeSeconds) {
// The ID of your GCP project
// String projectId = "your-project-id";
// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";
// The origin for this CORS config to allow requests from
// String origin = "http://example.appspot.com";
// The response header to share across origins
// String responseHeader = "Content-Type";
// The maximum amount of time the browser can make requests before it must repeat preflighted
// requests
// Integer maxAgeSeconds = 3600;
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Bucket bucket = storage.get(bucketName);
// See the HttpMethod documentation for other HTTP methods available:
// https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/urlfetch/HTTPMethod
HttpMethod method = HttpMethod.GET;
Cors cors = Cors.newBuilder().setOrigins(ImmutableList.of(Cors.Origin.of(origin))).setMethods(ImmutableList.of(method)).setResponseHeaders(ImmutableList.of(responseHeader)).setMaxAgeSeconds(maxAgeSeconds).build();
bucket.toBuilder().setCors(ImmutableList.of(cors)).build().update();
System.out.println("Bucket " + bucketName + " was updated with a CORS config to allow GET requests from " + origin + " sharing " + responseHeader + " responses across origins");
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class CreateBucketWithStorageClassAndLocation method createBucketWithStorageClassAndLocation.
public static void createBucketWithStorageClassAndLocation(String projectId, String bucketName) {
// The ID of your GCP project
// String projectId = "your-project-id";
// The ID to give your GCS bucket
// String bucketName = "your-unique-bucket-name";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
// See the StorageClass documentation for other valid storage classes:
// https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/StorageClass.html
StorageClass storageClass = StorageClass.COLDLINE;
// See this documentation for other valid locations:
// http://g.co/cloud/storage/docs/bucket-locations#location-mr
String location = "ASIA";
Bucket bucket = storage.create(BucketInfo.newBuilder(bucketName).setStorageClass(storageClass).setLocation(location).build());
System.out.println("Created bucket " + bucket.getName() + " in " + bucket.getLocation() + " with storage class " + bucket.getStorageClass());
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class EnableBucketVersioning method enableBucketVersioning.
public static void enableBucketVersioning(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);
bucket.toBuilder().setVersioningEnabled(true).build().update();
System.out.println("Versioning is now enabled for bucket " + bucketName);
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class RemoveBucketDefaultKMSKey method removeBucketDefaultKmsKey.
public static void removeBucketDefaultKmsKey(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);
bucket.toBuilder().setDefaultKmsKeyName(null).build().update();
System.out.println("Default KMS key was removed from " + bucketName);
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class SetAsyncTurboRpo method setAsyncTurboRpo.
public static void setAsyncTurboRpo(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);
bucket.toBuilder().setRpo(Rpo.ASYNC_TURBO).build().update();
System.out.println("Turbo replication was enabled for " + bucketName);
}
Aggregations