Search in sources :

Example 1 with Cors

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

the class ITBucketSnippets method testRemoveBucketCors.

@Test
public void testRemoveBucketCors() {
    storage.get(BUCKET).toBuilder().setCors(ImmutableList.of(Cors.newBuilder().setOrigins(ImmutableList.of(Cors.Origin.of("http://example.appspot.com"))).setMethods(ImmutableList.of(HttpMethod.GET)).setResponseHeaders(ImmutableList.of("Content-Type")).setMaxAgeSeconds(3600).build())).build().update();
    Cors cors = storage.get(BUCKET).getCors().get(0);
    assertNotNull(cors);
    assertTrue(cors.getOrigins().get(0).toString().contains("example.appspot.com"));
    assertTrue(cors.getResponseHeaders().contains("Content-Type"));
    assertEquals(3600, cors.getMaxAgeSeconds().intValue());
    assertTrue(cors.getMethods().get(0).toString().equalsIgnoreCase("GET"));
    RemoveBucketCors.removeBucketCors(PROJECT_ID, BUCKET);
    assertNull(storage.get(BUCKET).getCors());
}
Also used : Cors(com.google.cloud.storage.Cors) RemoveBucketCors(com.google.cloud.examples.storage.buckets.RemoveBucketCors) ConfigureBucketCors(com.google.cloud.examples.storage.buckets.ConfigureBucketCors) Test(org.junit.Test)

Example 2 with Cors

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

the class ITBucketSnippets method testConfigureBucketCors.

@Test
public void testConfigureBucketCors() {
    System.out.println(PROJECT_ID);
    ConfigureBucketCors.configureBucketCors(PROJECT_ID, BUCKET, "http://example.appspot.com", "Content-Type", 3600);
    Cors cors = storage.get(BUCKET).getCors().get(0);
    assertTrue(cors.getOrigins().get(0).toString().contains("example.appspot.com"));
    assertTrue(cors.getResponseHeaders().contains("Content-Type"));
    assertEquals(3600, cors.getMaxAgeSeconds().intValue());
    assertTrue(cors.getMethods().get(0).toString().equalsIgnoreCase("GET"));
}
Also used : Cors(com.google.cloud.storage.Cors) RemoveBucketCors(com.google.cloud.examples.storage.buckets.RemoveBucketCors) ConfigureBucketCors(com.google.cloud.examples.storage.buckets.ConfigureBucketCors) Test(org.junit.Test)

Example 3 with Cors

use of com.google.cloud.storage.Cors 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");
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) Cors(com.google.cloud.storage.Cors) HttpMethod(com.google.cloud.storage.HttpMethod)

Example 4 with Cors

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

the class RemoveBucketCors method removeBucketCors.

public static void removeBucketCors(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, Storage.BucketGetOption.fields(Storage.BucketField.CORS));
    // getCors() returns the List and copying over to an ArrayList so it's mutable.
    List<Cors> cors = new ArrayList<>(bucket.getCors());
    // Clear bucket CORS configuration.
    cors.clear();
    // Update bucket to remove CORS.
    bucket.toBuilder().setCors(cors).build().update();
    System.out.println("Removed CORS configuration from bucket " + bucketName);
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) ArrayList(java.util.ArrayList) Cors(com.google.cloud.storage.Cors)

Aggregations

Cors (com.google.cloud.storage.Cors)4 ConfigureBucketCors (com.google.cloud.examples.storage.buckets.ConfigureBucketCors)2 RemoveBucketCors (com.google.cloud.examples.storage.buckets.RemoveBucketCors)2 Bucket (com.google.cloud.storage.Bucket)2 Storage (com.google.cloud.storage.Storage)2 Test (org.junit.Test)2 HttpMethod (com.google.cloud.storage.HttpMethod)1 ArrayList (java.util.ArrayList)1