Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sydney-munro committed Mar 30, 2022
1 parent 6d92957 commit ddc4c5c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
Expand Up @@ -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<String, String> 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<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;
// 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());
}
Expand Down
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Expand Up @@ -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;
Expand All @@ -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<Notification> notificationList = storage.listNotifications(bucketName);
for (Notification notification : notificationList) {
Expand Down
Expand Up @@ -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;
Expand Down
Expand Up @@ -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;
Expand All @@ -40,6 +41,12 @@ public class CreateBucketPubSubNotificationTest extends TestBase {
private static final Map<String, String> 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
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit ddc4c5c

Please sign in to comment.