Last active
August 29, 2015 14:18
-
-
Save kiyoaki/fbba7721e3e06bbfb0fb to your computer and use it in GitHub Desktop.
Azure blob storage coping from another storage account blob
This file contains hidden or 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
using System; | |
using System.IO; | |
using Microsoft.WindowsAzure; | |
using Microsoft.WindowsAzure.StorageClient; | |
namespace AzureBlobBatch | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
if (args == null || args.Length < 4) | |
{ | |
Console.WriteLine("Required arguments is [source storage account name] [source storage key] " + | |
"[destination storage account name] [destination storage key] [container name (optional)]"); | |
Console.ReadKey(); | |
return; | |
} | |
string containerName = null; | |
if (args.Length == 5) | |
{ | |
containerName = args[4]; | |
} | |
var sourceCredentials = new StorageCredentialsAccountAndKey(args[0], args[1]); | |
var sourceAccount = new CloudStorageAccount(sourceCredentials, true); | |
var sourceClient = sourceAccount.CreateCloudBlobClient(); | |
var destinationCredentials = new StorageCredentialsAccountAndKey(args[2], args[3]); | |
var destinationAccount = new CloudStorageAccount(destinationCredentials, true); | |
var destinationClient = destinationAccount.CreateCloudBlobClient(); | |
if (containerName != null) | |
{ | |
var container = sourceClient.GetContainerReference(containerName); | |
Copy(container, destinationClient, container.Name); | |
} | |
else | |
{ | |
foreach (var container in sourceClient.ListContainers()) | |
{ | |
Copy(container, destinationClient, container.Name); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
Console.ReadKey(); | |
} | |
} | |
private static void Copy(CloudBlobContainer sourceContainer, CloudBlobClient destinationClient, string containerName) | |
{ | |
var destinationContainer = destinationClient.GetContainerReference(containerName); | |
destinationContainer.CreateIfNotExist(); | |
foreach (var blob in sourceContainer.ListBlobs(new BlobRequestOptions | |
{ | |
AccessCondition = AccessCondition.None, | |
BlobListingDetails = BlobListingDetails.All, | |
UseFlatBlobListing = true | |
})) | |
{ | |
var relativeUri = sourceContainer.Uri.MakeRelativeUri(blob.Uri); | |
var uri = new Uri(destinationContainer.Uri, relativeUri).ToString(); | |
var destinationBlob = destinationContainer.GetBlockBlobReference(uri); | |
if (destinationBlob.Exists()) | |
{ | |
continue; | |
} | |
var sourceBlob = sourceContainer.GetBlockBlobReference(blob.Uri.ToString()); | |
using (var stream = new MemoryStream()) | |
{ | |
sourceBlob.DownloadToStream(stream); | |
stream.Seek(0, SeekOrigin.Begin); | |
destinationBlob.UploadFromStream(stream); | |
} | |
Console.WriteLine("copied " + relativeUri); | |
} | |
} | |
} | |
public static class BlobExtensions | |
{ | |
public static bool Exists(this CloudBlob blob) | |
{ | |
try | |
{ | |
blob.FetchAttributes(); | |
return true; | |
} | |
catch (StorageClientException e) | |
{ | |
if (e.ErrorCode == StorageErrorCode.ResourceNotFound) | |
{ | |
return false; | |
} | |
throw; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment