Download a public object without credentials

Download an object from a public data set 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 
 ; 
 []( 
 std 
 :: 
 string 
  
 const 
&  
 bucket_name 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 object_name 
 ) 
  
 { 
  
 // Create a client that does not authenticate with the server. 
  
 auto 
  
 client 
  
 = 
  
 gcs 
 :: 
 Client 
 { 
  
 google 
 :: 
 cloud 
 :: 
 Options 
 {}. 
 set<google 
 :: 
 cloud 
 :: 
 UnifiedCredentialsOption 
> ( 
  
 google 
 :: 
 cloud 
 :: 
 MakeInsecureCredentials 
 ())}; 
  
 // Read an object, the object must have been made public. 
  
 gcs 
 :: 
 ObjectReadStream 
  
 stream 
  
 = 
  
 client 
 . 
 ReadObject 
 ( 
 bucket_name 
 , 
  
 object_name 
 ); 
  
 int 
  
 count 
  
 = 
  
 0 
 ; 
  
 std 
 :: 
 string 
  
 line 
 ; 
  
 while 
  
 ( 
 std 
 :: 
 getline 
 ( 
 stream 
 , 
  
 line 
 , 
  
 '\n' 
 )) 
  
 { 
  
 ++ 
 count 
 ; 
  
 } 
  
 if 
  
 ( 
 stream 
 . 
 bad 
 ()) 
  
 throw 
  
 google 
 :: 
 cloud 
 :: 
 Status 
 ( 
 stream 
 . 
 status 
 ()); 
  
 std 
 :: 
 cout 
 << 
 "The object 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.Cloud.Storage.V1 
 
 ; 
 using 
  
 System 
 ; 
 using 
  
 System.IO 
 ; 
 public 
  
 class 
  
 DownloadPublicFileSample 
 { 
  
 public 
  
 string 
  
 DownloadPublicFile 
 ( 
  
 string 
  
 bucketName 
  
 = 
  
 "your-bucket-name" 
 , 
  
 string 
  
 objectName 
  
 = 
  
 "your-object-name" 
 , 
  
 string 
  
 localPath 
  
 = 
  
 "path/to/download/object/to" 
 ) 
  
 { 
  
 var 
  
 storage 
  
 = 
  
  StorageClient 
 
 . 
  CreateUnauthenticated 
 
 (); 
  
 using 
  
 var 
  
 outputFile 
  
 = 
  
 File 
 . 
 OpenWrite 
 ( 
 localPath 
 ); 
  
 storage 
 . 
 DownloadObject 
 ( 
 bucketName 
 , 
  
 objectName 
 , 
  
 outputFile 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 $"Downloaded public file {objectName} from bucket {bucketName} to {localPath}." 
 ); 
  
 return 
  
 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" 
  
 "time" 
  
 "cloud.google.com/go/storage" 
  
 "google.golang.org/api/option" 
 ) 
 // downloadPublicFile downloads a public object. 
 func 
  
 downloadPublicFile 
 ( 
 w 
  
 io 
 . 
  Writer 
 
 , 
  
 bucket 
 , 
  
 object 
  
 string 
 ) 
  
 ([] 
 byte 
 , 
  
 error 
 ) 
  
 { 
  
 // bucket := "bucket-name" 
  
 // object := "object-name" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 // Create a client that does not authenticate with the server. 
  
 client 
 , 
  
 err 
  
 := 
  
 storage 
 . 
 NewClient 
 ( 
 ctx 
 , 
  
 option 
 . 
 WithoutAuthentication 
 ()) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 nil 
 , 
  
 fmt 
 . 
 Errorf 
 ( 
 "storage.NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 ctx 
 , 
  
 cancel 
  
 := 
  
 context 
 . 
 WithTimeout 
 ( 
 ctx 
 , 
  
 time 
 . 
 Second 
 * 
 50 
 ) 
  
 defer 
  
 cancel 
 () 
  
 rc 
 , 
  
 err 
  
 := 
  
 client 
 . 
  Bucket 
 
 ( 
 bucket 
 ). 
  Object 
 
 ( 
 object 
 ). 
  NewReader 
 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 nil 
 , 
  
 fmt 
 . 
 Errorf 
 ( 
 "Object(%q).NewReader: %w" 
 , 
  
 object 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 rc 
 . 
 Close 
 () 
  
 data 
 , 
  
 err 
  
 := 
  
 io 
 . 
 ReadAll 
 ( 
 rc 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 nil 
 , 
  
 fmt 
 . 
 Errorf 
 ( 
 "io.ReadAll: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Blob %v downloaded.\n" 
 , 
  
 object 
 ) 
  
 return 
  
 data 
 , 
  
 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. BlobId 
 
 ; 
 import 
  
 com.google.cloud.storage. Storage 
 
 ; 
 import 
  
 com.google.cloud.storage. StorageOptions 
 
 ; 
 import 
  
 java.nio.file.Path 
 ; 
 public 
  
 class 
 DownloadPublicObject 
  
 { 
  
 public 
  
 static 
  
 void 
  
 downloadPublicObject 
 ( 
  
 String 
  
 bucketName 
 , 
  
 String 
  
 publicObjectName 
 , 
  
 Path 
  
 destFilePath 
 ) 
  
 { 
  
 // The name of the bucket to access 
  
 // String bucketName = "my-bucket"; 
  
 // The name of the remote public file to download 
  
 // String publicObjectName = "publicfile.txt"; 
  
 // The path to which the file should be downloaded 
  
 // Path destFilePath = Paths.get("/local/path/to/file.txt"); 
  
 // Instantiate an anonymous Google Cloud Storage client, which can only access public files 
  
  Storage 
 
  
 storage 
  
 = 
  
  StorageOptions 
 
 . 
  getUnauthenticatedInstance 
 
 (). 
  getService 
 
 (); 
  
 storage 
 . 
  downloadTo 
 
 ( 
 BlobId 
 . 
 of 
 ( 
 bucketName 
 , 
  
 publicObjectName 
 ), 
  
 destFilePath 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
  
 "Downloaded public object " 
  
 + 
  
 publicObjectName 
  
 + 
  
 " from bucket name " 
  
 + 
  
 bucketName 
  
 + 
  
 " to " 
  
 + 
  
 destFilePath 
 ); 
  
 } 
 } 
 

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 srcFilename = 'your-file-name'; 
 // 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 
  
 downloadPublicFile 
 () 
  
 { 
  
 const 
  
 options 
  
 = 
  
 { 
  
 destination 
 : 
  
 destFileName 
 , 
  
 }; 
  
 // Download public file. 
  
 await 
  
 storage 
 . 
 bucket 
 ( 
 bucketName 
 ). 
 file 
 ( 
 srcFileName 
 ). 
  download 
 
 ( 
 options 
 ); 
  
 console 
 . 
 log 
 ( 
  
 `Downloaded public file 
 ${ 
 srcFileName 
 } 
 from bucket name 
 ${ 
 bucketName 
 } 
 to 
 ${ 
 destFileName 
 } 
 ` 
  
 ); 
 } 
 downloadPublicFile 
 (). 
 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; 
 /** 
 * Download a public file using anonymous credentials. 
 * 
 * @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 string $destination The local destination to save the object. 
 *        (e.g. '/home/admin/downloads/my-object') 
 */ 
 function download_public_file(string $bucketName, string $objectName, string $destination): void 
 { 
 // create a storage client without authentication 
 $storage = new StorageClient([ 
 ]); 
 $bucket = $storage->bucket($bucketName); 
 $object = $bucket->object($objectName); 
 // set `shouldSignRequest` to false to force the client to not authenticate. 
 // if you do not have any client configuration enabled (i.e. application 
 // default credentials), that option can be omitted. 
 $object->downloadToFile($destination, [ 
 'shouldSignRequest' => false, 
 ]); 
 printf( 
 'Downloaded public object %s from bucket %s to %s', 
 $objectName, 
 $bucketName, 
 $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_public_file 
 ( 
 bucket_name 
 , 
 source_blob_name 
 , 
 destination_file_name 
 ): 
  
 """Downloads a public blob from the bucket.""" 
 # bucket_name = "your-bucket-name" 
 # source_blob_name = "storage-object-name" 
 # destination_file_name = "local/path/to/file" 
 storage_client 
 = 
  storage 
 
 . 
  Client 
 
 . 
  create_anonymous_client 
 
 () 
 bucket 
 = 
 storage_client 
 . 
 bucket 
 ( 
 bucket_name 
 ) 
 blob 
 = 
 bucket 
 . 
 blob 
 ( 
 source_blob_name 
 ) 
 blob 
 . 
  download_to_filename 
 
 ( 
 destination_file_name 
 ) 
 print 
 ( 
 "Downloaded public blob 
 {} 
 from bucket 
 {} 
 to 
 {} 
 ." 
 . 
 format 
 ( 
 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 .

  def 
  
 download_public_file 
  
 bucket_name 
 :, 
  
 file_name 
 :, 
  
 local_file_path 
 : 
  
 # The name of the bucket to access 
  
 # bucket_name = "my-bucket" 
  
 # The name of the remote public file to download 
  
 # file_name = "publicfile.txt" 
  
 # 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 
 
 . 
  anonymous 
 
  
 bucket 
  
 = 
  
 storage 
 . 
 bucket 
  
 bucket_name 
 , 
  
 skip_lookup 
 : 
  
 true 
  
 file 
  
 = 
  
 bucket 
 . 
  file 
 
  
 file_name 
  
 file 
 . 
  download 
 
  
 local_file_path 
  
 puts 
  
 "Downloaded public object 
 #{ 
 file 
 . 
 name 
 } 
 from bucket 
 #{ 
 bucket 
 } 
 to 
 #{ 
 local_file_path 
 } 
 " 
 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: