|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package pubsub; |
| 18 | + |
| 19 | +// [START pubsub_create_cloud_storage_subscription] |
| 20 | +import com.google.cloud.pubsub.v1.SubscriptionAdminClient; |
| 21 | +import com.google.protobuf.Duration; |
| 22 | +import com.google.pubsub.v1.CloudStorageConfig; |
| 23 | +import com.google.pubsub.v1.ProjectSubscriptionName; |
| 24 | +import com.google.pubsub.v1.ProjectTopicName; |
| 25 | +import com.google.pubsub.v1.Subscription; |
| 26 | +import java.io.IOException; |
| 27 | + |
| 28 | +public class CreateCloudStorageSubscriptionExample { |
| 29 | + public static void main(String... args) throws Exception { |
| 30 | + // TODO(developer): Replace these variables before running the sample. |
| 31 | + String projectId = "your-project-id"; |
| 32 | + String topicId = "your-topic-id"; |
| 33 | + String subscriptionId = "your-subscription-id"; |
| 34 | + String bucket = "your-bucket"; |
| 35 | + String filenamePrefix = "log_events_"; |
| 36 | + String filenameSuffix = ".text"; |
| 37 | + Duration maxDuration = Duration.newBuilder().setSeconds(300).build(); |
| 38 | + |
| 39 | + createCloudStorageSubscription( |
| 40 | + projectId, topicId, subscriptionId, bucket, filenamePrefix, filenameSuffix, maxDuration); |
| 41 | + } |
| 42 | + |
| 43 | + public static void createCloudStorageSubscription( |
| 44 | + String projectId, |
| 45 | + String topicId, |
| 46 | + String subscriptionId, |
| 47 | + String bucket, |
| 48 | + String filenamePrefix, |
| 49 | + String filenameSuffix, |
| 50 | + Duration maxDuration) |
| 51 | + throws IOException { |
| 52 | + try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) { |
| 53 | + |
| 54 | + ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); |
| 55 | + ProjectSubscriptionName subscriptionName = |
| 56 | + ProjectSubscriptionName.of(projectId, subscriptionId); |
| 57 | + |
| 58 | + CloudStorageConfig cloudStorageConfig = |
| 59 | + CloudStorageConfig.newBuilder() |
| 60 | + .setBucket(bucket) |
| 61 | + .setFilenamePrefix(filenamePrefix) |
| 62 | + .setFilenameSuffix(filenameSuffix) |
| 63 | + .setMaxDuration(maxDuration) |
| 64 | + .build(); |
| 65 | + |
| 66 | + Subscription subscription = |
| 67 | + subscriptionAdminClient.createSubscription( |
| 68 | + Subscription.newBuilder() |
| 69 | + .setName(subscriptionName.toString()) |
| 70 | + .setTopic(topicName.toString()) |
| 71 | + .setCloudStorageConfig(cloudStorageConfig) |
| 72 | + .build()); |
| 73 | + |
| 74 | + System.out.println("Created a CloudStorage subscription: " + subscription.getAllFields()); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +// [END pubsub_create_cloud_storage_subscription] |
0 commit comments