Skip to content

feat(StorageBatchOperations): Add storage batch operations jobs samples #2105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions storagebatchoperations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Google Cloud Storage Batch Operations Samples

## Description

All code in the snippets directory demonstrates how to invoke
[Cloud Storage Batch Operations][google-cloud-php-storage-batch-operations] from PHP.

[cloud-storage-batch-operations]: https://cloud.google.com/storage/docs/batch-operations/overview

## Setup:

1. **Enable APIs** - [Enable the Storage Batch Operations Service API](https://console.cloud.google.com/flows/enableapi?apiid=storage.googleapis.com)
and create a new project or select an existing project.
2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click "New Credentials"
and select "Service Account Key". Create a new service account, use the JSON key type, and
select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`
to the path of the JSON key that was downloaded.
3. **Clone the repo** and cd into this directory

```sh
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
$ cd php-docs-samples/storagebatchoperations
```
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
Run `php composer.phar install` (if composer is installed locally) or `composer install`
(if composer is installed globally).


## Samples

To run the Storage Batch Operations Samples, run any of the files in `src/` on the CLI:

```
$ php src/create_job.php

Usage: create_job.php $jobId $bucketName $objectPrefix

@param string $projectId The Project ID
@param string $jobId The new Job ID
@param string $bucketName The Storage bucket name
@param string $objectPrefix The Object prefix
```
## The client library
This sample uses the [Cloud Storage Batch Operations Client Library for PHP][google-cloud-php-storage-batch-operations].
You can read the documentation for more details on API usage and use GitHub
to [browse the source][google-cloud-php-source] and [report issues][google-cloud-php-issues].
[google-cloud-php-storage-batch-operations]: https://cloud.google.com/storage/docs/reference/rpc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this link correct @thiyaguk09 ? It links to an orphaned page

[google-cloud-php-source]: https://github.com/GoogleCloudPlatform/google-cloud-php
[google-cloud-php-issues]: https://github.com/GoogleCloudPlatform/google-cloud-php/issues
[google-cloud-sdk]: https://cloud.google.com/sdk/
## Contributing changes
* See [CONTRIBUTING.md](../../CONTRIBUTING.md)
## Licensing
* See [LICENSE](../../LICENSE)
8 changes: 8 additions & 0 deletions storagebatchoperations/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"require": {
"google/cloud-storagebatchoperations": "0.1.1"
},
"require-dev": {
"google/cloud-storage": "^1.48.1"
}
}
23 changes: 23 additions & 0 deletions storagebatchoperations/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="../testing/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<directory>./vendor</directory>
</exclude>
<report>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<testsuites>
<testsuite name="PHP storagebatchoperations test">
<directory>test</directory>
</testsuite>
</testsuites>
<logging/>
<php>
<env name="PHPUNIT_TESTS" value="1"/>
</php>
</phpunit>
59 changes: 59 additions & 0 deletions storagebatchoperations/src/cancel_job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagebatchoperations/README.md
*/

namespace Google\Cloud\Samples\StorageBatchOperations;

# [START storage_batch_cancel_job]
use Google\Cloud\StorageBatchOperations\V1\Client\StorageBatchOperationsClient;
use Google\Cloud\StorageBatchOperations\V1\CancelJobRequest;

/**
* Cancel a batch job.
*
* @param string $projectId Your Google Cloud project ID.
* (e.g. 'my-project-id')
* @param string $jobId A unique identifier for this job.
* (e.g. '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5')
*/
function cancel_job(string $projectId, string $jobId): void
{
// Create a client.
$storageBatchOperationsClient = new StorageBatchOperationsClient();

$parent = $storageBatchOperationsClient->locationName($projectId, 'global');
$formattedName = $parent . '/jobs/' . $jobId;

$request = new CancelJobRequest([
'name' => $formattedName,
]);

$storageBatchOperationsClient->cancelJob($request);

printf('Cancelled job: %s', $formattedName);
}
# [END storage_batch_cancel_job]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
76 changes: 76 additions & 0 deletions storagebatchoperations/src/create_job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagebatchoperations/README.md
*/

namespace Google\Cloud\Samples\StorageBatchOperations;

# [START storage_batch_create_job]
use Google\Cloud\StorageBatchOperations\V1\Client\StorageBatchOperationsClient;
use Google\Cloud\StorageBatchOperations\V1\CreateJobRequest;
use Google\Cloud\StorageBatchOperations\V1\Job;
use Google\Cloud\StorageBatchOperations\V1\BucketList;
use Google\Cloud\StorageBatchOperations\V1\BucketList\Bucket;
use Google\Cloud\StorageBatchOperations\V1\PrefixList;
use Google\Cloud\StorageBatchOperations\V1\DeleteObject;

/**
* Create a new batch job.
*
* @param string $projectId Your Google Cloud project ID.
* (e.g. 'my-project-id')
* @param string $jobId A unique identifier for this job.
* (e.g. '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5')
* @param string $bucketName The name of your Cloud Storage bucket to operate on.
* (e.g. 'my-bucket')
* @param string $objectPrefix The prefix of objects to include in the operation.
* (e.g. 'prefix1')
*/
function create_job(string $projectId, string $jobId, string $bucketName, string $objectPrefix): void
{
// Create a client.
$storageBatchOperationsClient = new StorageBatchOperationsClient();

$parent = $storageBatchOperationsClient->locationName($projectId, 'global');

$prefixListConfig = new PrefixList(['included_object_prefixes' => [$objectPrefix]]);
$bucket = new Bucket(['bucket' => $bucketName, 'prefix_list' => $prefixListConfig]);
$bucketList = new BucketList(['buckets' => [$bucket]]);

$deleteObject = new DeleteObject(['permanent_object_deletion_enabled' => false]);

$job = new Job(['bucket_list' => $bucketList, 'delete_object' => $deleteObject]);

$request = new CreateJobRequest([
'parent' => $parent,
'job_id' => $jobId,
'job' => $job,
]);
$response = $storageBatchOperationsClient->createJob($request);

printf('Created job: %s', $response->getName());
}
# [END storage_batch_create_job]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
59 changes: 59 additions & 0 deletions storagebatchoperations/src/delete_job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagebatchoperations/README.md
*/

namespace Google\Cloud\Samples\StorageBatchOperations;

# [START storage_batch_delete_job]
use Google\Cloud\StorageBatchOperations\V1\Client\StorageBatchOperationsClient;
use Google\Cloud\StorageBatchOperations\V1\DeleteJobRequest;

/**
* Delete a batch job.
*
* @param string $projectId Your Google Cloud project ID.
* (e.g. 'my-project-id')
* @param string $jobId A unique identifier for this job.
* (e.g. '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5')
*/
function delete_job(string $projectId, string $jobId): void
{
// Create a client.
$storageBatchOperationsClient = new StorageBatchOperationsClient();

$parent = $storageBatchOperationsClient->locationName($projectId, 'global');
$formattedName = $parent . '/jobs/' . $jobId;

$request = new DeleteJobRequest([
'name' => $formattedName,
]);

$storageBatchOperationsClient->deleteJob($request);

printf('Deleted job: %s', $formattedName);
}
# [END storage_batch_delete_job]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
59 changes: 59 additions & 0 deletions storagebatchoperations/src/get_job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagebatchoperations/README.md
*/

namespace Google\Cloud\Samples\StorageBatchOperations;

# [START storage_batch_get_job]
use Google\Cloud\StorageBatchOperations\V1\Client\StorageBatchOperationsClient;
use Google\Cloud\StorageBatchOperations\V1\GetJobRequest;

/**
* Gets a batch job.
*
* @param string $projectId Your Google Cloud project ID.
* (e.g. 'my-project-id')
* @param string $jobId A unique identifier for this job.
* (e.g. '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5')
*/
function get_job(string $projectId, string $jobId): void
{
// Create a client.
$storageBatchOperationsClient = new StorageBatchOperationsClient();

$parent = $storageBatchOperationsClient->locationName($projectId, 'global');
$formattedName = $parent . '/jobs/' . $jobId;

$request = new GetJobRequest([
'name' => $formattedName,
]);

$response = $storageBatchOperationsClient->getJob($request);

printf('Got job: %s', $response->getName());
}
# [END storage_batch_get_job]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Loading