コード例 #1
0
ファイル: WorkerSupport.cs プロジェクト: kallex/Caloom
        public static int WebContentSync(string sourceContainerName, string sourcePathRoot, string targetContainerName, string targetPathRoot, PerformCustomOperation customHandler = null)
        {
            //requestOptions.BlobListingDetails = BlobListingDetails.Metadata;
            string sourceSearchRoot = sourceContainerName + "/" + sourcePathRoot;
            string targetSearchRoot = targetContainerName + "/" + targetPathRoot;
            CloudBlobContainer targetContainer = StorageSupport.CurrBlobClient.GetContainerReference(targetContainerName);
            BlobRequestOptions requestOptions = new BlobRequestOptions
                                                    {
                                                        UseFlatBlobListing = true,
                                                        BlobListingDetails = BlobListingDetails.Metadata,
                                                    };
            var sourceBlobList = StorageSupport.CurrBlobClient.ListBlobsWithPrefix(sourceSearchRoot, requestOptions).
                OfType<CloudBlob>().OrderBy(blob => blob.Name).ToArray();
            var targetBlobList = StorageSupport.CurrBlobClient.ListBlobsWithPrefix(targetSearchRoot, requestOptions).
                OfType<CloudBlob>().OrderBy(blob => blob.Name).ToArray();
            List<CloudBlob> targetBlobsToDelete;
            List<BlobCopyItem> blobCopyList;
            int sourcePathLen = sourcePathRoot.Length;
            int targetPathLen = targetPathRoot.Length;
            CompareSourceToTarget(sourceBlobList, targetBlobList, sourcePathLen, targetPathLen,
                out blobCopyList, out targetBlobsToDelete);
            foreach(var blobToDelete in targetBlobsToDelete)
            {
                try
                {
                    bool handled = false;
                    if (customHandler != null)
                    {
                        handled = customHandler(null, blobToDelete, SyncOperationType.Delete);
                    }
                    if (handled == false)
                        blobToDelete.DeleteWithoutFiringSubscriptions();
                }
                catch (WebException wex)
                {
                    throw new InvalidDataException("Error with blob deletion: " + blobToDelete.Name, wex);
                }
            }
            foreach(var blobCopyItem in blobCopyList)
            {
                try
                {
                    CloudBlob targetBlob;
                    if (blobCopyItem.TargetBlob == null)
                    {
                        string sourceBlobNameWithoutSourcePrefix = blobCopyItem.SourceBlob.Name.Substring(sourcePathRoot.Length);
                        string targetBlobName;
                        if (sourceBlobNameWithoutSourcePrefix.StartsWith("/") && String.IsNullOrEmpty(targetPathRoot))
                            targetBlobName = sourceBlobNameWithoutSourcePrefix.Substring(1);
                        else
                            targetBlobName = targetPathRoot + sourceBlobNameWithoutSourcePrefix;
                        //string targetBlobName = String.IsNullOrEmpty(targetPathRoot) ? sourceBlobName.
                        //string targetBlobName =
                        //    blobCopyItem.SourceBlob.Name.Replace(sourcePathRoot, targetPathRoot);
                        targetBlob = targetContainer.GetBlobReference(targetBlobName);
                    }
                    else
                        targetBlob = blobCopyItem.TargetBlob;
                    bool handled = false;
                    Console.WriteLine("Processing sync: " + blobCopyItem.SourceBlob.Name + " => " + targetBlob.Name);
                    if (customHandler != null)
                    {
                        handled = customHandler(blobCopyItem.SourceBlob, targetBlob, SyncOperationType.Copy);
                    }
                    if (handled == false)
                        targetBlob.CopyFromBlob(blobCopyItem.SourceBlob);
                }
                catch (WebException wex)
                {
                    throw new InvalidDataException("Error with blob copy: " + blobCopyItem.SourceBlob.Name, wex);
                }

            }
            return targetBlobsToDelete.Count + blobCopyList.Count;
        }
コード例 #2
0
        public static int WebContentSync(string sourceContainerName, string sourcePathRoot, string targetContainerName, string targetPathRoot, PerformCustomOperation customHandler = null)
        {
            //requestOptions.BlobListingDetails = BlobListingDetails.Metadata;
            string             sourceSearchRoot = sourceContainerName + "/" + sourcePathRoot;
            string             targetSearchRoot = targetContainerName + "/" + targetPathRoot;
            CloudBlobContainer targetContainer  = StorageSupport.CurrBlobClient.GetContainerReference(targetContainerName);
            BlobRequestOptions requestOptions   = new BlobRequestOptions
            {
                UseFlatBlobListing = true,
                BlobListingDetails = BlobListingDetails.Metadata,
            };
            var sourceBlobList = StorageSupport.CurrBlobClient.ListBlobsWithPrefix(sourceSearchRoot, requestOptions).
                                 OfType <CloudBlob>().OrderBy(blob => blob.Name).ToArray();
            var targetBlobList = StorageSupport.CurrBlobClient.ListBlobsWithPrefix(targetSearchRoot, requestOptions).
                                 OfType <CloudBlob>().OrderBy(blob => blob.Name).ToArray();
            List <CloudBlob>    targetBlobsToDelete;
            List <BlobCopyItem> blobCopyList;
            int sourcePathLen = sourcePathRoot.Length;
            int targetPathLen = targetPathRoot.Length;

            CompareSourceToTarget(sourceBlobList, targetBlobList, sourcePathLen, targetPathLen,
                                  out blobCopyList, out targetBlobsToDelete);
            foreach (var blobToDelete in targetBlobsToDelete)
            {
                bool handled = false;
                if (customHandler != null)
                {
                    handled = customHandler(null, blobToDelete, SyncOperationType.Delete);
                }
                if (handled == false)
                {
                    blobToDelete.DeleteWithoutFiringSubscriptions();
                }
            }
            foreach (var blobCopyItem in blobCopyList)
            {
                CloudBlob targetBlob;
                if (blobCopyItem.TargetBlob == null)
                {
                    string sourceBlobNameWithoutSourcePrefix = blobCopyItem.SourceBlob.Name.Substring(sourcePathRoot.Length);
                    string targetBlobName;
                    if (sourceBlobNameWithoutSourcePrefix.StartsWith("/") && String.IsNullOrEmpty(targetPathRoot))
                    {
                        targetBlobName = sourceBlobNameWithoutSourcePrefix.Substring(1);
                    }
                    else
                    {
                        targetBlobName = targetPathRoot + sourceBlobNameWithoutSourcePrefix;
                    }
                    //string targetBlobName = String.IsNullOrEmpty(targetPathRoot) ? sourceBlobName.
                    //string targetBlobName =
                    //    blobCopyItem.SourceBlob.Name.Replace(sourcePathRoot, targetPathRoot);
                    targetBlob = targetContainer.GetBlobReference(targetBlobName);
                }
                else
                {
                    targetBlob = blobCopyItem.TargetBlob;
                }
                bool handled = false;
                Console.WriteLine("Processing sync: " + blobCopyItem.SourceBlob.Name + " => " + targetBlob.Name);
                if (customHandler != null)
                {
                    handled = customHandler(blobCopyItem.SourceBlob, targetBlob, SyncOperationType.Copy);
                }
                if (handled == false)
                {
                    targetBlob.CopyFromBlob(blobCopyItem.SourceBlob);
                }
            }
            return(targetBlobsToDelete.Count + blobCopyList.Count);
        }