Set a temporary hold on an object

An example of how to set a temporary hold on an object.

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 .

  namespace 
  
 gcs 
  
 = 
  
 :: 
 google 
 :: 
 cloud 
 :: 
 storage 
 ; 
 using 
  
 :: 
 google 
 :: 
 cloud 
 :: 
 StatusOr 
 ; 
 []( 
 gcs 
 :: 
 Client 
  
 client 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 bucket_name 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 object_name 
 ) 
  
 { 
  
 StatusOr<gcs 
 :: 
 ObjectMetadata 
>  
 original 
  
 = 
  
 client 
 . 
 GetObjectMetadata 
 ( 
 bucket_name 
 , 
  
 object_name 
 ); 
  
 if 
  
 ( 
 ! 
 original 
 ) 
  
 throw 
  
 std 
 :: 
 move 
 ( 
 original 
 ). 
 status 
 (); 
  
 StatusOr<gcs 
 :: 
 ObjectMetadata 
>  
 updated 
  
 = 
  
 client 
 . 
 PatchObject 
 ( 
  
 bucket_name 
 , 
  
 object_name 
 , 
  
 gcs 
 :: 
 ObjectMetadataPatchBuilder 
 (). 
 SetTemporaryHold 
 ( 
 true 
 ), 
  
 gcs 
 :: 
 IfMetagenerationMatch 
 ( 
 original 
 - 
> metageneration 
 ())); 
  
 if 
  
 ( 
 ! 
 updated 
 ) 
  
 throw 
  
 std 
 :: 
 move 
 ( 
 updated 
 ). 
 status 
 (); 
  
 std 
 :: 
 cout 
 << 
 "The temporary hold for object " 
 << 
 updated 
 - 
> name 
 () 
 << 
 " in bucket " 
 << 
 updated 
 - 
> bucket 
 () 
 << 
 " is " 
 << 
 ( 
 updated 
 - 
> temporary_hold 
 () 
  
 ? 
  
 "enabled" 
  
 : 
  
 "disabled" 
 ) 
 << 
 " 
 \n 
 " 
 ; 
 } 
 

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 .

  using 
  
  Google.Cloud.Storage.V1 
 
 ; 
 public 
  
 class 
  
 SetTemporaryHoldSample 
 { 
  
 public 
  
 void 
  
 SetTemporaryHold 
 ( 
  
 string 
  
 bucketName 
  
 = 
  
 "your-unique-bucket-name" 
 , 
  
 string 
  
 objectName 
  
 = 
  
 "your-object-name" 
 ) 
  
 { 
  
 var 
  
 storage 
  
 = 
  
  StorageClient 
 
 . 
  Create 
 
 (); 
  
 var 
  
 storageObject 
  
 = 
  
 storage 
 . 
 GetObject 
 ( 
 bucketName 
 , 
  
 objectName 
 ); 
  
 storageObject 
 . 
 TemporaryHold 
  
 = 
  
 true 
 ; 
  
 storage 
 . 
 UpdateObject 
 ( 
 storageObject 
 ); 
  
 System 
 . 
 Console 
 . 
 WriteLine 
 ( 
 $"Temporary hold was set for {objectName}." 
 ); 
  
 } 
 } 
 

Go

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

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

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 "time" 
  
 "cloud.google.com/go/storage" 
 ) 
 // setTemporaryHold sets TemporaryHold flag of an object to true. 
 func 
  
 setTemporaryHold 
 ( 
 w 
  
 io 
 . 
  Writer 
 
 , 
  
 bucket 
 , 
  
 object 
  
 string 
 ) 
  
 error 
  
 { 
  
 // bucket := "bucket-name" 
  
 // object := "object-name" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 storage 
 . 
 NewClient 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "storage.NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 ctx 
 , 
  
 cancel 
  
 := 
  
 context 
 . 
 WithTimeout 
 ( 
 ctx 
 , 
  
 time 
 . 
 Second 
 * 
 10 
 ) 
  
 defer 
  
 cancel 
 () 
  
 o 
  
 := 
  
 client 
 . 
  Bucket 
 
 ( 
 bucket 
 ). 
  Object 
 
 ( 
 object 
 ) 
  
 // Optional: set a metageneration-match precondition to avoid potential race 
  
 // conditions and data corruptions. The request to update is aborted if the 
  
 // object's metageneration does not match your precondition. 
  
 attrs 
 , 
  
 err 
  
 := 
  
 o 
 . 
 Attrs 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "object.Attrs: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 o 
  
 = 
  
 o 
 . 
 If 
 ( 
 storage 
 . 
  Conditions 
 
 { 
 MetagenerationMatch 
 : 
  
 attrs 
 . 
 Metageneration 
 }) 
  
 // Update the object to set the object hold. 
  
 objectAttrsToUpdate 
  
 := 
  
 storage 
 . 
  ObjectAttrsToUpdate 
 
 { 
  
 TemporaryHold 
 : 
  
 true 
 , 
  
 } 
  
 if 
  
 _ 
 , 
  
 err 
  
 := 
  
 o 
 . 
 Update 
 ( 
 ctx 
 , 
  
 objectAttrsToUpdate 
 ); 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "Object(%q).Update: %w" 
 , 
  
 object 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Temporary hold was enabled for %v.\n" 
 , 
  
 object 
 ) 
  
 return 
  
 nil 
 } 
 

Java

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

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

  import 
  
 com.google.cloud.storage. Blob 
 
 ; 
 import 
  
 com.google.cloud.storage. BlobId 
 
 ; 
 import 
  
 com.google.cloud.storage. Storage 
 
 ; 
 import 
  
 com.google.cloud.storage. StorageException 
 
 ; 
 import 
  
 com.google.cloud.storage. StorageOptions 
 
 ; 
 public 
  
 class 
 SetTemporaryHold 
  
 { 
  
 public 
  
 static 
  
 void 
  
 setTemporaryHold 
 ( 
 String 
  
 projectId 
 , 
  
 String 
  
 bucketName 
 , 
  
 String 
  
 objectName 
 ) 
  
 throws 
  
  StorageException 
 
  
 { 
  
 // The ID of your GCP project 
  
 // String projectId = "your-project-id"; 
  
 // The ID of your GCS bucket 
  
 // String bucketName = "your-unique-bucket-name"; 
  
 // The ID of your GCS object 
  
 // String objectName = "your-object-name"; 
  
  Storage 
 
  
 storage 
  
 = 
  
  StorageOptions 
 
 . 
 newBuilder 
 (). 
 setProjectId 
 ( 
 projectId 
 ). 
 build 
 (). 
  getService 
 
 (); 
  
  BlobId 
 
  
 blobId 
  
 = 
  
  BlobId 
 
 . 
 of 
 ( 
 bucketName 
 , 
  
 objectName 
 ); 
  
  Blob 
 
  
 blob 
  
 = 
  
 storage 
 . 
  get 
 
 ( 
 blobId 
 ); 
  
 if 
  
 ( 
 blob 
  
 == 
  
 null 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "The object " 
  
 + 
  
 objectName 
  
 + 
  
 " was not found in " 
  
 + 
  
 bucketName 
 ); 
  
 return 
 ; 
  
 } 
  
 // Optional: set a generation-match precondition to avoid potential race 
  
 // conditions and data corruptions. The request to upload returns a 412 error if 
  
 // the object's generation number does not match your precondition. 
  
  Storage 
 
 . 
 BlobTargetOption 
  
 precondition 
  
 = 
  
  Storage 
 
 . 
 BlobTargetOption 
 . 
 generationMatch 
 (); 
  
 blob 
 . 
  toBuilder 
 
 (). 
 setTemporaryHold 
 ( 
 true 
 ). 
 build 
 (). 
 update 
 ( 
 precondition 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Temporary hold was set for " 
  
 + 
  
 objectName 
 ); 
  
 } 
 } 
 

