-
-
Save bugre/71560a0c1bf849abe8f79ac761a02dfb 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