From b8b5e5cfb4f0b5d74d4c01c2b454799838a0b13b Mon Sep 17 00:00:00 2001 From: Sydney Munro Date: Mon, 28 Mar 2022 16:02:06 -0700 Subject: [PATCH 1/9] Adding PubSub Notification Samples --- samples/install-without-bom/pom.xml | 6 + samples/native-image-sample/pom.xml | 6 + samples/snapshot/pom.xml | 6 + samples/snippets/pom.xml | 6 + .../CreateBucketPubSubNotification.java | 40 +++++++ .../DeleteBucketPubSubNotification.java | 42 +++++++ .../bucket/ListPubSubNotifications.java | 40 +++++++ .../bucket/PrintPubSubNotification.java | 41 +++++++ .../CreateBucketPubSubNotificationTest.java | 87 +++++++++++++++ .../DeleteBucketPubSubNotificationTest.java | 104 ++++++++++++++++++ .../bucket/ListPubSubNotificationsTest.java | 88 +++++++++++++++ .../bucket/PrintPubSubNotificationTest.java | 88 +++++++++++++++ 12 files changed, 554 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java create mode 100644 samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java create mode 100644 samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java create mode 100644 samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java create mode 100644 samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java create mode 100644 samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java create mode 100644 samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java create mode 100644 samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml index 4ab5ccb9b2..ca89a15790 100644 --- a/samples/install-without-bom/pom.xml +++ b/samples/install-without-bom/pom.xml @@ -58,6 +58,12 @@ 4.5.13 test + + com.google.cloud + google-cloud-pubsub + 1.116.2 + test + diff --git a/samples/native-image-sample/pom.xml b/samples/native-image-sample/pom.xml index 36e2b626f8..a2aee6e3e7 100644 --- a/samples/native-image-sample/pom.xml +++ b/samples/native-image-sample/pom.xml @@ -58,6 +58,12 @@ 1.1.3 test + + com.google.cloud + google-cloud-pubsub + 1.116.2 + test + diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml index b890f1bafb..b8b167e46b 100644 --- a/samples/snapshot/pom.xml +++ b/samples/snapshot/pom.xml @@ -49,6 +49,12 @@ 4.5.13 test + + com.google.cloud + google-cloud-pubsub + 1.116.2 + test + diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index 54b99b747f..060d827f64 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -69,5 +69,11 @@ 4.5.13 test + + com.google.cloud + google-cloud-pubsub + 1.106.0 + test + diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java new file mode 100644 index 0000000000..f79fff8a00 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java @@ -0,0 +1,40 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.storage.bucket; + +// [START storage_create_bucket_notifications] + +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import com.google.cloud.storage.Notification; +import com.google.cloud.storage.NotificationInfo; + + +public class CreateBucketPubSubNotification { + + public static void createBucketPubSubNotification(String bucketName, NotificationInfo notificationInfo) { + // The ID to give your GCS bucket + // String bucketName = "your-unique-bucket-name"; + // The NotificationInfo for the notification you would like to create + // See: https://cloud.google.com/java/docs/reference/google-cloud-notification/latest/com.google.cloud.notification.NotificationInfo + Storage storage = StorageOptions.newBuilder().build().getService(); + Notification notification = storage.createNotification(bucketName, notificationInfo); + System.out.println("Successfully created notification for topic " + notification.getTopic()); + } +} +// [END storage_create_bucket_notifications] \ No newline at end of file diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java new file mode 100644 index 0000000000..c392d0266d --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java @@ -0,0 +1,42 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.storage.bucket; + +// [START storage_delete_bucket_notification] + +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; + + +public class DeleteBucketPubSubNotification { + + public static void deleteBucketPubSubNotification(String bucketName, String notificationId) { + // The ID to give your GCS bucket + // String bucketName = "your-unique-bucket-name"; + // The NotificationId for the notification you would like to delete\ + // String notificationId = "your-unique-notification-id" + Storage storage = StorageOptions.newBuilder().build().getService(); + boolean success = storage.deleteNotification(bucketName, notificationId); + if(success) { + System.out.println("Successfully deleted notification"); + } else { + System.out.println("Failed to find notification"); + } + } +} +// [END storage_delete_bucket_notification] \ No newline at end of file diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java b/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java new file mode 100644 index 0000000000..7bfb662a80 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java @@ -0,0 +1,40 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.storage.bucket; + +// [START storage_list_bucket_notifications] + +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import com.google.cloud.storage.Notification; +import java.util.List; + + +public class ListPubSubNotifications { + + public static void listPubSubNotifications(String bucketName) { + // The ID to give your GCS bucket + // String bucketName = "your-unique-bucket-name"; + Storage storage = StorageOptions.newBuilder().build().getService(); + List notificationList = storage.listNotifications(bucketName); + for(Notification notification : notificationList) { + System.out.println("Found notification " + notification.getTopic() + " for bucket "+ bucketName); + } + } +} +// [END storage_list_bucket_notifications] \ No newline at end of file diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java new file mode 100644 index 0000000000..402767767c --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java @@ -0,0 +1,41 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.storage.bucket; + +// [START storage_print_pubsub_bucket_notification] + +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import com.google.cloud.storage.Notification; +import java.util.List; + + +public class PrintPubSubNotification { + + public static void printPubSubNotification(String bucketName, String notificationId) { + // The ID to give your GCS bucket + // String bucketName = "your-unique-bucket-name"; + // The Pub/Sub topic you would like to find + // String notificationId = "your-unique-notification-id" + + Storage storage = StorageOptions.newBuilder().build().getService(); + Notification notification = storage.getNotification(bucketName, notificationId); + System.out.println("Found notification " + notification.getTopic() + " for bucket "+ bucketName); + } +} +// [END storage_print_pubsub_bucket_notification] \ No newline at end of file diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java new file mode 100644 index 0000000000..c3e4804337 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java @@ -0,0 +1,87 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.storage.bucket; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.cloud.pubsub.v1.TopicAdminClient; +import com.example.storage.TestBase; +import com.google.cloud.storage.Notification; +import com.google.cloud.storage.NotificationInfo; +import com.google.common.collect.ImmutableMap; +import com.google.iam.v1.Binding; +import com.google.iam.v1.GetIamPolicyRequest; +import com.google.iam.v1.SetIamPolicyRequest; +import java.io.IOException; +import java.util.Map; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class CreateBucketPubSubNotificationTest extends TestBase { + private static final Notification.PayloadFormat PAYLOAD_FORMAT = + Notification.PayloadFormat.JSON_API_V1.JSON_API_V1; + private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); + private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String TOPIC = String.format("projects/%s/topics/new-topic", PROJECT); + private static TopicAdminClient topicAdminClient; + + @BeforeClass + public static void configureTopicAdminClient() throws IOException { + if(PROJECT != null) { + topicAdminClient = TopicAdminClient.create(); + topicAdminClient.createTopic(TOPIC); + GetIamPolicyRequest getIamPolicyRequest = + GetIamPolicyRequest.newBuilder().setResource(TOPIC).build(); + com.google.iam.v1.Policy policy = topicAdminClient.getIamPolicy(getIamPolicyRequest); + Binding binding = + Binding.newBuilder().setRole("roles/owner").addMembers("allAuthenticatedUsers").build(); + SetIamPolicyRequest setIamPolicyRequest = + SetIamPolicyRequest.newBuilder() + .setResource(TOPIC) + .setPolicy(policy.toBuilder().addBindings(binding).build()) + .build(); + topicAdminClient.setIamPolicy(setIamPolicyRequest); + } + } + + @AfterClass + public static void deleteTopicAndClient() { + /* Delete the Pub/Sub topic */ + if (topicAdminClient != null) { + topicAdminClient.deleteTopic(TOPIC); + topicAdminClient.close(); + } + } + + @Test + public void testCreateBucketPubSubNotification() { + // Check that we can access project value and that topic admin client came up successfully + assertNotNull("Unable to determine project", PROJECT); + assertNotNull("Topic Admin Client did not start up", topicAdminClient); + + NotificationInfo notificationInfo = + NotificationInfo.newBuilder(TOPIC) + .setCustomAttributes(CUSTOM_ATTRIBUTES) + .setPayloadFormat(PAYLOAD_FORMAT) + .build(); + CreateBucketPubSubNotification.createBucketPubSubNotification(bucketName, notificationInfo); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(TOPIC); + } + +} \ No newline at end of file diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java new file mode 100644 index 0000000000..7c7093bbd2 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java @@ -0,0 +1,104 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.storage.bucket; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.cloud.pubsub.v1.TopicAdminClient; +import com.example.storage.TestBase; +import com.google.cloud.storage.Notification; +import com.google.cloud.storage.NotificationInfo; +import com.google.common.collect.ImmutableMap; +import com.google.iam.v1.Binding; +import com.google.iam.v1.GetIamPolicyRequest; +import com.google.iam.v1.SetIamPolicyRequest; +import java.io.IOException; +import java.util.Map; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class DeleteBucketPubSubNotificationTest extends TestBase { + + private static final Notification.PayloadFormat PAYLOAD_FORMAT = + Notification.PayloadFormat.JSON_API_V1.JSON_API_V1; + private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); + private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String TOPIC = String.format("projects/%s/topics/new-topic", PROJECT); + private static TopicAdminClient topicAdminClient; + private static final NotificationInfo notificationInfo = NotificationInfo.newBuilder(TOPIC) + .setCustomAttributes(CUSTOM_ATTRIBUTES) + .setPayloadFormat(PAYLOAD_FORMAT) + .build(); + + @BeforeClass + public static void configureTopicAdminClient() throws IOException { + if (PROJECT != null) { + topicAdminClient = TopicAdminClient.create(); + topicAdminClient.createTopic(TOPIC); + GetIamPolicyRequest getIamPolicyRequest = + GetIamPolicyRequest.newBuilder().setResource(TOPIC).build(); + com.google.iam.v1.Policy policy = topicAdminClient.getIamPolicy(getIamPolicyRequest); + Binding binding = + Binding.newBuilder().setRole("roles/owner").addMembers("allAuthenticatedUsers").build(); + SetIamPolicyRequest setIamPolicyRequest = + SetIamPolicyRequest.newBuilder() + .setResource(TOPIC) + .setPolicy(policy.toBuilder().addBindings(binding).build()) + .build(); + topicAdminClient.setIamPolicy(setIamPolicyRequest); + } + } + + @AfterClass + public static void deleteTopicAndClient() { + /* Delete the Pub/Sub topic */ + if (topicAdminClient != null) { + topicAdminClient.deleteTopic(TOPIC); + topicAdminClient.close(); + } + } + + @Test + public void testDeleteBucketPubSubNotification() { + // Check that we can access project value and that topic admin client came up successfully + assertNotNull("Unable to determine project", PROJECT); + assertNotNull("Topic Admin Client did not start up", topicAdminClient); + + Notification notification = storage.createNotification(bucketName, notificationInfo); + com.example.storage.bucket.DeleteBucketPubSubNotification.deleteBucketPubSubNotification(bucketName, + notification.getNotificationId()); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains( + "Successfully deleted notification"); + } + + @Test + public void testNotificationNotFound() { + // Check that we can access project value and that topic admin client came up successfully + assertNotNull("Unable to determine project", PROJECT); + assertNotNull("Topic Admin Client did not start up", topicAdminClient); + Notification notification = storage.createNotification(bucketName, notificationInfo); + // Do a delete first. + storage.deleteNotification(bucketName, notification.getNotificationId()); + // Now try to delete again. + com.example.storage.bucket.DeleteBucketPubSubNotification.deleteBucketPubSubNotification(bucketName, + notification.getNotificationId()); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("Failed to find notification"); + } + +} \ No newline at end of file diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java new file mode 100644 index 0000000000..45d8dbfbfe --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java @@ -0,0 +1,88 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.storage.bucket; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.cloud.pubsub.v1.TopicAdminClient; +import com.example.storage.TestBase; +import com.google.cloud.storage.Notification; +import com.google.cloud.storage.NotificationInfo; +import com.google.common.collect.ImmutableMap; +import com.google.iam.v1.Binding; +import com.google.iam.v1.GetIamPolicyRequest; +import com.google.iam.v1.SetIamPolicyRequest; +import java.io.IOException; +import java.util.Map; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ListPubSubNotificationsTest extends TestBase { + private static final Notification.PayloadFormat PAYLOAD_FORMAT = + Notification.PayloadFormat.JSON_API_V1.JSON_API_V1; + private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); + private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String TOPIC = String.format("projects/%s/topics/new-topic", PROJECT); + private static TopicAdminClient topicAdminClient; + + @BeforeClass + public static void configureTopicAdminClient() throws IOException { + if(PROJECT != null) { + topicAdminClient = TopicAdminClient.create(); + topicAdminClient.createTopic(TOPIC); + GetIamPolicyRequest getIamPolicyRequest = + GetIamPolicyRequest.newBuilder().setResource(TOPIC).build(); + com.google.iam.v1.Policy policy = topicAdminClient.getIamPolicy(getIamPolicyRequest); + Binding binding = + Binding.newBuilder().setRole("roles/owner").addMembers("allAuthenticatedUsers").build(); + SetIamPolicyRequest setIamPolicyRequest = + SetIamPolicyRequest.newBuilder() + .setResource(TOPIC) + .setPolicy(policy.toBuilder().addBindings(binding).build()) + .build(); + topicAdminClient.setIamPolicy(setIamPolicyRequest); + } + } + + @AfterClass + public static void deleteTopicAndClient() { + /* Delete the Pub/Sub topic */ + if (topicAdminClient != null) { + topicAdminClient.deleteTopic(TOPIC); + topicAdminClient.close(); + } + } + + @Test + public void testListBucketPubSubNotification() { + // Check that we can access project value and that topic admin client came up successfully + assertNotNull("Unable to determine project", PROJECT); + assertNotNull("Topic Admin Client did not start up", topicAdminClient); + + NotificationInfo notificationInfo = + NotificationInfo.newBuilder(TOPIC) + .setCustomAttributes(CUSTOM_ATTRIBUTES) + .setPayloadFormat(PAYLOAD_FORMAT) + .build(); + storage.createNotification(bucketName, notificationInfo); + ListPubSubNotifications.listPubSubNotifications(bucketName); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(TOPIC); + } + +} \ No newline at end of file diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java new file mode 100644 index 0000000000..e71492b60e --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java @@ -0,0 +1,88 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.storage.bucket; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.cloud.pubsub.v1.TopicAdminClient; +import com.example.storage.TestBase; +import com.google.cloud.storage.Notification; +import com.google.cloud.storage.NotificationInfo; +import com.google.common.collect.ImmutableMap; +import com.google.iam.v1.Binding; +import com.google.iam.v1.GetIamPolicyRequest; +import com.google.iam.v1.SetIamPolicyRequest; +import java.io.IOException; +import java.util.Map; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class PrintPubSubNotificationTest extends TestBase { + private static final Notification.PayloadFormat PAYLOAD_FORMAT = + Notification.PayloadFormat.JSON_API_V1.JSON_API_V1; + private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); + private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String TOPIC = String.format("projects/%s/topics/new-topic", PROJECT); + private static TopicAdminClient topicAdminClient; + + @BeforeClass + public static void configureTopicAdminClient() throws IOException { + if(PROJECT != null) { + topicAdminClient = TopicAdminClient.create(); + topicAdminClient.createTopic(TOPIC); + GetIamPolicyRequest getIamPolicyRequest = + GetIamPolicyRequest.newBuilder().setResource(TOPIC).build(); + com.google.iam.v1.Policy policy = topicAdminClient.getIamPolicy(getIamPolicyRequest); + Binding binding = + Binding.newBuilder().setRole("roles/owner").addMembers("allAuthenticatedUsers").build(); + SetIamPolicyRequest setIamPolicyRequest = + SetIamPolicyRequest.newBuilder() + .setResource(TOPIC) + .setPolicy(policy.toBuilder().addBindings(binding).build()) + .build(); + topicAdminClient.setIamPolicy(setIamPolicyRequest); + } + } + + @AfterClass + public static void deleteTopicAndClient() { + /* Delete the Pub/Sub topic */ + if (topicAdminClient != null) { + topicAdminClient.deleteTopic(TOPIC); + topicAdminClient.close(); + } + } + + @Test + public void testPrintBucketPubSubNotification() { + // Check that we can access project value and that topic admin client came up successfully + assertNotNull("Unable to determine project", PROJECT); + assertNotNull("Topic Admin Client did not start up", topicAdminClient); + + NotificationInfo notificationInfo = + NotificationInfo.newBuilder(TOPIC) + .setCustomAttributes(CUSTOM_ATTRIBUTES) + .setPayloadFormat(PAYLOAD_FORMAT) + .build(); + Notification notification = storage.createNotification(bucketName, notificationInfo); + PrintPubSubNotification.printPubSubNotification(bucketName, notification.getNotificationId()); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(TOPIC); + } + +} \ No newline at end of file From 37c0c335a56a3328350e74f88aac3cfb18a566dd Mon Sep 17 00:00:00 2001 From: Sydney Munro Date: Mon, 28 Mar 2022 16:53:16 -0700 Subject: [PATCH 2/9] Fix checkstyle issues --- .../storage/bucket/CreateBucketPubSubNotification.java | 7 ++++--- .../storage/bucket/DeleteBucketPubSubNotification.java | 2 +- .../example/storage/bucket/ListPubSubNotifications.java | 7 ++++--- .../example/storage/bucket/PrintPubSubNotification.java | 5 +++-- .../storage/bucket/CreateBucketPubSubNotificationTest.java | 5 +++-- .../storage/bucket/DeleteBucketPubSubNotificationTest.java | 2 +- .../storage/bucket/ListPubSubNotificationsTest.java | 5 +++-- .../storage/bucket/PrintPubSubNotificationTest.java | 5 +++-- 8 files changed, 22 insertions(+), 16 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java index f79fff8a00..ab660484a3 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java @@ -19,15 +19,16 @@ // [START storage_create_bucket_notifications] import com.google.cloud.storage.Bucket; -import com.google.cloud.storage.Storage; -import com.google.cloud.storage.StorageOptions; import com.google.cloud.storage.Notification; import com.google.cloud.storage.NotificationInfo; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; public class CreateBucketPubSubNotification { - public static void createBucketPubSubNotification(String bucketName, NotificationInfo notificationInfo) { + public static void createBucketPubSubNotification(String bucketName, + NotificationInfo notificationInfo) { // The ID to give your GCS bucket // String bucketName = "your-unique-bucket-name"; // The NotificationInfo for the notification you would like to create diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java index c392d0266d..779b83be4e 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java @@ -32,7 +32,7 @@ public static void deleteBucketPubSubNotification(String bucketName, String noti // String notificationId = "your-unique-notification-id" Storage storage = StorageOptions.newBuilder().build().getService(); boolean success = storage.deleteNotification(bucketName, notificationId); - if(success) { + if (success) { System.out.println("Successfully deleted notification"); } else { System.out.println("Failed to find notification"); diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java b/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java index 7bfb662a80..b72c735f61 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java @@ -19,9 +19,9 @@ // [START storage_list_bucket_notifications] import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.Notification; import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageOptions; -import com.google.cloud.storage.Notification; import java.util.List; @@ -32,8 +32,9 @@ public static void listPubSubNotifications(String bucketName) { // String bucketName = "your-unique-bucket-name"; Storage storage = StorageOptions.newBuilder().build().getService(); List notificationList = storage.listNotifications(bucketName); - for(Notification notification : notificationList) { - System.out.println("Found notification " + notification.getTopic() + " for bucket "+ bucketName); + for (Notification notification : notificationList) { + System.out.println( + "Found notification " + notification.getTopic() + " for bucket " + bucketName); } } } diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java index 402767767c..5ca2c623c4 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java @@ -19,9 +19,9 @@ // [START storage_print_pubsub_bucket_notification] import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.Notification; import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageOptions; -import com.google.cloud.storage.Notification; import java.util.List; @@ -35,7 +35,8 @@ public static void printPubSubNotification(String bucketName, String notificatio Storage storage = StorageOptions.newBuilder().build().getService(); Notification notification = storage.getNotification(bucketName, notificationId); - System.out.println("Found notification " + notification.getTopic() + " for bucket "+ bucketName); + System.out.println( + "Found notification " + notification.getTopic() + " for bucket " + bucketName); } } // [END storage_print_pubsub_bucket_notification] \ No newline at end of file diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java index c3e4804337..b8fa3be569 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java @@ -19,8 +19,8 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertNotNull; -import com.google.cloud.pubsub.v1.TopicAdminClient; import com.example.storage.TestBase; +import com.google.cloud.pubsub.v1.TopicAdminClient; import com.google.cloud.storage.Notification; import com.google.cloud.storage.NotificationInfo; import com.google.common.collect.ImmutableMap; @@ -34,6 +34,7 @@ import org.junit.Test; public class CreateBucketPubSubNotificationTest extends TestBase { + private static final Notification.PayloadFormat PAYLOAD_FORMAT = Notification.PayloadFormat.JSON_API_V1.JSON_API_V1; private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); @@ -43,7 +44,7 @@ public class CreateBucketPubSubNotificationTest extends TestBase { @BeforeClass public static void configureTopicAdminClient() throws IOException { - if(PROJECT != null) { + if (PROJECT != null) { topicAdminClient = TopicAdminClient.create(); topicAdminClient.createTopic(TOPIC); GetIamPolicyRequest getIamPolicyRequest = diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java index 7c7093bbd2..809233068d 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java @@ -19,8 +19,8 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertNotNull; -import com.google.cloud.pubsub.v1.TopicAdminClient; import com.example.storage.TestBase; +import com.google.cloud.pubsub.v1.TopicAdminClient; import com.google.cloud.storage.Notification; import com.google.cloud.storage.NotificationInfo; import com.google.common.collect.ImmutableMap; diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java index 45d8dbfbfe..d1eb9217d8 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java @@ -19,8 +19,8 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertNotNull; -import com.google.cloud.pubsub.v1.TopicAdminClient; import com.example.storage.TestBase; +import com.google.cloud.pubsub.v1.TopicAdminClient; import com.google.cloud.storage.Notification; import com.google.cloud.storage.NotificationInfo; import com.google.common.collect.ImmutableMap; @@ -34,6 +34,7 @@ import org.junit.Test; public class ListPubSubNotificationsTest extends TestBase { + private static final Notification.PayloadFormat PAYLOAD_FORMAT = Notification.PayloadFormat.JSON_API_V1.JSON_API_V1; private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); @@ -43,7 +44,7 @@ public class ListPubSubNotificationsTest extends TestBase { @BeforeClass public static void configureTopicAdminClient() throws IOException { - if(PROJECT != null) { + if (PROJECT != null) { topicAdminClient = TopicAdminClient.create(); topicAdminClient.createTopic(TOPIC); GetIamPolicyRequest getIamPolicyRequest = diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java index e71492b60e..e35431ebe4 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java @@ -19,8 +19,8 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertNotNull; -import com.google.cloud.pubsub.v1.TopicAdminClient; import com.example.storage.TestBase; +import com.google.cloud.pubsub.v1.TopicAdminClient; import com.google.cloud.storage.Notification; import com.google.cloud.storage.NotificationInfo; import com.google.common.collect.ImmutableMap; @@ -34,6 +34,7 @@ import org.junit.Test; public class PrintPubSubNotificationTest extends TestBase { + private static final Notification.PayloadFormat PAYLOAD_FORMAT = Notification.PayloadFormat.JSON_API_V1.JSON_API_V1; private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); @@ -43,7 +44,7 @@ public class PrintPubSubNotificationTest extends TestBase { @BeforeClass public static void configureTopicAdminClient() throws IOException { - if(PROJECT != null) { + if (PROJECT != null) { topicAdminClient = TopicAdminClient.create(); topicAdminClient.createTopic(TOPIC); GetIamPolicyRequest getIamPolicyRequest = From 584abffc7828105713306afb1708cc1fbee816bc Mon Sep 17 00:00:00 2001 From: Sydney Munro Date: Wed, 30 Mar 2022 10:14:18 -0700 Subject: [PATCH 3/9] Address PR comments --- .../CreateBucketPubSubNotification.java | 35 ++++++++++++++++--- .../DeleteBucketPubSubNotification.java | 2 +- .../bucket/ListPubSubNotifications.java | 2 +- .../bucket/PrintPubSubNotification.java | 1 - .../CreateBucketPubSubNotificationTest.java | 17 +++++---- 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java index ab660484a3..a255992cef 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java @@ -18,22 +18,49 @@ // [START storage_create_bucket_notifications] -import com.google.cloud.storage.Bucket; import com.google.cloud.storage.Notification; import com.google.cloud.storage.NotificationInfo; +import com.google.cloud.storage.NotificationInfo.EventType; +import com.google.cloud.storage.NotificationInfo.PayloadFormat; import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageOptions; +import java.util.Map; public class CreateBucketPubSubNotification { public static void createBucketPubSubNotification(String bucketName, - NotificationInfo notificationInfo) { + String topicName, + Map customAttributes, + EventType[] eventTypes, + String objectNamePrefix, + PayloadFormat payloadFormat, + String selfLink, + String etag) { // The ID to give your GCS bucket // String bucketName = "your-unique-bucket-name"; - // The NotificationInfo for the notification you would like to create - // See: https://cloud.google.com/java/docs/reference/google-cloud-notification/latest/com.google.cloud.notification.NotificationInfo + // The name of the topic you would like to create a notification for + // String topicName = "projects/{your-project}/topics/{your-topic}"; + // Any custom attributes + // Map customAttributes = Map.of("label", "value"); + // The object name prefix for which this notification configuration applies + // String objectNamePrefix = "blob-"; + // Desired content of the Payload + // PayloadFormat payloadFormat = PayloadFormat.JSON_API_V1.JSON_API_V1; + // The canonical URI of this topic as a string + // String selfLink = "//pubsub.googleapis.com/projects/{project-identifier}/topics/{my-topic}"; + // HTTP 1.1 Entity tag for this subscription notification + // String etag = "etag-value"; + Storage storage = StorageOptions.newBuilder().build().getService(); + NotificationInfo notificationInfo = NotificationInfo.newBuilder(topicName) + .setEtag(etag) + .setCustomAttributes(customAttributes) + .setSelfLink(selfLink) + .setEventTypes(eventTypes) + .setObjectNamePrefix(objectNamePrefix) + .setPayloadFormat(payloadFormat) + .build(); Notification notification = storage.createNotification(bucketName, notificationInfo); System.out.println("Successfully created notification for topic " + notification.getTopic()); } diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java index 779b83be4e..92ee100c9d 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java @@ -17,7 +17,6 @@ package com.example.storage.bucket; // [START storage_delete_bucket_notification] - import com.google.cloud.storage.Bucket; import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageOptions; @@ -30,6 +29,7 @@ public static void deleteBucketPubSubNotification(String bucketName, String noti // String bucketName = "your-unique-bucket-name"; // The NotificationId for the notification you would like to delete\ // String notificationId = "your-unique-notification-id" + Storage storage = StorageOptions.newBuilder().build().getService(); boolean success = storage.deleteNotification(bucketName, notificationId); if (success) { diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java b/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java index b72c735f61..48f2e2df87 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java @@ -17,7 +17,6 @@ package com.example.storage.bucket; // [START storage_list_bucket_notifications] - import com.google.cloud.storage.Bucket; import com.google.cloud.storage.Notification; import com.google.cloud.storage.Storage; @@ -30,6 +29,7 @@ public class ListPubSubNotifications { public static void listPubSubNotifications(String bucketName) { // The ID to give your GCS bucket // String bucketName = "your-unique-bucket-name"; + Storage storage = StorageOptions.newBuilder().build().getService(); List notificationList = storage.listNotifications(bucketName); for (Notification notification : notificationList) { diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java index 5ca2c623c4..af3c422a17 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java @@ -17,7 +17,6 @@ package com.example.storage.bucket; // [START storage_print_pubsub_bucket_notification] - import com.google.cloud.storage.Bucket; import com.google.cloud.storage.Notification; import com.google.cloud.storage.Storage; diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java index b8fa3be569..f8f7f40b1d 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java @@ -22,7 +22,8 @@ import com.example.storage.TestBase; import com.google.cloud.pubsub.v1.TopicAdminClient; import com.google.cloud.storage.Notification; -import com.google.cloud.storage.NotificationInfo; +import com.google.cloud.storage.NotificationInfo.EventType; +import com.google.cloud.storage.NotificationInfo.PayloadFormat; import com.google.common.collect.ImmutableMap; import com.google.iam.v1.Binding; import com.google.iam.v1.GetIamPolicyRequest; @@ -40,6 +41,12 @@ public class CreateBucketPubSubNotificationTest extends TestBase { private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String TOPIC = String.format("projects/%s/topics/new-topic", PROJECT); + private static final String ETAG = "0xFF00"; + private static final String SELF_LINK = "http://storage/b/n"; + private static final String OBJECT_NAME_PREFIX = "index.html"; + private static final EventType[] EVENT_TYPES = { + EventType.OBJECT_FINALIZE, EventType.OBJECT_METADATA_UPDATE + }; private static TopicAdminClient topicAdminClient; @BeforeClass @@ -76,12 +83,8 @@ public void testCreateBucketPubSubNotification() { assertNotNull("Unable to determine project", PROJECT); assertNotNull("Topic Admin Client did not start up", topicAdminClient); - NotificationInfo notificationInfo = - NotificationInfo.newBuilder(TOPIC) - .setCustomAttributes(CUSTOM_ATTRIBUTES) - .setPayloadFormat(PAYLOAD_FORMAT) - .build(); - CreateBucketPubSubNotification.createBucketPubSubNotification(bucketName, notificationInfo); + com.example.storage.bucket.CreateBucketPubSubNotification.createBucketPubSubNotification(bucketName, TOPIC, + CUSTOM_ATTRIBUTES, EVENT_TYPES, OBJECT_NAME_PREFIX, PAYLOAD_FORMAT, SELF_LINK, ETAG); assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(TOPIC); } From f790f1df427c8de7a14003db729cb8b54313e7cd Mon Sep 17 00:00:00 2001 From: Sydney Munro Date: Wed, 30 Mar 2022 11:08:09 -0700 Subject: [PATCH 4/9] Removing unneeded newline --- .../example/storage/bucket/CreateBucketPubSubNotification.java | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java index a255992cef..5ea846e26c 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java @@ -17,7 +17,6 @@ package com.example.storage.bucket; // [START storage_create_bucket_notifications] - import com.google.cloud.storage.Notification; import com.google.cloud.storage.NotificationInfo; import com.google.cloud.storage.NotificationInfo.EventType; From 9022a7c34d94f517b32dd509ac3991e3510bb6b0 Mon Sep 17 00:00:00 2001 From: Sydney Munro Date: Wed, 30 Mar 2022 11:22:25 -0700 Subject: [PATCH 5/9] Making topics test specific to avoid conflicts --- .../storage/bucket/CreateBucketPubSubNotificationTest.java | 2 +- .../storage/bucket/DeleteBucketPubSubNotificationTest.java | 2 +- .../com/example/storage/bucket/ListPubSubNotificationsTest.java | 2 +- .../com/example/storage/bucket/PrintPubSubNotificationTest.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java index f8f7f40b1d..7c4e3f0c44 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java @@ -40,7 +40,7 @@ public class CreateBucketPubSubNotificationTest extends TestBase { Notification.PayloadFormat.JSON_API_V1.JSON_API_V1; private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String TOPIC = String.format("projects/%s/topics/new-topic", PROJECT); + private static final String TOPIC = String.format("projects/%s/topics/new-topic-create", PROJECT); private static final String ETAG = "0xFF00"; private static final String SELF_LINK = "http://storage/b/n"; private static final String OBJECT_NAME_PREFIX = "index.html"; diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java index 809233068d..f8049e010b 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java @@ -39,7 +39,7 @@ public class DeleteBucketPubSubNotificationTest extends TestBase { Notification.PayloadFormat.JSON_API_V1.JSON_API_V1; private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String TOPIC = String.format("projects/%s/topics/new-topic", PROJECT); + private static final String TOPIC = String.format("projects/%s/topics/new-topic-delete", PROJECT); private static TopicAdminClient topicAdminClient; private static final NotificationInfo notificationInfo = NotificationInfo.newBuilder(TOPIC) .setCustomAttributes(CUSTOM_ATTRIBUTES) diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java index d1eb9217d8..5c74ef3a2c 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java @@ -39,7 +39,7 @@ public class ListPubSubNotificationsTest extends TestBase { Notification.PayloadFormat.JSON_API_V1.JSON_API_V1; private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String TOPIC = String.format("projects/%s/topics/new-topic", PROJECT); + private static final String TOPIC = String.format("projects/%s/topics/new-topic-list", PROJECT); private static TopicAdminClient topicAdminClient; @BeforeClass diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java index e35431ebe4..f9eaf2c3cf 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java @@ -39,7 +39,7 @@ public class PrintPubSubNotificationTest extends TestBase { Notification.PayloadFormat.JSON_API_V1.JSON_API_V1; private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String TOPIC = String.format("projects/%s/topics/new-topic", PROJECT); + private static final String TOPIC = String.format("projects/%s/topics/new-topic-print", PROJECT); private static TopicAdminClient topicAdminClient; @BeforeClass From ab6979bbc06aeb53c1181383a7b12e7c87e9643d Mon Sep 17 00:00:00 2001 From: Sydney Munro Date: Wed, 30 Mar 2022 11:26:50 -0700 Subject: [PATCH 6/9] Checkstyle fixes --- .../bucket/CreateBucketPubSubNotificationTest.java | 11 +++++++++-- .../bucket/DeleteBucketPubSubNotificationTest.java | 6 ++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java index 7c4e3f0c44..e34bbcca2c 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java @@ -83,8 +83,15 @@ public void testCreateBucketPubSubNotification() { assertNotNull("Unable to determine project", PROJECT); assertNotNull("Topic Admin Client did not start up", topicAdminClient); - com.example.storage.bucket.CreateBucketPubSubNotification.createBucketPubSubNotification(bucketName, TOPIC, - CUSTOM_ATTRIBUTES, EVENT_TYPES, OBJECT_NAME_PREFIX, PAYLOAD_FORMAT, SELF_LINK, ETAG); + com.example.storage.bucket.CreateBucketPubSubNotification.createBucketPubSubNotification( + bucketName, + TOPIC, + CUSTOM_ATTRIBUTES, + EVENT_TYPES, + OBJECT_NAME_PREFIX, + PAYLOAD_FORMAT, + SELF_LINK, + ETAG); assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(TOPIC); } diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java index f8049e010b..2abbad233f 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java @@ -81,7 +81,8 @@ public void testDeleteBucketPubSubNotification() { assertNotNull("Topic Admin Client did not start up", topicAdminClient); Notification notification = storage.createNotification(bucketName, notificationInfo); - com.example.storage.bucket.DeleteBucketPubSubNotification.deleteBucketPubSubNotification(bucketName, + com.example.storage.bucket.DeleteBucketPubSubNotification.deleteBucketPubSubNotification( + bucketName, notification.getNotificationId()); assertThat(stdOut.getCapturedOutputAsUtf8String()).contains( "Successfully deleted notification"); @@ -96,7 +97,8 @@ public void testNotificationNotFound() { // Do a delete first. storage.deleteNotification(bucketName, notification.getNotificationId()); // Now try to delete again. - com.example.storage.bucket.DeleteBucketPubSubNotification.deleteBucketPubSubNotification(bucketName, + com.example.storage.bucket.DeleteBucketPubSubNotification.deleteBucketPubSubNotification( + bucketName, notification.getNotificationId()); assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("Failed to find notification"); } From ddbc55414b311384e4c78dad5b767eb39f66a6ce Mon Sep 17 00:00:00 2001 From: Sydney Munro Date: Wed, 30 Mar 2022 14:22:01 -0700 Subject: [PATCH 7/9] Adding newlines in comment section to improve readability --- .../storage/bucket/CreateBucketPubSubNotification.java | 6 ++++++ .../storage/bucket/DeleteBucketPubSubNotification.java | 3 ++- .../com/example/storage/bucket/PrintPubSubNotification.java | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java index 5ea846e26c..d98727497b 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java @@ -38,16 +38,22 @@ public static void createBucketPubSubNotification(String bucketName, String etag) { // The ID to give your GCS bucket // String bucketName = "your-unique-bucket-name"; + // The name of the topic you would like to create a notification for // String topicName = "projects/{your-project}/topics/{your-topic}"; + // Any custom attributes // Map customAttributes = Map.of("label", "value"); + // The object name prefix for which this notification configuration applies // String objectNamePrefix = "blob-"; + // Desired content of the Payload // PayloadFormat payloadFormat = PayloadFormat.JSON_API_V1.JSON_API_V1; + // The canonical URI of this topic as a string // String selfLink = "//pubsub.googleapis.com/projects/{project-identifier}/topics/{my-topic}"; + // HTTP 1.1 Entity tag for this subscription notification // String etag = "etag-value"; diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java index 92ee100c9d..b6e9ccbfed 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java @@ -27,7 +27,8 @@ public class DeleteBucketPubSubNotification { public static void deleteBucketPubSubNotification(String bucketName, String notificationId) { // The ID to give your GCS bucket // String bucketName = "your-unique-bucket-name"; - // The NotificationId for the notification you would like to delete\ + + // The NotificationId for the notification you would like to delete // String notificationId = "your-unique-notification-id" Storage storage = StorageOptions.newBuilder().build().getService(); diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java index af3c422a17..dbb8811bd7 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java @@ -29,6 +29,7 @@ public class PrintPubSubNotification { public static void printPubSubNotification(String bucketName, String notificationId) { // The ID to give your GCS bucket // String bucketName = "your-unique-bucket-name"; + // The Pub/Sub topic you would like to find // String notificationId = "your-unique-notification-id" From acb0115651d231e692c4e58331fcc6150a3dc55c Mon Sep 17 00:00:00 2001 From: Sydney Munro Date: Thu, 31 Mar 2022 10:24:59 -0700 Subject: [PATCH 8/9] Addressing PR comments on ETAG and Selflink not being included in the sample --- .../bucket/CreateBucketPubSubNotification.java | 12 +----------- .../bucket/CreateBucketPubSubNotificationTest.java | 6 +----- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java index d98727497b..1d5cc9c247 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java @@ -33,9 +33,7 @@ public static void createBucketPubSubNotification(String bucketName, Map customAttributes, EventType[] eventTypes, String objectNamePrefix, - PayloadFormat payloadFormat, - String selfLink, - String etag) { + PayloadFormat payloadFormat) { // The ID to give your GCS bucket // String bucketName = "your-unique-bucket-name"; @@ -51,17 +49,9 @@ public static void createBucketPubSubNotification(String bucketName, // Desired content of the Payload // PayloadFormat payloadFormat = PayloadFormat.JSON_API_V1.JSON_API_V1; - // The canonical URI of this topic as a string - // String selfLink = "//pubsub.googleapis.com/projects/{project-identifier}/topics/{my-topic}"; - - // HTTP 1.1 Entity tag for this subscription notification - // String etag = "etag-value"; - Storage storage = StorageOptions.newBuilder().build().getService(); NotificationInfo notificationInfo = NotificationInfo.newBuilder(topicName) - .setEtag(etag) .setCustomAttributes(customAttributes) - .setSelfLink(selfLink) .setEventTypes(eventTypes) .setObjectNamePrefix(objectNamePrefix) .setPayloadFormat(payloadFormat) diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java index e34bbcca2c..9804d367dc 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java @@ -41,8 +41,6 @@ public class CreateBucketPubSubNotificationTest extends TestBase { private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String TOPIC = String.format("projects/%s/topics/new-topic-create", PROJECT); - private static final String ETAG = "0xFF00"; - private static final String SELF_LINK = "http://storage/b/n"; private static final String OBJECT_NAME_PREFIX = "index.html"; private static final EventType[] EVENT_TYPES = { EventType.OBJECT_FINALIZE, EventType.OBJECT_METADATA_UPDATE @@ -89,9 +87,7 @@ public void testCreateBucketPubSubNotification() { CUSTOM_ATTRIBUTES, EVENT_TYPES, OBJECT_NAME_PREFIX, - PAYLOAD_FORMAT, - SELF_LINK, - ETAG); + PAYLOAD_FORMAT); assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(TOPIC); } From 9e0d8178c93484e4c3d4797e91af7dd742ec991c Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Thu, 31 Mar 2022 17:27:11 +0000 Subject: [PATCH 9/9] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20po?= =?UTF-8?q?st-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- README.md | 10 ++++++--- .../CreateBucketPubSubNotification.java | 19 ++++++++-------- .../DeleteBucketPubSubNotification.java | 4 +--- .../bucket/ListPubSubNotifications.java | 4 +--- .../bucket/PrintPubSubNotification.java | 7 ++---- .../CreateBucketPubSubNotificationTest.java | 13 +++-------- .../DeleteBucketPubSubNotificationTest.java | 22 +++++++++---------- .../bucket/ListPubSubNotificationsTest.java | 3 +-- .../bucket/PrintPubSubNotificationTest.java | 3 +-- 9 files changed, 36 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 5f38caabf1..f62c7fe815 100644 --- a/README.md +++ b/README.md @@ -49,20 +49,20 @@ If you are using Maven without BOM, add this to your dependencies: If you are using Gradle 5.x or later, add this to your dependencies ```Groovy -implementation platform('com.google.cloud:libraries-bom:25.0.0') +implementation platform('com.google.cloud:libraries-bom:25.1.0') implementation 'com.google.cloud:google-cloud-storage' ``` If you are using Gradle without BOM, add this to your dependencies ```Groovy -implementation 'com.google.cloud:google-cloud-storage:2.5.1' +implementation 'com.google.cloud:google-cloud-storage:2.6.0' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.cloud" % "google-cloud-storage" % "2.5.1" +libraryDependencies += "com.google.cloud" % "google-cloud-storage" % "2.6.0" ``` ## Authentication @@ -240,9 +240,11 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-storage/tree/ | Change Default Storage Class | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/ChangeDefaultStorageClass.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/ChangeDefaultStorageClass.java) | | Configure Bucket Cors | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/ConfigureBucketCors.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/ConfigureBucketCors.java) | | Create Bucket | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucket.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/CreateBucket.java) | +| Create Bucket Pub Sub Notification | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java) | | Create Bucket With Storage Class And Location | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketWithStorageClassAndLocation.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketWithStorageClassAndLocation.java) | | Create Bucket With Turbo Replication | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketWithTurboReplication.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketWithTurboReplication.java) | | Delete Bucket | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucket.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucket.java) | +| Delete Bucket Pub Sub Notification | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java) | | Disable Bucket Versioning | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/DisableBucketVersioning.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/DisableBucketVersioning.java) | | Disable Default Event Based Hold | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/DisableDefaultEventBasedHold.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/DisableDefaultEventBasedHold.java) | | Disable Lifecycle Management | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/DisableLifecycleManagement.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/DisableLifecycleManagement.java) | @@ -262,10 +264,12 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-storage/tree/ | Get Uniform Bucket Level Access | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/GetUniformBucketLevelAccess.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/GetUniformBucketLevelAccess.java) | | List Bucket Iam Members | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/ListBucketIamMembers.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/ListBucketIamMembers.java) | | List Buckets | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/ListBuckets.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/ListBuckets.java) | +| List Pub Sub Notifications | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java) | | Lock Retention Policy | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/LockRetentionPolicy.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/LockRetentionPolicy.java) | | Make Bucket Public | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/MakeBucketPublic.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/MakeBucketPublic.java) | | Print Bucket Acl | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAcl.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAcl.java) | | Print Bucket Acl Filter By User | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAclFilterByUser.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAclFilterByUser.java) | +| Print Pub Sub Notification | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java) | | Remove Bucket Cors | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketCors.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketCors.java) | | Remove Bucket Default Kms Key | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketDefaultKmsKey.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketDefaultKmsKey.java) | | Remove Bucket Default Owner | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketDefaultOwner.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketDefaultOwner.java) | diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java index 1d5cc9c247..f68f0e2fa1 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java @@ -25,10 +25,10 @@ import com.google.cloud.storage.StorageOptions; import java.util.Map; - public class CreateBucketPubSubNotification { - public static void createBucketPubSubNotification(String bucketName, + public static void createBucketPubSubNotification( + String bucketName, String topicName, Map customAttributes, EventType[] eventTypes, @@ -50,14 +50,15 @@ public static void createBucketPubSubNotification(String bucketName, // PayloadFormat payloadFormat = PayloadFormat.JSON_API_V1.JSON_API_V1; Storage storage = StorageOptions.newBuilder().build().getService(); - NotificationInfo notificationInfo = NotificationInfo.newBuilder(topicName) - .setCustomAttributes(customAttributes) - .setEventTypes(eventTypes) - .setObjectNamePrefix(objectNamePrefix) - .setPayloadFormat(payloadFormat) - .build(); + NotificationInfo notificationInfo = + NotificationInfo.newBuilder(topicName) + .setCustomAttributes(customAttributes) + .setEventTypes(eventTypes) + .setObjectNamePrefix(objectNamePrefix) + .setPayloadFormat(payloadFormat) + .build(); Notification notification = storage.createNotification(bucketName, notificationInfo); System.out.println("Successfully created notification for topic " + notification.getTopic()); } } -// [END storage_create_bucket_notifications] \ No newline at end of file +// [END storage_create_bucket_notifications] diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java index b6e9ccbfed..dd59e2e189 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java @@ -17,11 +17,9 @@ package com.example.storage.bucket; // [START storage_delete_bucket_notification] -import com.google.cloud.storage.Bucket; import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageOptions; - public class DeleteBucketPubSubNotification { public static void deleteBucketPubSubNotification(String bucketName, String notificationId) { @@ -40,4 +38,4 @@ public static void deleteBucketPubSubNotification(String bucketName, String noti } } } -// [END storage_delete_bucket_notification] \ No newline at end of file +// [END storage_delete_bucket_notification] diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java b/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java index 48f2e2df87..b3fb614f8b 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java @@ -17,13 +17,11 @@ package com.example.storage.bucket; // [START storage_list_bucket_notifications] -import com.google.cloud.storage.Bucket; import com.google.cloud.storage.Notification; import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageOptions; import java.util.List; - public class ListPubSubNotifications { public static void listPubSubNotifications(String bucketName) { @@ -38,4 +36,4 @@ public static void listPubSubNotifications(String bucketName) { } } } -// [END storage_list_bucket_notifications] \ No newline at end of file +// [END storage_list_bucket_notifications] diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java b/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java index dbb8811bd7..083a0b551a 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java @@ -17,19 +17,16 @@ package com.example.storage.bucket; // [START storage_print_pubsub_bucket_notification] -import com.google.cloud.storage.Bucket; import com.google.cloud.storage.Notification; import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageOptions; -import java.util.List; - public class PrintPubSubNotification { public static void printPubSubNotification(String bucketName, String notificationId) { // The ID to give your GCS bucket // String bucketName = "your-unique-bucket-name"; - + // The Pub/Sub topic you would like to find // String notificationId = "your-unique-notification-id" @@ -39,4 +36,4 @@ public static void printPubSubNotification(String bucketName, String notificatio "Found notification " + notification.getTopic() + " for bucket " + bucketName); } } -// [END storage_print_pubsub_bucket_notification] \ No newline at end of file +// [END storage_print_pubsub_bucket_notification] diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java index 9804d367dc..bb28ec6db2 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java @@ -23,7 +23,6 @@ import com.google.cloud.pubsub.v1.TopicAdminClient; import com.google.cloud.storage.Notification; import com.google.cloud.storage.NotificationInfo.EventType; -import com.google.cloud.storage.NotificationInfo.PayloadFormat; import com.google.common.collect.ImmutableMap; import com.google.iam.v1.Binding; import com.google.iam.v1.GetIamPolicyRequest; @@ -43,7 +42,7 @@ public class CreateBucketPubSubNotificationTest extends TestBase { private static final String TOPIC = String.format("projects/%s/topics/new-topic-create", PROJECT); private static final String OBJECT_NAME_PREFIX = "index.html"; private static final EventType[] EVENT_TYPES = { - EventType.OBJECT_FINALIZE, EventType.OBJECT_METADATA_UPDATE + EventType.OBJECT_FINALIZE, EventType.OBJECT_METADATA_UPDATE }; private static TopicAdminClient topicAdminClient; @@ -82,13 +81,7 @@ public void testCreateBucketPubSubNotification() { assertNotNull("Topic Admin Client did not start up", topicAdminClient); com.example.storage.bucket.CreateBucketPubSubNotification.createBucketPubSubNotification( - bucketName, - TOPIC, - CUSTOM_ATTRIBUTES, - EVENT_TYPES, - OBJECT_NAME_PREFIX, - PAYLOAD_FORMAT); + bucketName, TOPIC, CUSTOM_ATTRIBUTES, EVENT_TYPES, OBJECT_NAME_PREFIX, PAYLOAD_FORMAT); assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(TOPIC); } - -} \ No newline at end of file +} diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java index 2abbad233f..e094f33469 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/DeleteBucketPubSubNotificationTest.java @@ -41,10 +41,11 @@ public class DeleteBucketPubSubNotificationTest extends TestBase { private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String TOPIC = String.format("projects/%s/topics/new-topic-delete", PROJECT); private static TopicAdminClient topicAdminClient; - private static final NotificationInfo notificationInfo = NotificationInfo.newBuilder(TOPIC) - .setCustomAttributes(CUSTOM_ATTRIBUTES) - .setPayloadFormat(PAYLOAD_FORMAT) - .build(); + private static final NotificationInfo notificationInfo = + NotificationInfo.newBuilder(TOPIC) + .setCustomAttributes(CUSTOM_ATTRIBUTES) + .setPayloadFormat(PAYLOAD_FORMAT) + .build(); @BeforeClass public static void configureTopicAdminClient() throws IOException { @@ -82,10 +83,9 @@ public void testDeleteBucketPubSubNotification() { Notification notification = storage.createNotification(bucketName, notificationInfo); com.example.storage.bucket.DeleteBucketPubSubNotification.deleteBucketPubSubNotification( - bucketName, - notification.getNotificationId()); - assertThat(stdOut.getCapturedOutputAsUtf8String()).contains( - "Successfully deleted notification"); + bucketName, notification.getNotificationId()); + assertThat(stdOut.getCapturedOutputAsUtf8String()) + .contains("Successfully deleted notification"); } @Test @@ -98,9 +98,7 @@ public void testNotificationNotFound() { storage.deleteNotification(bucketName, notification.getNotificationId()); // Now try to delete again. com.example.storage.bucket.DeleteBucketPubSubNotification.deleteBucketPubSubNotification( - bucketName, - notification.getNotificationId()); + bucketName, notification.getNotificationId()); assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("Failed to find notification"); } - -} \ No newline at end of file +} diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java index 5c74ef3a2c..c7dee85472 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/ListPubSubNotificationsTest.java @@ -85,5 +85,4 @@ public void testListBucketPubSubNotification() { ListPubSubNotifications.listPubSubNotifications(bucketName); assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(TOPIC); } - -} \ No newline at end of file +} diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java index f9eaf2c3cf..05526675ff 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/PrintPubSubNotificationTest.java @@ -85,5 +85,4 @@ public void testPrintBucketPubSubNotification() { PrintPubSubNotification.printPubSubNotification(bucketName, notification.getNotificationId()); assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(TOPIC); } - -} \ No newline at end of file +}