Downloading Objects from Google Cloud Storage

Downloading objects from Google Cloud Storage is similar to uploading. Below is the sample illustration.

Command Line

Step 1: Use the below command to download the object from gcs:

gsutil cp gs://[BUCKET_NAME]/[OBJECT_NAME] [LOCAL_DESTINATION_PATH]

The above command will download the object BUCKET_NAME/OBJECT_NAME to your LOCAL_DESTINATION_PATH.

Example: Let us download sample.txt to a sampleFolder from the bucket as below

gsutil cp gs://gfgbucket/sample.txt ./sampleFolder

Output

Copying gs://gfgbucket/sample.txt...
/ [1 files][ 22.0 B/ 22.0 B]
Operation completed over 1 objects/22.0 B.

API

Client Libraries:

Step 2: Use the below sample code written in Java and Python using Google Client Library to download your object.

Java




import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
  
import java.nio.file.Path;
import java.nio.file.Paths;
  
public class GCSDownloader {
  
    public static void main(String[] args) {
        // Replace these values with your actual GCP project ID and bucket name
        String projectId = "your-project-id";
        String bucketName = "your-bucket-name";
  
        // Name of the object in the Cloud Storage bucket that you want to download
        String objectName = "your-object-name";
  
        // Path to the local file where you want to save the downloaded file
        String localFilePath = "path/to/local/downloaded-file.txt";
  
        // Create a Storage client
        Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
  
        // Get the Blob reference and download the file
        Blob blob = storage.get(bucketName, objectName);
        blob.downloadTo(Paths.get(localFilePath));
  
        System.out.println("File downloaded from Cloud Storage: " + blob.getName());
    }
}


Python3




# Importing the Google Cloud client library
from google.cloud import storage
  
# Initializing the client
client = storage.Client()
  
# Getting the bucket
bucket = client.get_bucket('[BUCKET_NAME]')
  
# Getting the blob (object) in the bucket
blob = bucket.blob('[OBJECT_NAME]')
  
# Downloading the blob to a local file
blob.download_to_filename('[LOCAL_DESTINATION_PATH]')
  
print(f'File downloaded from Cloud Storage: {blob.name}')


Output:

File downloaded from Cloud Storage: sample.txt

REST API:

Step 3: Use below command to download object using cURL command.

curl -H "Authorization: Bearer [YOUR_ACCESS_TOKEN]"
"https://storage.googleapis.com/storage/v1/b/[BUCKET_NAME]/o/[OBJECT_NAME]?alt=media" -o [LOCAL_DESTINATION_PATH]

Example: Let us download sample.txt to our sampleFolder as sample2 from the bucket as below

curl -H "Authorization: Bearer [YOUR_ACCESS_TOKEN]" "https://storage.googleapis.com/storage/v1/b/gfgbucket/o/sample2?alt=media" -o ./sampleFolder

Output:

ESFQHGX2Mir3gF5erD8xeoc8M1QolA1w0171" "https://storage.googleapis.com/storage/v1/b/gfgbucket/o/sample2?alt=media" -o ./sampleFolder/sample2
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 22 100 22 0 0 87 0 --:--:-- --:--:-- --:--:-- 87

Uploading and Downloading Objects in Google Cloud Storage: Command Line and API

Cloud storage refers to storing your data in remote locations i.e., the cloud. Cloud Storage became increasingly popular in the information era. With tons of data in hand, storing them in the cloud is very affordable. By storing the data in the cloud, we can access the data at any time irrespective of the device and the location.

Similar Reads

Google Cloud Storage (GCS)

Google Cloud Storage is a service provided by the Google Cloud Platform(GCP) for cloud storage. GCP offers pay-per-use services. Apart from Cloud Storage, GCP offers many other services like hosting your websites in the cloud, running your own Operation System from the Cloud, and other cloud computing processes....

Objects in GCS

An object is any kind of data that you upload to Google Cloud Storage. For example, an object can be a file, image, video, application, etc. Every object has data, metadata, a unique identifier, etc. For more details like naming conventions etc., you can refer to your cloud storage service provider. (Here it is GCP)....

Pre-Requisites

A Google Cloud account with billing enabled Sample object to upload and download from GCS. (Here I am using a sample.txt file as the object) A basic idea on the command line and API. Have created your bucket in GCP....

Uploading Objects in Google Cloud Storage

Command Line...

Downloading Objects from Google Cloud Storage

...

FAQs on Uploading and Downloading Objects in Google Cloud Storage: Command Line and API

...

Contact Us