Node.js

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

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

  /** 
 * TODO(developer): Uncomment the following lines before running the sample. 
 */ 
 // The ID of your GCS bucket 
 // const bucketName = 'your-unique-bucket-name'; 
 // The ID of your GCS file 
 // const fileName = 'your-file-name'; 
 // Imports the Google Cloud client library 
 const 
  
 { 
 Storage 
 } 
  
 = 
  
 require 
 ( 
 ' @google-cloud/storage 
' 
 ); 
 // Creates a client 
 const 
  
 storage 
  
 = 
  
 new 
  
 Storage 
 (); 
 async 
  
 function 
  
 setTemporaryHold 
 () 
  
 { 
  
 // Optional: set a meta-generation-match precondition to avoid potential race 
  
 // conditions and data corruptions. The request to set metadata is aborted if the 
  
 // object's metageneration number does not match your precondition. 
  
 const 
  
 options 
  
 = 
  
 { 
  
 ifMetagenerationMatch 
 : 
  
 metagenerationMatchPrecondition 
 , 
  
 }; 
  
 await 
  
 storage 
 . 
 bucket 
 ( 
 bucketName 
 ). 
 file 
 ( 
 fileName 
 ). 
 setMetadata 
 ( 
  
 { 
  
 temporaryHold 
 : 
  
 true 
 , 
  
 }, 
  
 options 
  
 ); 
  
 console 
 . 
 log 
 ( 
 `Temporary hold was set for 
 ${ 
 fileName 
 } 
 .` 
 ); 
 } 
 setTemporaryHold 
 (). 
 catch 
 ( 
 console 
 . 
 error 
 ); 
 

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\Storage\StorageClient; 
 /** 
 * Sets a temporary hold for an object. 
 * 
 * @param string $bucketName The name of your Cloud Storage bucket. 
 *        (e.g. 'my-bucket') 
 * @param string $objectName The name of your Cloud Storage object. 
 *        (e.g. 'my-object') 
 */ 
 function set_temporary_hold(string $bucketName, string $objectName): void 
 { 
 $storage = new StorageClient(); 
 $bucket = $storage->bucket($bucketName); 
 $object = $bucket->object($objectName); 
 $object->update(['temporaryHold' => true]); 
 printf('Temporary hold was set for %s' . PHP_EOL, $objectName); 
 } 
 

Python

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

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

  from 
  
 google.cloud 
  
 import 
  storage 
 
 def 
  
 set_temporary_hold 
 ( 
 bucket_name 
 , 
 blob_name 
 ): 
  
 """Sets a temporary hold on a given blob""" 
 # bucket_name = "my-bucket" 
 # blob_name = "my-blob" 
 storage_client 
 = 
  storage 
 
 . 
  Client 
 
 () 
 bucket 
 = 
 storage_client 
 . 
  bucket 
 
 ( 
 bucket_name 
 ) 
 blob 
 = 
 bucket 
 . 
 blob 
 ( 
 blob_name 
 ) 
 metageneration_match_precondition 
 = 
 None 
 # Optional: set a metageneration-match precondition to avoid potential race 
 # conditions and data corruptions. The request to patch is aborted if the 
 # object's metageneration does not match your precondition. 
 blob 
 . 
 reload 
 () 
 # Fetch blob metadata to use in metageneration_match_precondition. 
 metageneration_match_precondition 
 = 
 blob 
 . 
 metageneration 
 blob 
 . 
  temporary_hold 
 
 = 
 True 
 blob 
 . 
 patch 
 ( 
 if_metageneration_match 
 = 
 metageneration_match_precondition 
 ) 
 print 
 ( 
 "Temporary hold was set for # 
 {blob_name} 
 " 
 ) 
 

Ruby

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

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

  def 
  
 set_temporary_hold 
  
 bucket_name 
 :, 
  
 file_name 
 : 
  
 # The ID of your GCS bucket 
  
 # bucket_name = "your-unique-bucket-name" 
  
 # The ID of your GCS object 
  
 # file_name = "your-file-name" 
  
 require 
  
 "google/cloud/storage" 
  
 storage 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  Storage 
 
 . 
  new 
 
  
 bucket 
  
 = 
  
 storage 
 . 
 bucket 
  
 bucket_name 
 , 
  
 skip_lookup 
 : 
  
 true 
  
 file 
  
 = 
  
 bucket 
 . 
  file 
 
  
 file_name 
  
 file 
 . 
  set_temporary_hold! 
 
  
 puts 
  
 "Temporary hold was set for 
 #{ 
 file_name 
 } 
 ." 
 end 
 

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: