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

I mostly work with C#, ASP.NET, MVC, WCF, Web API, Entity FrameWork, MS Sql.
More under this category...
Published by Mritunjay Kumar
, Mindfire Solutions,
about 5 years ago
1784 Views
Published by Mritunjay Kumar
, Mindfire Solutions,
about 5 years ago
1596 Views
Published by Mritunjay Kumar
, Mindfire Solutions,
about 5 years ago
2241 Views
Published by Mritunjay Kumar
, Mindfire Solutions,
about 5 years ago
1932 Views
Published by Mritunjay Kumar
, Mindfire Solutions,
about 6 years ago
2097 Views
Published by Mritunjay Kumar
, Mindfire Solutions,
about 6 years ago
6
if (6 > 1) {
Comments
} else {
Comment
}
6941 Views
Published by Mritunjay Kumar
, Mindfire Solutions,
about 6 years ago
4
if (4 > 1) {
Comments
} else {
Comment
}
6480 Views
All under this category...