Skip to content

Instantly share code, notes, and snippets.

@donma
Created February 1, 2019 04:47
Show Gist options
  • Save donma/d90a694628d55c6b5137e849238bc3ea to your computer and use it in GitHub Desktop.
Save donma/d90a694628d55c6b5137e849238bc3ea to your computer and use it in GitHub Desktop.
Azure Blob Storage Recovery from Snapshot
var connsctionString = "your_connection_string";
var cloudStorage = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(connsctionString);
var cloudBlobClient = cloudStorage.CreateCloudBlobClient();
var cloudBlobContainer = cloudBlobClient.GetContainerReference("donmablogsample");
//建立如果不存在的話
var resultCreateContainer = cloudBlobContainer.CreateIfNotExistsAsync().Result;
Console.WriteLine("donmablogsample create already.");
Microsoft.WindowsAzure.Storage.Blob.CloudBlobDirectory cloudBlobDirectory =
cloudBlobContainer.GetDirectoryReference("TEST2/TEST21/");
var bbReference = cloudBlobDirectory.GetBlockBlobReference("sampleText3.json");
var res = new List<CloudBlockBlob>();
Microsoft.WindowsAzure.Storage.Blob.BlobContinuationToken continuationToken = null;
do
{
//只看 TEST2/TEST21/sampleText3.json 的快照
var listingResult = cloudBlobContainer.ListBlobsSegmentedAsync("TEST2/TEST21/sampleText3.json", true, Microsoft.WindowsAzure.Storage.Blob.BlobListingDetails.Snapshots, 100, continuationToken, null, new Microsoft.WindowsAzure.Storage.OperationContext
{
}).Result;
continuationToken = listingResult.ContinuationToken;
res.AddRange(listingResult.Results.Select(x => (x as Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob)).ToList());
}
while (continuationToken != null);
foreach (var r in res)
{
r.FetchAttributesAsync().GetAwaiter().GetResult();
Console.WriteLine("Meta-tag:" + (r.Metadata.ContainsKey("tag") ? (r.Metadata["tag"]) : "") + "," + r.DownloadTextAsync().Result + ";" + " IsSnapShot:" + r.IsSnapshot);
}
//Recovery from tag:9
//還原 tag 9 的快照
var bFileInfo = cloudBlobDirectory.GetBlockBlobReference("sampleText3.json");
foreach (var r in res)
{
if (r.IsSnapshot && r.Metadata.ContainsKey("tag"))
{
if (r.Metadata["tag"] == "9")
{
var copyResult = bFileInfo.StartCopyAsync(r).Result;
Console.WriteLine("Copy Result:" + copyResult);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment