diff --git a/storagebatchoperations/README.md b/storagebatchoperations/README.md new file mode 100644 index 000000000..5ed579182 --- /dev/null +++ b/storagebatchoperations/README.md @@ -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/php/docs/reference/cloud-storagebatchoperations/latest +[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) diff --git a/storagebatchoperations/composer.json b/storagebatchoperations/composer.json new file mode 100644 index 000000000..e4f2639c5 --- /dev/null +++ b/storagebatchoperations/composer.json @@ -0,0 +1,8 @@ +{ + "require": { + "google/cloud-storagebatchoperations": "0.1.1" + }, + "require-dev": { + "google/cloud-storage": "^1.48.1" + } +} diff --git a/storagebatchoperations/phpunit.xml.dist b/storagebatchoperations/phpunit.xml.dist new file mode 100644 index 000000000..e6e259d21 --- /dev/null +++ b/storagebatchoperations/phpunit.xml.dist @@ -0,0 +1,23 @@ + + + + + ./src + + + ./vendor + + + + + + + + test + + + + + + + diff --git a/storagebatchoperations/src/cancel_job.php b/storagebatchoperations/src/cancel_job.php new file mode 100644 index 000000000..b89503a86 --- /dev/null +++ b/storagebatchoperations/src/cancel_job.php @@ -0,0 +1,59 @@ +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); diff --git a/storagebatchoperations/src/create_job.php b/storagebatchoperations/src/create_job.php new file mode 100644 index 000000000..5c57ac77f --- /dev/null +++ b/storagebatchoperations/src/create_job.php @@ -0,0 +1,76 @@ +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); diff --git a/storagebatchoperations/src/delete_job.php b/storagebatchoperations/src/delete_job.php new file mode 100644 index 000000000..6c1621e3a --- /dev/null +++ b/storagebatchoperations/src/delete_job.php @@ -0,0 +1,59 @@ +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); diff --git a/storagebatchoperations/src/get_job.php b/storagebatchoperations/src/get_job.php new file mode 100644 index 000000000..f6e4438ea --- /dev/null +++ b/storagebatchoperations/src/get_job.php @@ -0,0 +1,59 @@ +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); diff --git a/storagebatchoperations/src/list_jobs.php b/storagebatchoperations/src/list_jobs.php new file mode 100644 index 000000000..68161b628 --- /dev/null +++ b/storagebatchoperations/src/list_jobs.php @@ -0,0 +1,58 @@ +locationName($projectId, 'global'); + + $request = new ListJobsRequest([ + 'parent' => $parent, + ]); + + $jobs = $storageBatchOperationsClient->listJobs($request); + + foreach ($jobs as $job) { + printf('Job name: %s' . PHP_EOL, $job->getName()); + } +} +# [END storage_batch_list_jobs] + +// 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); diff --git a/storagebatchoperations/test/StorageBatchOperationsTest.php b/storagebatchoperations/test/StorageBatchOperationsTest.php new file mode 100644 index 000000000..0eb22636d --- /dev/null +++ b/storagebatchoperations/test/StorageBatchOperationsTest.php @@ -0,0 +1,170 @@ +locationName(self::$projectId, 'global'); + self::$jobName = self::$parent . '/jobs/' . self::$jobId; + + self::$bucket = self::$storage->createBucket(sprintf('php-gcs-sbo-sample-%s', $uniqueBucketId)); + + $objectName = self::$objectPrefix . '-object-1.txt'; + self::$bucket->upload('test content', ['name' => $objectName]); + + } + + public static function tearDownAfterClass(): void + { + foreach (self::$bucket->objects(['versions' => true]) as $object) { + $object->delete(); + } + self::$bucket->delete(); + } + + public function testCreateJob() + { + $output = $this->runFunctionSnippet('create_job', [ + self::$projectId, self::$jobId, self::$bucket->name(), self::$objectPrefix + ]); + + $this->assertStringContainsString( + sprintf('Created job: %s', self::$parent), + $output + ); + } + + /** + * @depends testCreateJob + */ + public function testGetJob() + { + $output = $this->runFunctionSnippet('get_job', [ + self::$projectId, self::$jobId + ]); + + $this->assertStringContainsString( + self::$jobName, + $output + ); + } + + /** + * @depends testGetJob + */ + public function testListJobs() + { + $output = $this->runFunctionSnippet('list_jobs', [ + self::$projectId + ]); + + $this->assertStringContainsString( + self::$jobName, + $output + ); + } + + /** + * @depends testListJobs + */ + public function testCancelJob() + { + $output = $this->runFunctionSnippet('cancel_job', [ + self::$projectId, self::$jobId + ]); + + $this->assertStringContainsString( + sprintf('Cancelled job: %s', self::$jobName), + $output + ); + } + + /** + * @depends testCancelJob + */ + public function testDeleteJob() + { + $attempt = 0; + $maxAttempts = 10; + $jobReadyForDeletion = false; + while ($attempt < $maxAttempts && !$jobReadyForDeletion) { + $attempt++; + $request = new GetJobRequest([ + 'name' => self::$jobName, + ]); + + $response = self::$storageBatchOperationsClient->getJob($request); + $state = $response->getState(); + $status = \Google\Cloud\StorageBatchOperations\V1\Job\State::name($state); + + // A job is typically deletable if it's not in a creating/pending/running state + // Consider PENDING or IN_PROGRESS as states to wait out. + // For immediate deletion, maybe it needs to be SUCCEEDED or FAILED or CANCELED. + if ($status !== 'STATE_UNSPECIFIED' && $status !== 'RUNNING') { + $jobReadyForDeletion = true; + } + + if (!$jobReadyForDeletion && $attempt < $maxAttempts) { + sleep(10); // Wait 10 seconds + } + } + + if (!$jobReadyForDeletion) { + $this->fail('Job did not reach a deletable state within the allowed time.'); + } + + // Now attempt to delete the job + $output = $this->runFunctionSnippet('delete_job', [ + self::$projectId, self::$jobId + ]); + + $this->assertStringContainsString( + sprintf('Deleted job: %s', self::$jobName), + $output + ); + } +}