Skip to content

Commit ffbf1f7

Browse files
Sample for Storage Bucket Notifications (GoogleCloudPlatform#1610)
## Goal Implement four samples: ``` storage_print_pubsub_bucket_notification storage_create_bucket_notifications storage_delete_bucket_notification storage_list_bucket_notifications ``` ## Change Summary 1. Added a new sample for `storage_create_bucket_notifications` 2. Added tests for the sample in `BucketNotificationsTest`, will support all `storage_*_bucket_notifications` samples 3. Added pubsub in composer, otherwise a clean test run will fail. ## Tests ``` ➜ storage git:(storage_bucket_notifications) ✗ XDEBUG_MODE=coverage ../testing/vendor/bin/phpunit --verbose -c phpunit.xml.dist test/BucketNotificationsTest.php PHPUnit 8.5.23 by Sebastian Bergmann and contributors. Runtime: PHP 7.4.28 with Xdebug 3.1.2 Configuration: /usr/local/google/home/vishwarajanand/github/php-docs-samples/storage/phpunit.xml.dist .... 4 / 4 (100%) Time: 1.66 minutes, Memory: 14.00 MB OK (4 tests, 7 assertions) Generating code coverage report in Clover XML format ... done [247 ms] ➜ storage git:(storage_bucket_notifications) ``` > Squashed following commits: * untested create bucket notifications sample * Sample for creating notification * Tests ready * nit fix removed redundant null check * Addressing PR comments * Fixed filename to match region tag * nit fix added a new line after print statements * Added new samples * Print notification sample and test * Added tests for delete bucket notifications sample * feat: [Storage] complex upload download samples (GoogleCloudPlatform#1606) * feat(spanner): sample for copyBackup (GoogleCloudPlatform#1568) * nit fix linting errors * Fixing indentation in composer.json * nit fix linting errors * nit fix minor indentation issues Co-authored-by: Saransh Dhingra
1 parent 39855f1 commit ffbf1f7

6 files changed

+455
-0
lines changed

storage/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"paragonie/random_compat": "^9.0.0"
55
},
66
"require-dev": {
7+
"google/cloud-pubsub": "^1.31",
78
"guzzlehttp/guzzle": "^7.0"
89
}
910
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
/**
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_create_bucket_notifications]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Creates a notification configuration for a bucket.
31+
* This sample is used on this page:
32+
* https://cloud.google.com/storage/docs/reporting-changes
33+
*
34+
* @param string $bucketName The name of your Cloud Storage bucket.
35+
* @param string $topicName The name of the topic you would like to create a notification.
36+
*/
37+
function create_bucket_notifications(
38+
string $bucketName,
39+
string $topicName
40+
): void {
41+
// $bucketName = 'my-bucket';
42+
// $topicName = 'my-topic';
43+
44+
$storage = new StorageClient();
45+
$bucket = $storage->bucket($bucketName);
46+
$notification = $bucket->createNotification($topicName);
47+
48+
printf(
49+
'Successfully created notification with ID %s for bucket %s in topic %s' . PHP_EOL,
50+
$notification->id(),
51+
$bucketName,
52+
$topicName
53+
);
54+
}
55+
# [END storage_create_bucket_notifications]
56+
57+
// The following 2 lines are only needed to run the samples
58+
require_once __DIR__ . '/../../testing/sample_helpers.php';
59+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
/**
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_delete_bucket_notification]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Deletes a notification configuration for a bucket.
31+
* This sample is used on this page:
32+
* https://cloud.google.com/storage/docs/reporting-changes
33+
*
34+
* @param string $bucketName The name of your Cloud Storage bucket.
35+
* @param string $notificationId The ID of the notification.
36+
*/
37+
function delete_bucket_notifications(
38+
string $bucketName,
39+
string $notificationId
40+
): void {
41+
// $bucketName = 'your-bucket';
42+
// $notificationId = 'your-notification-id';
43+
44+
$storage = new StorageClient();
45+
$bucket = $storage->bucket($bucketName);
46+
$notification = $bucket->notification($notificationId);
47+
$notification->delete();
48+
49+
printf(
50+
'Successfully deleted notification with ID %s for bucket %s' . PHP_EOL,
51+
$notification->id(),
52+
$bucketName
53+
);
54+
}
55+
# [END storage_delete_bucket_notification]
56+
57+
// The following 2 lines are only needed to run the samples
58+
require_once __DIR__ . '/../../testing/sample_helpers.php';
59+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
/**
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_list_bucket_notifications]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Lists notification configurations for a bucket.
31+
* This sample is used on this page:
32+
* https://cloud.google.com/storage/docs/reporting-changes
33+
*
34+
* @param string $bucketName The name of your Cloud Storage bucket.
35+
*/
36+
function list_bucket_notifications(
37+
string $bucketName
38+
): void {
39+
// $bucketName = 'your-bucket';
40+
41+
$storage = new StorageClient();
42+
$bucket = $storage->bucket($bucketName);
43+
$notifications = $bucket->notifications();
44+
45+
foreach ($notifications as $notification) {
46+
printf('Found notification with id %s' . PHP_EOL, $notification->id());
47+
}
48+
printf(
49+
'Listed %s notifications of storage bucket %s.' . PHP_EOL,
50+
iterator_count($notifications),
51+
$bucketName,
52+
);
53+
}
54+
# [END storage_list_bucket_notifications]
55+
56+
// The following 2 lines are only needed to run the samples
57+
require_once __DIR__ . '/../../testing/sample_helpers.php';
58+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
/**
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_print_pubsub_bucket_notification]
27+
28+
use Google\Cloud\Storage\StorageClient;
29+
30+
/**
31+
* Lists notification configurations for a bucket.
32+
* This sample is used on this page:
33+
* https://cloud.google.com/storage/docs/reporting-changes
34+
*
35+
* @param string $bucketName The name of your Cloud Storage bucket.
36+
* @param string $notificationId The ID of the notification.
37+
*/
38+
function print_pubsub_bucket_notification(
39+
string $bucketName,
40+
string $notificationId
41+
): void {
42+
// $bucketName = 'your-bucket';
43+
// $notificationId = 'your-notification-id';
44+
45+
$storage = new StorageClient();
46+
$bucket = $storage->bucket($bucketName);
47+
$notification = $bucket->notification($notificationId);
48+
$notificationInfo = $notification->info();
49+
50+
printf(
51+
<<
52+
Notification ID: %s
53+
Topic Name: %s
54+
Event Types: %s
55+
Custom Attributes: %s
56+
Payload Format: %s
57+
Blob Name Prefix: %s
58+
Etag: %s
59+
Self Link: %s
60+
EOF . PHP_EOL,
61+
$notification->id(),
62+
$notificationInfo['topic'],
63+
$notificationInfo['event_types'] ?? '',
64+
$notificationInfo['custom_attributes'] ?? '',
65+
$notificationInfo['payload_format'],
66+
$notificationInfo['blob_name_prefix'] ?? '',
67+
$notificationInfo['etag'],
68+
$notificationInfo['selfLink']
69+
);
70+
}
71+
# [END storage_print_pubsub_bucket_notification]
72+
73+
// The following 2 lines are only needed to run the samples
74+
require_once __DIR__ . '/../../testing/sample_helpers.php';
75+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)