Let's implement a console application for renaming a file in Google cloud storage with help of Google.Apis.Storage.v1.

We will implement this functionality in two steps.
#1. Copy the file with changed name.
#2. Delete the original file.
My dev machine: .Net 4.5, Visual Studio 2012

Prerequisites: Install Google.Apis.Storage.v1 from NuGet packages.

File details:
Program.cs: Entry point of the application, contains Main function. This file will also contain different other functions required for renaming a file in Google cloud storage.

Enough, let's see the code now!

Code under Program.cs

using System;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Storage.v1;

internal class Program
{
    // Below details can be retrieved from Google console site.
    const string GCSBucketName = "MySampleBucket";
    const string P12KeyFullPath = @"C:\Temp\key.p12";
    const string P12KeySecret = "gcssecret";
    const string GCSServiceAccountEmail
                  = "GetItFromGoogleCloudStorageConsole@developer.gserviceaccount.com";
    const string GCSApplicationName = "MySampleProject";

    private static void Main(string[] args)
    {
        try
        {
            RenameFile("Test.pdf", "Changed-Test.pdf");
        }
        catch (AggregateException ex)
        {
            foreach (var e in ex.InnerExceptions)
            {
                Console.WriteLine("ERROR: " + e.Message);
            }
        }
        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }

    private static void RenameFile(string originalFileName, string changedFileNameAfterRename)
    {
        // Loading the Key file
        var certificate
            = new X509Certificate2(P12KeyFullPath, P12KeySecret,
                                   X509KeyStorageFlags.MachineKeySet |
                                   X509KeyStorageFlags.PersistKeySet |
                                   X509KeyStorageFlags.Exportable);

        // Authentication first!
        var credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(GCSServiceAccountEmail)
                {
                    Scopes = new[] { StorageService.Scope.DevstorageReadWrite }
                }.FromCertificate(certificate));

        // Well, all setup now!
        // Let's move to initializing service for Google API!
        var service = new StorageService(
            new BaseClientService.Initializer
                {
                    ApplicationName = GCSApplicationName,
                    HttpClientInitializer = credential
                }
            );

        // Step#1. Copy the file with changed/ requested file name
        var copyObject = new ObjectsResource(service);
        var copyRequest = copyObject.Copy(null, GCSBucketName,
                                                originalFileName,
                                                GCSBucketName,
                                                changedFileNameAfterRename);
        copyRequest.Execute();

        // Step#2. Delete the original file
        var deleteOriginalFileObject = new ObjectsResource(service);
        var deleteOriginalFileRequest =
            deleteOriginalFileObject.Delete(GCSBucketName,
                                            originalFileName);
        deleteOriginalFileRequest.Execute();

        service.Dispose();
    }
}
Discussion
4 X 8 =
** To prevent abusing comments from publishing, posted comments will be reviewed and then published!
 Mritunjay Kumar
Works at Mindfire Solutions

I mostly work with C#, ASP.NET, MVC, WCF, Web API, Entity FrameWork, MS Sql.

More under this category...
Convert data table to generic list
Extension method to convert a delimited string to list of generic primitive type
Convert generic list to data table
Generate random string from set of fixed chars
Initializing X509Certificate2, Windows 2012 + IIS8 + .Net 4.5
Google cloud storage API - upload file with progress bar
Google cloud storage API - download file with progress bar
All under this category...