Skip to content

Commit 2e00b47

Browse files
authored
feat(StorageBatchOperations): Add storage batch operations jobs samples (#2105)
1 parent ab4b611 commit 2e00b47

File tree

9 files changed

+573
-0
lines changed

9 files changed

+573
-0
lines changed

storagebatchoperations/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Google Cloud Storage Batch Operations Samples
2+
3+
## Description
4+
5+
All code in the snippets directory demonstrates how to invoke
6+
[Cloud Storage Batch Operations][google-cloud-php-storage-batch-operations] from PHP.
7+
8+
[cloud-storage-batch-operations]: https://cloud.google.com/storage/docs/batch-operations/overview
9+
10+
## Setup:
11+
12+
1. **Enable APIs** - [Enable the Storage Batch Operations Service API](https://console.cloud.google.com/flows/enableapi?apiid=storage.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/storagebatchoperations
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 Batch Operations Samples, run any of the files in `src/` on the CLI:
32+
33+
```
34+
$ php src/create_job.php
35+
36+
Usage: create_job.php $jobId $bucketName $objectPrefix
37+
38+
@param string $projectId The Project ID
39+
@param string $jobId The new Job ID
40+
@param string $bucketName The Storage bucket name
41+
@param string $objectPrefix The Object prefix
42+
```
43+
44+
## The client library
45+
46+
This sample uses the [Cloud Storage Batch Operations Client Library for PHP][google-cloud-php-storage-batch-operations].
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-batch-operations]: https://cloud.google.com/php/docs/reference/cloud-storagebatchoperations/latest
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)

