Download an object using a byte range

Download an object using a byte range from a Cloud Storage bucket.

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 
 ; 
 []( 
 gcs 
 :: 
 Client 
  
 client 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 bucket_name 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 object_name 
 , 
  
 std 
 :: 
 int64_t 
  
 start 
 , 
  
 std 
 :: 
 int64_t 
  
 end 
 ) 
  
 { 
  
 gcs 
 :: 
 ObjectReadStream 
  
 stream 
  
 = 
  
 client 
 . 
 ReadObject 
 ( 
 bucket_name 
 , 
  
 object_name 
 , 
  
 gcs 
 :: 
 ReadRange 
 ( 
 start 
 , 
  
 end 
 )); 
  
 int 
  
 count 
  
 = 
  
 0 
 ; 
  
 std 
 :: 
 string 
  
 line 
 ; 
  
 while 
  
 ( 
 std 
 :: 
 getline 
 ( 
 stream 
 , 
  
 line 
 , 
  
 '\n' 
 )) 
  
 { 
  
 std 
 :: 
 cout 
 << 
 line 
 << 
 " 
 \n 
 " 
 ; 
  
 ++ 
 count 
 ; 
  
 } 
  
 if 
  
 ( 
 stream 
 . 
 bad 
 ()) 
  
 throw 
  
 google 
 :: 
 cloud 
 :: 
 Status 
 ( 
 stream 
 . 
 status 
 ()); 
  
 std 
 :: 
 cout 
 << 
 "The requested range has " 
 << 
 count 
 << 
 " lines 
 \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.Apis.Storage.v1 
 ; 
 using 
  
  Google.Cloud.Storage.V1 
 
 ; 
 using 
  
 System 
 ; 
 using 
  
 System.IO 
 ; 
 using 
  
 System.Net.Http 
 ; 
 using 
  
 System.Net.Http.Headers 
 ; 
 using 
  
 System.Threading.Tasks 
 ; 
 public 
  
 class 
  
 DownloadByteRangeAsyncSample 
 { 
  
 public 
  
 async 
  
 Task 
  
 DownloadByteRangeAsync 
 ( 
  
 string 
  
 bucketName 
  
 = 
  
 "your-unique-bucket-name" 
 , 
  
 string 
  
 objectName 
  
 = 
  
 "my-file-name" 
 , 
  
 long 
  
 firstByte 
  
 = 
  
 0 
 , 
  
 long 
  
 lastByte 
  
 = 
  
 20 
 , 
  
 string 
  
 localPath 
  
 = 
  
 "my-local-path/my-file-name" 
 ) 
  
 { 
  
 var 
  
 storageClient 
  
 = 
  
  StorageClient 
 
 . 
  Create 
 
 (); 
  
 // Create an HTTP request for the media, for a limited byte range. 
  
 StorageService 
  
 storage 
  
 = 
  
 storageClient 
 . 
 Service 
 ; 
  
 var 
  
 uri 
  
 = 
  
 new 
  
 Uri 
 ( 
 $"{storage.BaseUri}b/{bucketName}/o/{objectName}?alt=media" 
 ); 
  
 var 
  
 request 
  
 = 
  
 new 
  
 HttpRequestMessage 
  
 { 
  
 RequestUri 
  
 = 
  
 uri 
  
 }; 
  
 request 
 . 
 Headers 
 . 
  Range 
 
  
 = 
  
 new 
  
 RangeHeaderValue 
 ( 
 firstByte 
 , 
  
 lastByte 
 ); 
  
 using 
  
 var 
  
 outputFile 
  
 = 
  
 File 
 . 
 OpenWrite 
 ( 
 localPath 
 ); 
  
 // Use the HttpClient in the storage object because it supplies 
  
 // all the authentication headers we need. 
  
 var 
  
 response 
  
 = 
  
 await 
  
 storage 
 . 
 HttpClient 
 . 
 SendAsync 
 ( 
 request 
 ); 
  
 await 
  
 response 
 . 
 Content 
 . 
 CopyToAsync 
 ( 
 outputFile 
 , 
  
 null 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 $"Downloaded {objectName} to {localPath}." 
 ); 
  
 } 
 } 
 

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" 
  
 "os" 
  
 "time" 
  
 "cloud.google.com/go/storage" 
 ) 
 // downloadByteRange downloads a specific byte range of an object to a file. 
 func 
  
 downloadByteRange 
 ( 
 w 
  
 io 
 . 
  Writer 
 
 , 
  
 bucket 
 , 
  
 object 
  
 string 
 , 
  
 startByte 
  
 int64 
 , 
  
 endByte 
  
 int64 
 , 
  
 destFileName 
  
 string 
 ) 
  
 error 
  
 { 
  
 // bucket := "bucket-name" 
  
 // object := "object-name" 
  
 // startByte := 0 
  
 // endByte := 20 
  
 // destFileName := "file.txt" 
  
 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 
 * 
 50 
 ) 
  
 defer 
  
 cancel 
 () 
  
 f 
 , 
  
 err 
  
 := 
  
 os 
 . 
  Create 
 
 ( 
 destFileName 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "os.Create: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 length 
  
 := 
  
 endByte 
  
 - 
  
 startByte 
  
 rc 
 , 
  
 err 
  
 := 
  
 client 
 . 
  Bucket 
 
 ( 
 bucket 
 ). 
  Object 
 
 ( 
 object 
 ). 
  NewRangeReader 
 
 ( 
 ctx 
 , 
  
 startByte 
 , 
  
 length 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "Object(%q).NewReader: %w" 
 , 
  
 object 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 rc 
 . 
 Close 
 () 
  
 if 
  
 _ 
 , 
  
 err 
  
 := 
  
 io 
 . 
 Copy 
 ( 
 f 
 , 
  
 rc 
 ); 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "io.Copy: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 if 
  
 err 
  
 = 
  
 f 
 . 
 Close 
 (); 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "f.Close: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Bytes %v to %v of blob %v downloaded to local file %v\n" 
 , 
  
 startByte 
 , 
  
 startByte 
 + 
 length 
 , 
  
 object 
 , 
  
 destFileName 
 ) 
  
 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. ReadChannel 
 
 ; 
 import 
  
 com.google.cloud.storage. BlobId 
 
 ; 
 import 
  
 com.google.cloud.storage. Storage 
 
 ; 
 import 
  
 com.google.cloud.storage. StorageOptions 
 
 ; 
 import 
  
 com.google.common.io.ByteStreams 
 ; 
 import 
  
 java.io.IOException 
 ; 
 import 
  
 java.nio.channels.FileChannel 
 ; 
 import 
  
 java.nio.file.Paths 
 ; 
 import 
  
 java.nio.file.StandardOpenOption 
 ; 
 public 
  
 class 
 DownloadByteRange 
  
 { 
  
 public 
  
 static 
  
 void 
  
 downloadByteRange 
 ( 
  
  String 
 
  
 projectId 
 , 
  
  String 
 
  
 bucketName 
 , 
  
  String 
 
  
 blobName 
 , 
  
 long 
  
 startByte 
 , 
  
 long 
  
 endBytes 
 , 
  
  String 
 
  
 destFileName 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
 // The ID of your GCP project 
  
 // String projectId = "your-project-id"; 
  
 // The ID of your GCS bucket 
  
 // String bucketName = "your-unique-bucket-name"; 
  
 // The name of the blob/file that you wish to modify permissions on 
  
 // String blobName = "your-blob-name"; 
  
 // The starting byte at which to begin the download 
  
 // long startByte = 0; 
  
 // The ending byte at which to end the download 
  
 // long endByte = 20; 
  
 // The path to which the file should be downloaded 
  
 // String destFileName = '/local/path/to/file.txt'; 
  
  Storage 
 
  
 storage 
  
 = 
  
  StorageOptions 
 
 . 
 newBuilder 
 (). 
 setProjectId 
 ( 
 projectId 
 ). 
 build 
 (). 
 getService 
 (); 
  
  BlobId 
 
  
 blobId 
  
 = 
  
  BlobId 
 
 . 
 of 
 ( 
 bucketName 
 , 
  
 blobName 
 ); 
  
 try 
  
 ( 
  ReadChannel 
 
  
 from 
  
 = 
  
 storage 
 . 
  reader 
 
 ( 
 blobId 
 ); 
  
 FileChannel 
  
 to 
  
 = 
  
 FileChannel 
 . 
 open 
 ( 
 Paths 
 . 
 get 
 ( 
 destFileName 
 ), 
  
 StandardOpenOption 
 . 
 WRITE 
 )) 
  
 { 
  
 from 
 . 
 seek 
 ( 
 startByte 
 ); 
  
 from 
 . 
 limit 
 ( 
 endBytes 
 ); 
  
 ByteStreams 
 . 
 copy 
 ( 
 from 
 , 
  
 to 
 ); 
  
 System 
 . 
 out 
 . 
 printf 
 ( 
  
 "%s downloaded to %s from byte %d to byte %d%n" 
 , 
  
 blobId 
 . 
  toGsUtilUri 
 
 (), 
  
 destFileName 
 , 
  
 startByte 
 , 
  
 endBytes 
 ); 
  
 } 
  
 } 
 } 
 

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'; 
 // The starting byte at which to begin the download 
 // const startByte = 0; 
 // The ending byte at which to end the download 
 // const endByte = 20; 
 // The path to which the file should be downloaded 
 // const destFileName = '/local/path/to/file.txt'; 
 // Imports the Google Cloud client library 
 const 
  
 { 
 Storage 
 } 
  
 = 
  
 require 
 ( 
 ' @google-cloud/storage 
' 
 ); 
 // Creates a client 
 const 
  
 storage 
  
 = 
  
 new 
  
 Storage 
 (); 
 async 
  
 function 
  
 downloadByteRange 
 () 
  
 { 
  
 const 
  
 options 
  
 = 
  
 { 
  
 destination 
 : 
  
 destFileName 
 , 
  
 start 
 : 
  
 startByte 
 , 
  
 end 
 : 
  
 endByte 
 , 
  
 }; 
  
 // Downloads the file from the starting byte to the ending byte specified in options 
  
 await 
  
 storage 
 . 
 bucket 
 ( 
 bucketName 
 ). 
 file 
 ( 
 fileName 
 ). 
  download 
 
 ( 
 options 
 ); 
  
 console 
 . 
 log 
 ( 
  
 `gs:// 
 ${ 
 bucketName 
 } 
 / 
 ${ 
 fileName 
 } 
 downloaded to 
 ${ 
 destFileName 
 } 
 from byte 
 ${ 
 startByte 
 } 
 to byte 
 ${ 
 endByte 
 } 
 .` 
  
 ); 
 } 
 downloadByteRange 
 (); 
 

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; 
 /** 
 * Download a byte range from Cloud Storage and save it as a local file. 
 * 
 * @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') 
 * @param int $startByte The starting byte at which to begin the download. 
 *        (e.g. 1) 
 * @param int $endByte The ending byte at which to end the download. (e.g. 5) 
 * @param string $destination The local destination to save the object. 
 *        (e.g. '/path/to/your/file') 
 */ 
 function download_byte_range( 
 string $bucketName, 
 string $objectName, 
 int $startByte, 
 int $endByte, 
 string $destination 
 ): void { 
 $storage = new StorageClient(); 
 $bucket = $storage->bucket($bucketName); 
 $object = $bucket->object($objectName); 
 $object->downloadToFile($destination, [ 
 'restOptions' => [ 
 'headers' => [ 
 'Range' => "bytes=$startByte-$endByte", 
 ], 
 ], 
 ]); 
 printf( 
 'Downloaded gs://%s/%s to %s' . PHP_EOL, 
 $bucketName, 
 $objectName, 
 basename($destination) 
 ); 
 } 
 

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 
  
 download_byte_range 
 ( 
 bucket_name 
 , 
 source_blob_name 
 , 
 start_byte 
 , 
 end_byte 
 , 
 destination_file_name 
 ): 
  
 """Downloads a blob from the bucket.""" 
 # The ID of your GCS bucket 
 # bucket_name = "your-bucket-name" 
 # The ID of your GCS object 
 # source_blob_name = "storage-object-name" 
 # The starting byte at which to begin the download 
 # start_byte = 0 
 # The ending byte at which to end the download 
 # end_byte = 20 
 # The path to which the file should be downloaded 
 # destination_file_name = "local/path/to/file" 
 storage_client 
 = 
  storage 
 
 . 
  Client 
 
 () 
 bucket 
 = 
 storage_client 
 . 
  bucket 
 
 ( 
 bucket_name 
 ) 
 # Construct a client side representation of a blob. 
 # Note `Bucket.blob` differs from `Bucket.get_blob` as it doesn't retrieve 
 # any content from Google Cloud Storage. As we don't need additional data, 
 # using `Bucket.blob` is preferred here. 
 blob 
 = 
 bucket 
 . 
 blob 
 ( 
 source_blob_name 
 ) 
 blob 
 . 
  download_to_filename 
 
 ( 
 destination_file_name 
 , 
 start 
 = 
 start_byte 
 , 
 end 
 = 
 end_byte 
 ) 
 print 
 ( 
 "Downloaded bytes 
 {} 
 to 
 {} 
 of object 
 {} 
 from bucket 
 {} 
 to local file 
 {} 
 ." 
 . 
 format 
 ( 
 start_byte 
 , 
 end_byte 
 , 
 source_blob_name 
 , 
 bucket_name 
 , 
 destination_file_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 .

  # The ID of your GCS bucket 
 # bucket_name = "your-unique-bucket-name" 
 # file_name = "Name of a file in the Storage bucket" 
 # The starting byte at which to begin the download 
 # start_byte = 0 
 # The ending byte at which to end the download 
 # end_byte = 20 
 # The path to which the file should be downloaded 
 # local_file_path = "/local/path/to/file.txt" 
 require 
  
 "google/cloud/storage" 
 storage 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  Storage 
 
 . 
  new 
 
 bucket 
  
 = 
  
 storage 
 . 
 bucket 
  
 bucket_name 
 file 
  
 = 
  
 bucket 
 . 
  file 
 
  
 file_name 
 file 
 . 
  download 
 
  
 local_file_path 
 , 
  
 range 
 : 
  
 start_byte 
 .. 
 end_byte 
 puts 
  
 "Downloaded bytes 
 #{ 
 start_byte 
 } 
 to 
 #{ 
 end_byte 
 } 
 of object 
 #{ 
 file_name 
 } 
 from bucket 
 #{ 
 bucket_name 
 } 
 " 
  
 \ 
  
 + 
  
 " to local file 
 #{ 
 local_file_path 
 } 
 ." 
 

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: