-
Notifications
You must be signed in to change notification settings - Fork 1k
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
thiyaguk09
wants to merge
2
commits into
GoogleCloudPlatform:main
Choose a base branch
from
thiyaguk09:storage-batch-operations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+573
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
[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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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