Last active
February 10, 2021 07:56
-
-
Save TsuyoshiUshio/b258e20b5a4c21d24200cec222757511 to your computer and use it in GitHub Desktop.
Storage migration from Google Cloud Storage to Azure with Async/Await
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"frameworks": { | |
"net46":{ | |
"dependencies": { | |
"Google.Cloud.Storage.V1": "1.0.0-beta05" | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "Microsoft.WindowsAzure.Storage" | |
using System; | |
using System.IO; | |
using Google.Cloud.Storage.V1; | |
using Microsoft.WindowsAzure.Storage; | |
using Google.Apis.Auth.OAuth2; | |
public async static Task Run(string message, TraceWriter log) | |
{ | |
log.Info($"C# Queue trigger function processed: {message}"); | |
// Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "D:\\home\\site\\wwwroot\\{YOUR FUNCTION NAME}\\servicecert.json"); // this function is from 1.0.0-beta06 | |
// Google Storage | |
var credential = GoogleCredential.FromStream(System.IO.File.OpenRead("D:\\home\\site\\wwwroot\\{YOUR FUNCTION NAME}\\servicecert.json")); | |
var client = StorageClient.Create(credential); | |
var bucketName = "simpleatest"; | |
// Azure Storage Account | |
var storageAccount = CloudStorageAccount.Parse("BlobEndpoint={YOUR BLOB SERVICE SAS TOKEN is here}"); | |
var blobClient = storageAccount.CreateCloudBlobClient(); | |
var container = blobClient.GetContainerReference(bucketName); | |
await container.CreateIfNotExistsAsync(); | |
foreach(var obj in client.ListObjects(bucketName, message)) | |
{ | |
if (IsDirectory(obj.Name)) | |
{ | |
container.GetDirectoryReference(obj.Name); | |
} else | |
{ | |
var blockBlob = container.GetBlockBlobReference(obj.Name); | |
using (var stream = await blockBlob.OpenWriteAsync()) | |
{ | |
await client.DownloadObjectAsync(bucketName, obj.Name, stream); | |
} | |
} | |
log.Info($"{obj.Name}:{obj.ContentType}"); | |
} | |
} | |
private static bool IsDirectory(string backetPath) | |
{ | |
return backetPath.EndsWith("/"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Edit {YOUR FUNCTION NAME} and {YOUR BLOB SERVICE SAS TOKEN is here}
Example
{YOUR FUNCTION NAME} : GCSMigration
{YOUR BLOB SERVICE SAS TOKEN is here}: https://xxxxx.blob.core.windows.net/?sv=2015-12-11&ss=bfqt&srt=sco&sp=rwdlacup&se=2017-01-03T02:47:40Z&st=2016-12-31T18:47:40Z&spr=https&sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
You can refer this blog post
https://blogs.technet.microsoft.com/livedevopsinjapan/2017/01/02/storage-migration-from-google-cloud-to-azure-using-azure-functions/