Skip to content

Commit 851664c

Browse files
feat(StorageInsights): Adding samples (#1988)
1 parent 16bd04c commit 851664c

9 files changed

+580
-0
lines changed

storageinsights/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Google Cloud Storage Insights Samples
2+
3+
## Description
4+
5+
All code in the snippets directory demonstrate how to invoke
6+
[Cloud Storage Insights][cloud-storage-insights] from PHP.
7+
8+
[cloud-storage-insights]: https://cloud.google.com/storage/docs/insights/inventory-reports
9+
10+
## Setup:
11+
12+
1. **Enable APIs** - [Enable the Storage Insights Service API](https://console.cloud.google.com/flows/enableapi?apiid=storageinsights.googleapis.com)
13+
and create a new project or select an existing project.
14+
2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click "New Credentials"
15+
and select "Service Account Key". Create a new service account, use the JSON key type, and
16+
select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`
17+
to the path of the JSON key that was downloaded.
18+
3. **Clone the repo** and cd into this directory
19+
20+
```sh
21+
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
22+
$ cd php-docs-samples/storageinsights
23+
```
24+
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
25+
Run `php composer.phar install` (if composer is installed locally) or `composer install`
26+
(if composer is installed globally).
27+
28+
29+
## Samples
30+
31+
To run the Storage Insights Samples, run any of the files in `src/` on the CLI:
32+
33+
```
34+
$ php src/create_inventory_report_config.php
35+
36+
Usage: create_inventory_report_config.php $bucketName $sourceGcsBucketName $sinkGcsBucketName
37+
38+
@param string $projectId The Project ID
39+
@param string $location The location of bucket
40+
@param string $sourceBucketName The Storage bucket name
41+
@param string $destinationBucketName The Storage bucket name
42+
```
43+
44+
## The client library
45+
46+
This sample uses the [Cloud Storage Insights Client Library for PHP][google-cloud-php-storage-insights].
47+
You can read the documentation for more details on API usage and use GitHub
48+
to [browse the source][google-cloud-php-source] and [report issues][google-cloud-php-issues].
49+
50+
[google-cloud-php-storage-insights]: https://cloud.google.com/storage/docs/insights/inventory-reports
51+
[google-cloud-php-source]: https://github.com/GoogleCloudPlatform/google-cloud-php
52+
[google-cloud-php-issues]: https://github.com/GoogleCloudPlatform/google-cloud-php/issues
53+
[google-cloud-sdk]: https://cloud.google.com/sdk/
54+
55+
## Contributing changes
56+
57+
* See [CONTRIBUTING.md](../../CONTRIBUTING.md)
58+
59+
## Licensing
60+
61+
* See [LICENSE](../../LICENSE)

storageinsights/composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"require": {
3+
"google/cloud-storageinsights": "^0.3.2"
4+
},
5+
"require-dev": {
6+
"google/cloud-storage": "^1.41.0"
7+
}
8+
}

storageinsights/phpunit.xml.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="../testing/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">./src</directory>
6+
</include>
7+
<exclude>
8+
<directory>./vendor</directory>
9+
</exclude>
10+
<report>
11+
<clover outputFile="build/logs/clover.xml"/>
12+
</report>
13+
</coverage>
14+
<testsuites>
15+
<testsuite name="PHP storageinsights test">
16+
<directory>test</directory>
17+
</testsuite>
18+
</testsuites>
19+
<logging/>
20+
<php>
21+
<env name="PHPUNIT_TESTS" value="1"/>
22+
</php>
23+
</phpunit>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Google Inc.
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+
namespace Google\Cloud\Samples\StorageInsights;
19+
20+
# [START storageinsights_create_inventory_report_config]
21+
use Google\Type\Date;
22+
use Google\Cloud\StorageInsights\V1\CSVOptions;
23+
use Google\Cloud\StorageInsights\V1\ReportConfig;
24+
use Google\Cloud\StorageInsights\V1\FrequencyOptions;
25+
use Google\Cloud\StorageInsights\V1\CloudStorageFilters;
26+
use Google\Cloud\StorageInsights\V1\StorageInsightsClient;
27+
use Google\Cloud\StorageInsights\V1\ObjectMetadataReportOptions;
28+
use Google\Cloud\StorageInsights\V1\CloudStorageDestinationOptions;
29+
30+
/**
31+
* Creates an inventory report config.
32+
* Example:
33+
* ```
34+
* create_inventory_report_config($projectId, $bucketLocation, $sourceBucket, $destinationBucket);
35+
* ```
36+
* @param string $projectId Your Google Cloud Project ID
37+
* @param string $bucketLocation The location of your source and destination buckets
38+
* @param string $sourceBucket The name of your Google Cloud Storage source bucket
39+
* @param string $destinationBucket The name of your Google Cloud Storage destination bucket
40+
*/
41+
function create_inventory_report_config(
42+
string $projectId,
43+
string $bucketLocation,
44+
string $sourceBucket,
45+
string $destinationBucket
46+
): void {
47+
$storageInsightsClient = new StorageInsightsClient();
48+
49+
$reportConfig = (new ReportConfig())
50+
->setDisplayName('Example inventory report configuration')
51+
->setFrequencyOptions((new FrequencyOptions())
52+
->setFrequency(FrequencyOptions\Frequency::WEEKLY)
53+
->setStartDate((new Date())
54+
->setDay(15)
55+
->setMonth(8)
56+
->setYear(3023))
57+
->setEndDate((new Date())
58+
->setDay(15)
59+
->setMonth(9)
60+
->setYear(3023)))
61+
->setCsvOptions((new CSVOptions())
62+
->setDelimiter(',')
63+
->setRecordSeparator("\n")
64+
->setHeaderRequired(true))
65+
->setObjectMetadataReportOptions((new ObjectMetadataReportOptions())
66+
->setMetadataFields(['project', 'name', 'bucket'])
67+
->setStorageFilters((new CloudStorageFilters())
68+
->setBucket($sourceBucket))
69+
->setStorageDestinationOptions((new CloudStorageDestinationOptions())
70+
->setBucket($destinationBucket)));
71+
72+
$formattedParent = $storageInsightsClient->locationName($projectId, $bucketLocation);
73+
$response = $storageInsightsClient->createReportConfig($formattedParent, $reportConfig);
74+
75+
print('Created inventory report config with name:' . PHP_EOL);
76+
print($response->getName());
77+
}
78+
# [END storageinsights_create_inventory_report_config]
79+
80+
// The following 2 lines are only needed to run the samples
81+
require_once __DIR__ . '/../../testing/sample_helpers.php';
82+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Google Inc.
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+
namespace Google\Cloud\Samples\StorageInsights;
19+
20+
# [START storageinsights_delete_inventory_report_config]
21+
use Google\Cloud\StorageInsights\V1\StorageInsightsClient;
22+
23+
/**
24+
* Delete an inventory report config.
25+
* Example:
26+
* ```
27+
* delete_inventory_report_config($projectId, $bucketLocation, $inventoryReportConfigUuid);
28+
* ```
29+
*
30+
* @param string $projectId Your Google Cloud Project ID
31+
* @param string $bucketLocation The location of your bucket
32+
* @param string $inventoryReportConfigUuid The UUID of the inventory report config you want to delete
33+
*/
34+
function delete_inventory_report_config(
35+
string $projectId,
36+
string $bucketLocation,
37+
string $inventoryReportConfigUuid
38+
): void {
39+
$storageInsightsClient = new StorageInsightsClient();
40+
41+
$reportConfigName = $storageInsightsClient->reportConfigName($projectId, $bucketLocation, $inventoryReportConfigUuid);
42+
$storageInsightsClient->deleteReportConfig($reportConfigName);
43+
44+
printf('Deleted inventory report config with name %s' . PHP_EOL, $reportConfigName);
45+
}
46+
# [END storageinsights_delete_inventory_report_config]
47+
48+
// The following 2 lines are only needed to run the samples
49+
require_once __DIR__ . '/../../testing/sample_helpers.php';
50+
\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+
<?php
2+
/**
3+
* Copyright 2024 Google Inc.
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+
namespace Google\Cloud\Samples\StorageInsights;
19+
20+
# [START storageinsights_edit_inventory_report_config]
21+
use Google\Cloud\StorageInsights\V1\StorageInsightsClient;
22+
use Google\Protobuf\FieldMask;
23+
24+
/**
25+
* Update an existing inventory report config.
26+
* Example:
27+
* ```
28+
* edit_inventory_report_config($projectId, $bucketLocation, $inventoryReportConfigUuid);
29+
* ```
30+
*
31+
* @param string $projectId Your Google Cloud Project ID
32+
* @param string $bucketLocation The location of your bucket
33+
* @param string $inventoryReportConfigUuid The UUID of the inventory report config to edit
34+
*/
35+
function edit_inventory_report_config(
36+
string $projectId,
37+
string $bucketLocation,
38+
string $inventoryReportConfigUuid
39+
): void {
40+
$storageInsightsClient = new StorageInsightsClient();
41+
42+
$reportConfigName = $storageInsightsClient->reportConfigName($projectId, $bucketLocation, $inventoryReportConfigUuid);
43+
$reportConfig = $storageInsightsClient->getReportConfig($reportConfigName);
44+
45+
// Set any other fields you want to update here
46+
$updatedReportConfig = $reportConfig->setDisplayName('Updated Display Name');
47+
$updateMask = new FieldMask([
48+
'paths' => ['display_name']
49+
]);
50+
51+
$storageInsightsClient->updateReportConfig($updateMask, $updatedReportConfig);
52+
53+
printf('Edited inventory report config with name %s' . PHP_EOL, $reportConfigName);
54+
}
55+
# [END storageinsights_edit_inventory_report_config]
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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Google Inc.
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+
namespace Google\Cloud\Samples\StorageInsights;
19+
20+
# [START storageinsights_get_inventory_report_names]
21+
use Google\Cloud\StorageInsights\V1\StorageInsightsClient;
22+
23+
/**
24+
* Gets an existing inventory report config.
25+
* Example:
26+
* ```
27+
* get_inventory_report_names($projectId, $bucketLocation, $inventoryReportConfigUuid);
28+
* ```
29+
*
30+
* @param string $projectId Your Google Cloud Project ID
31+
* @param string $bucketLocation The location your bucket is in
32+
* @param string $inventoryReportConfigUuid The UUID of the inventory report you want to get file names for
33+
*/
34+
function get_inventory_report_names(
35+
string $projectId,
36+
string $bucketLocation,
37+
string $inventoryReportConfigUuid
38+
): void {
39+
$storageInsightsClient = new StorageInsightsClient();
40+
41+
$reportConfigName = $storageInsightsClient->reportConfigName($projectId, $bucketLocation, $inventoryReportConfigUuid);
42+
$reportConfig = $storageInsightsClient->getReportConfig($reportConfigName);
43+
$extension = $reportConfig->hasCsvOptions() ? 'csv' : 'parquet';
44+
print('You can use the Google Cloud Storage Client '
45+
. 'to download the following objects from Google Cloud Storage:' . PHP_EOL);
46+
$listReportConfigs = $storageInsightsClient->listReportDetails($reportConfig->getName());
47+
foreach ($listReportConfigs->iterateAllElements() as $reportDetail) {
48+
for ($index = $reportDetail->getShardsCount() - 1; $index >= 0; $index--) {
49+
printf('%s%d.%s' . PHP_EOL, $reportDetail->getReportPathPrefix(), $index, $extension);
50+
}
51+
}
52+
}
53+
# [END storageinsights_get_inventory_report_names]
54+
55+
// The following 2 lines are only needed to run the samples
56+
require_once __DIR__ . '/../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Google Inc.
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+
namespace Google\Cloud\Samples\StorageInsights;
19+
20+
# [START storageinsights_list_inventory_report_configs]
21+
use Google\Cloud\StorageInsights\V1\StorageInsightsClient;
22+
23+
/**
24+
* Lists inventory report configs.
25+
* Example:
26+
* ```
27+
* list_inventory_report_configs($projectId, $location);
28+
* ```
29+
*
30+
* @param string $projectId Your Google Cloud Project ID
31+
* @param string $location The location to list configs in
32+
*/
33+
function list_inventory_report_configs(string $projectId, string $location): void
34+
{
35+
$storageInsightsClient = new StorageInsightsClient();
36+
37+
$formattedParent = $storageInsightsClient->locationName($projectId, $location);
38+
$configs = $storageInsightsClient->listReportConfigs($formattedParent);
39+
40+
printf('Inventory report configs in project %s and location %s:' . PHP_EOL, $projectId, $location);
41+
foreach ($configs->iterateAllElements() as $config) {
42+
printf('%s' . PHP_EOL, $config->getName());
43+
}
44+
}
45+
# [END storageinsights_list_inventory_report_configs]
46+
47+
// The following 2 lines are only needed to run the samples
48+
require_once __DIR__ . '/../../testing/sample_helpers.php';
49+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)