Complete Guide to Uploading and Downloading Files from Google Cloud Storage Using C#
Google Cloud Storage (GCS) is a robust and scalable object storage service provided by Google Cloud. It allows you to store and access data on Google’s infrastructure, and one of its key features is the ability to create Cloud Storage Buckets. These buckets are containers for storing your files and can be easily accessed via APIs. In this article, we will cover how to work with Google Cloud Buckets using C# by uploading and consuming files through code examples.
Understanding Google Cloud Storage Buckets
A bucket in Google Cloud Storage is a container for your data. Buckets can hold objects (files) and have globally unique names. Once you have a bucket, you can use Google Cloud’s APIs or SDKs to interact with it.
You can perform various operations on buckets such as:
- Uploading files
- Downloading files
- Listing objects
- Deleting files
Google Cloud SDK provides libraries for different programming languages, including C#, to help interact with Cloud Storage.
Prerequisites
Before we dive into the code, make sure you have the following set up:
- Google Cloud Project: Create a project on Google Cloud Console.
- Google Cloud SDK for .NET: Install the Google Cloud SDK for .NET by using NuGet packages.
- Service Account Key: Generate a service account key and download the JSON file. This will be used for authentication.
- Enable Google Cloud Storage API: Make sure the Google Cloud Storage API is enabled in your Google Cloud project.
Install NuGet Packages
To interact with Google Cloud Storage in C#, you need to install the following NuGet package:
Install-Package Google.Cloud.Storage.V1
This package provides the C# client libraries needed to interact with Google Cloud Storage.
Uploading Files to a Google Cloud Bucket
To upload files to a Cloud Storage bucket, follow the steps below. You will authenticate using a service account key, initialize a storage client, and upload a file to the bucket.
1. Authentication
You need to authenticate your application using a service account. You can set the environment variable GOOGLE_APPLICATION_CREDENTIALS
to the path of your service account JSON key file to authenticate your app.
Example (Windows):
set GOOGLE_APPLICATION_CREDENTIALS=C:\path\to\your-service-account-key.json
2. Uploading a File
Here’s a simple example to upload a file to your Cloud Storage bucket:
using Google.Cloud.Storage.V1; using System; using System.IO; class GoogleCloudStorageExample { public static void UploadFile(string bucketName, string filePath, string objectName) { var storageClient = StorageClient.Create(); using (var fileStream = File.OpenRead(filePath)) { storageClient.UploadObject(bucketName, objectName, null, fileStream); Console.WriteLine($"File {objectName} uploaded to {bucketName}."); } } static void Main(string[] args) { string bucketName = "your-bucket-name"; string filePath = "path-to-your-local-file.txt"; string objectName = "uploaded-file.txt"; UploadFile(bucketName, filePath, objectName); } }
Explanation:
StorageClient.Create()
creates a client to interact with Google Cloud Storage.UploadObject
method uploads the file specified infileStream
to the bucket. TheobjectName
is the name of the file as it will appear in the bucket.
Consuming (Downloading) Files from a Google Cloud Bucket
To download a file from your Cloud Storage bucket, you can use the following approach:
1. Downloading a File
Here’s an example of how to download a file:
using Google.Cloud.Storage.V1; using System; using System.IO; class GoogleCloudStorageExample { public static void DownloadFile(string bucketName, string objectName, string localPath) { var storageClient = StorageClient.Create(); using (var outputFile = File.OpenWrite(localPath)) { storageClient.DownloadObject(bucketName, objectName, outputFile); Console.WriteLine($"File {objectName} downloaded to {localPath}."); } } static void Main(string[] args) { string bucketName = "your-bucket-name"; string objectName = "uploaded-file.txt"; string localPath = "path-to-save-the-file.txt"; DownloadFile(bucketName, objectName, localPath); } }
Explanation:
DownloadObject
method retrieves the object from the specified bucket and saves it to the local file system.- The method requires the
bucketName
(the name of your bucket),objectName
(the name of the file in the bucket), andlocalPath
(where to save the downloaded file).
Listing Files in a Bucket
To list all files (objects) in a bucket, use the following code:
using Google.Cloud.Storage.V1; using System; class GoogleCloudStorageExample { public static void ListFiles(string bucketName) { var storageClient = StorageClient.Create(); var objects = storageClient.ListObjects(bucketName); foreach (var obj in objects) { Console.WriteLine(obj.Name); } } static void Main(string[] args) { string bucketName = "your-bucket-name"; ListFiles(bucketName); } }
Explanation:
- The
ListObjects
method retrieves a list of all objects in the specified bucket. The output is iterated, and the name of each object is printed.
In this article, we covered how to interact with Google Cloud Storage using C# to upload and download files. We also discussed how to list files in a bucket. By using the Google Cloud Storage API for .NET, you can easily integrate Google Cloud Storage into your C# applications and manage your data on the cloud. For further reading, make sure to explore additional Google Cloud Storage features such as object versioning, lifecycle management, and access control.