Storage Batch Create Job

Create a storage batch operations job

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C++

For more information, see the Cloud Storage C++ API reference documentation .

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  []( 
 google 
 :: 
 cloud 
 :: 
 storagebatchoperations_v1 
 :: 
 StorageBatchOperationsClient 
  
 client 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 project_id 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 job_id 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 target_bucket_name 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 object_prefix 
 ) 
  
 { 
  
 auto 
  
 const 
  
 parent 
  
 = 
  
 std 
 :: 
 string 
 { 
 "projects/" 
 } 
  
 + 
  
 project_id 
  
 + 
  
 "/locations/global" 
 ; 
  
 namespace 
  
 sbo 
  
 = 
  
 google 
 :: 
 cloud 
 :: 
 storagebatchoperations 
 :: 
 v1 
 ; 
  
 sbo 
 :: 
 Job 
  
 job 
 ; 
  
 sbo 
 :: 
 BucketList 
 * 
  
 bucket_list 
  
 = 
  
 job 
 . 
 mutable_bucket_list 
 (); 
  
 sbo 
 :: 
 BucketList 
 :: 
 Bucket 
 * 
  
 bucket_config 
  
 = 
  
 bucket_list 
 - 
> add_buckets 
 (); 
  
 bucket_config 
 - 
> set_bucket 
 ( 
 target_bucket_name 
 ); 
  
 sbo 
 :: 
 PrefixList 
 * 
  
 prefix_list_config 
  
 = 
  
 bucket_config 
 - 
> mutable_prefix_list 
 (); 
  
 prefix_list_config 
 - 
> add_included_object_prefixes 
 ( 
 object_prefix 
 ); 
  
 sbo 
 :: 
 DeleteObject 
 * 
  
 delete_object_config 
  
 = 
  
 job 
 . 
 mutable_delete_object 
 (); 
  
 delete_object_config 
 - 
> set_permanent_object_deletion_enabled 
 ( 
 false 
 ); 
  
 auto 
  
 result 
  
 = 
  
 client 
 . 
 CreateJob 
 ( 
 parent 
 , 
  
 job 
 , 
  
 job_id 
 ). 
 get 
 (); 
  
 if 
  
 ( 
 ! 
 result 
 ) 
  
 throw 
  
 result 
 . 
 status 
 (); 
  
 std 
 :: 
 cout 
 << 
 "Created job: " 
 << 
 result 
 - 
> name 
 () 
 << 
 " 
 \n 
 " 
 ; 
 } 
 

PHP

For more information, see the Cloud Storage PHP API reference documentation .

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  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()); 
 } 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Create a Mobile Website
View Site in Mobile | Classic
Share by: