-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Adding PubSub Notification Samples (#1317)
* Adding PubSub Notification Samples See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
62684c2
commit fa9920d
Showing
13 changed files
with
581 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...les/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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.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, | ||
String topicName, | ||
Map<String, String> customAttributes, | ||
EventType[] eventTypes, | ||
String objectNamePrefix, | ||
PayloadFormat payloadFormat) { | ||
// 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<String, String> 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; | ||
|
||
Storage storage = StorageOptions.newBuilder().build().getService(); | ||
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] |
41 changes: 41 additions & 0 deletions
41
...les/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_delete_bucket_notification] | ||
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] |
39 changes: 39 additions & 0 deletions
39
samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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.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) { | ||
// The ID to give your GCS bucket | ||
// String bucketName = "your-unique-bucket-name"; | ||
|
||
Storage storage = StorageOptions.newBuilder().build().getService(); | ||
List<Notification> notificationList = storage.listNotifications(bucketName); | ||
for (Notification notification : notificationList) { | ||
System.out.println( | ||
"Found notification " + notification.getTopic() + " for bucket " + bucketName); | ||
} | ||
} | ||
} | ||
// [END storage_list_bucket_notifications] |
39 changes: 39 additions & 0 deletions
39
samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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.Notification; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
|
||
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] |
Oops, something went wrong.