storagebatchoperations/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-storagebatchoperations": "0.1.1"
4+
},
5+
"require-dev": {
6+
"google/cloud-storage": "^1.48.1"
7+
}
8+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
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 storagebatchoperations 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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagebatchoperations/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\StorageBatchOperations;
26+
27+
# [START storage_batch_cancel_job]
28+
use Google\Cloud\StorageBatchOperations\V1\Client\StorageBatchOperationsClient;
29+
use Google\Cloud\StorageBatchOperations\V1\CancelJobRequest;
30+
31+
/**
32+
* Cancel a batch job.
33+
*
34+
* @param string $projectId Your Google Cloud project ID.
35+
* (e.g. 'my-project-id')
36+
* @param string $jobId A unique identifier for this job.
37+
* (e.g. '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5')
38+
*/
39+
function cancel_job(string $projectId, string $jobId): void
40+
{
41+
// Create a client.
42+
$storageBatchOperationsClient = new StorageBatchOperationsClient();
43+
44+
$parent = $storageBatchOperationsClient->locationName($projectId, 'global');
45+
$formattedName = $parent . '/jobs/' . $jobId;
46+
47+
$request = new CancelJobRequest([
48+
'name' => $formattedName,
49+
]);
50+
51+
$storageBatchOperationsClient->cancelJob($request);
52+
53+
printf('Cancelled job: %s', $formattedName);
54+
}
55+
# [END storage_batch_cancel_job]
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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagebatchoperations/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\StorageBatchOperations;
26+
27+
# [START storage_batch_create_job]
28+
use Google\Cloud\StorageBatchOperations\V1\Client\StorageBatchOperationsClient;
29+
use Google\Cloud\StorageBatchOperations\V1\CreateJobRequest;
30+
use Google\Cloud\StorageBatchOperations\V1\Job;
31+
use Google\Cloud\StorageBatchOperations\V1\BucketList;
32+
use Google\Cloud\StorageBatchOperations\V1\BucketList\Bucket;
33+
use Google\Cloud\StorageBatchOperations\V1\PrefixList;
34+
use Google\Cloud\StorageBatchOperations\V1\DeleteObject;
35+
36+
/**
37+
* Create a new batch job.
38+
*
39+
* @param string $projectId Your Google Cloud project ID.
40+
* (e.g. 'my-project-id')
41+
* @param string $jobId A unique identifier for this job.
42+
* (e.g. '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5')
43+
* @param string $bucketName The name of your Cloud Storage bucket to operate on.
44+
* (e.g. 'my-bucket')
45+
* @param string $objectPrefix The prefix of objects to include in the operation.
46+
* (e.g. 'prefix1')
47+
*/
48+
function create_job(string $projectId, string $jobId, string $bucketName, string $objectPrefix): void
49+
{
50+
// Create a client.
51+
$storageBatchOperationsClient = new StorageBatchOperationsClient();
52+
53+
$parent = $storageBatchOperationsClient->locationName($projectId, 'global');
54+
55+
$prefixListConfig = new PrefixList(['included_object_prefixes' => [$objectPrefix]]);
56+
$bucket = new Bucket(['bucket' => $bucketName, 'prefix_list' => $prefixListConfig]);
57+
$bucketList = new BucketList(['buckets' => [$bucket]]);
58+
59+
$deleteObject = new DeleteObject(['permanent_object_deletion_enabled' => false]);
60+
61+
$job = new Job(['bucket_list' => $bucketList, 'delete_object' => $deleteObject]);
62+
63+
$request = new CreateJobRequest([
64+
'parent' => $parent,
65+
'job_id' => $jobId,
66+
'job' => $job,
67+
]);
68+
$response = $storageBatchOperationsClient->createJob($request);
69+
70+
printf('Created job: %s', $response->getName());
71+
}
72+
# [END storage_batch_create_job]
73+
74+
// The following 2 lines are only needed to run the samples
75+
require_once __DIR__ . '/../../testing/sample_helpers.php';
76+
\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+
/**
4+
* Copyright 2025 Google LLC
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagebatchoperations/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\StorageBatchOperations;
26+
27+
# [START storage_batch_delete_job]
28+
use Google\Cloud\StorageBatchOperations\V1\Client\StorageBatchOperationsClient;
29+
use Google\Cloud\StorageBatchOperations\V1\DeleteJobRequest;
30+
31+
/**
32+
* Delete a batch job.
33+
*
34+
* @param string $projectId Your Google Cloud project ID.
35+
* (e.g. 'my-project-id')
36+
* @param string $jobId A unique identifier for this job.
37+
* (e.g. '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5')
38+
*/
39+
function delete_job(string $projectId, string $jobId): void
40+
{
41+
// Create a client.
42+
$storageBatchOperationsClient = new StorageBatchOperationsClient();
43+
44+
$parent = $storageBatchOperationsClient->locationName($projectId, 'global');
45+
$formattedName = $parent . '/jobs/' . $jobId;
46+
47+
$request = new DeleteJobRequest([
48+
'name' => $formattedName,
49+
]);
50+
51+
$storageBatchOperationsClient->deleteJob($request);
52+
53+
printf('Deleted job: %s', $formattedName);
54+
}
55+
# [END storage_batch_delete_job]
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+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagebatchoperations/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\StorageBatchOperations;
26+
27+
# [START storage_batch_get_job]
28+
use Google\Cloud\StorageBatchOperations\V1\Client\StorageBatchOperationsClient;
29+
use Google\Cloud\StorageBatchOperations\V1\GetJobRequest;
30+
31+
/**
32+
* Gets a batch job.
33+
*
34+
* @param string $projectId Your Google Cloud project ID.
35+
* (e.g. 'my-project-id')
36+
* @param string $jobId A unique identifier for this job.
37+
* (e.g. '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5')
38+
*/
39+
function get_job(string $projectId, string $jobId): void
40+
{
41+
// Create a client.
42+
$storageBatchOperationsClient = new StorageBatchOperationsClient();
43+
44+
$parent = $storageBatchOperationsClient->locationName($projectId, 'global');
45+
$formattedName = $parent . '/jobs/' . $jobId;
46+
47+
$request = new GetJobRequest([
48+
'name' => $formattedName,
49+
]);
50+
51+
$response = $storageBatchOperationsClient->getJob($request);
52+
53+
printf('Got job: %s', $response->getName());
54+
}
55+
# [END storage_batch_get_job]
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);

0 commit comments

Comments
 (0)