I wrote a simple console application demonstrating this functionality. Please note that it uses Storage Client Library 2.0.3:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string blobContainerName = "images";
string sourceFolderName = "TestFolder/";
string newFolderName = "DevFolder/";
CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
CloudBlobContainer blobContainer =
storageAccount.CreateCloudBlobClient().GetContainerReference(blobContainerName);
var blobContainerUri = blobContainer.Uri.AbsoluteUri;
//Now let's fetch the blobs from "source folder"
var blobs = blobContainer.ListBlobs(sourceFolderName, true);
//Now we'll enumerate through blobs
foreach (var blob in blobs)
{
var sourceBlockBlob = blob as CloudBlockBlob;
string newBlobName = newFolderName + sourceBlockBlob.Name.Substring(sourceFolderName.Length);
var newBlob = blobContainer.GetBlockBlobReference(newBlobName);
newBlob.StartCopyFromBlob(sourceBlockBlob);
while (true)
{
//Since copy blob operation is an async operation, we must wait for the copy operation to finish.
//To do so, we'll check if the copy operation is completed or not by fetching properties of the new blob.
newBlob.FetchAttributes();
if (newBlob.CopyState.Status != CopyStatus.Pending)
{
break;
}
//It's still not completed. So wait for some time.
System.Threading.Thread.Sleep(1000);
}
//Get the properties one more time
newBlob.FetchAttributes();
if (newBlob.CopyState.Status == CopyStatus.Success)
{
Console.WriteLine(string.Format("Successfully copied {0} to {1}", sourceBlockBlob.Name, newBlobName));
//Delete the source blob only if the copy is successful.
sourceBlockBlob.DeleteIfExists();
Console.WriteLine(string.Format("Successfully deleted {0}", sourceBlockBlob.Name));
}
}
Console.WriteLine("Press any key to terminate the application.");
Console.ReadLine();
}
}
}
484



被折叠的 条评论
为什么被折叠?



