// have destination location. // have original blob name and prefix // new name should be destination name + (blob.name - blob.prefix) public static BlobCopyData StartCopy(BasicBlobContainer origBlob, string DestinationUrl, DestinationBlobType destBlobType, bool skipIfExists) { var client = AzureHelper.GetTargetCloudBlobClient(DestinationUrl); var opt = client.GetServiceProperties(); var containerName = AzureHelper.GetContainerFromUrl(DestinationUrl); var destBlobPrefix = AzureHelper.GetBlobFromUrl(DestinationUrl); var container = client.GetContainerReference(containerName); container.CreateIfNotExists(); ICloudBlob blob = null; var url = GeneratedAccessibleUrl(origBlob); string blobName; string blobWithoutPrefix = string.Empty; if (!string.IsNullOrWhiteSpace(origBlob.BlobPrefix) && origBlob.Name.Length > origBlob.BlobPrefix.Length) { blobWithoutPrefix = origBlob.Name.Substring(origBlob.BlobPrefix.Length); } if (!string.IsNullOrWhiteSpace(blobWithoutPrefix)) { blobName = string.Format("{0}/{1}", destBlobPrefix, blobWithoutPrefix); } else { // need to get just filename. ie last element of / var actualBlobName = origBlob.Name.Split('/').Last(); if (string.IsNullOrWhiteSpace(destBlobPrefix)) { blobName = actualBlobName; } else { blobName = string.Format("{0}/{1}", destBlobPrefix, actualBlobName); } } // include unknown for now. Unsure. if (destBlobType == DestinationBlobType.Block || destBlobType == DestinationBlobType.Unknown) { blob = container.GetBlockBlobReference(blobName); } else if (destBlobType == DestinationBlobType.Page) { blob = container.GetPageBlobReference(blobName); } if (skipIfExists) { try { blob.Exists(); } catch (Exception) { Console.WriteLine("Skipping {0}", url); return(null); } } if (blob != null) { try { // crazy large values, want to try and debug an issue. var brOptions = new BlobRequestOptions(); brOptions.MaximumExecutionTime = new TimeSpan(0, maxExecutionTimeInMins, 0); brOptions.ServerTimeout = new TimeSpan(0, maxServerTimeoutInMins, 0); // return copyID incase user wants to kill the process later. var copyID = blob.StartCopyFromBlob(new Uri(url), options: brOptions); var bcd = new BlobCopyData { CopyID = copyID, Blob = blob }; return(bcd); } catch (Exception ex) { Console.WriteLine("StartCopyFromBlob error msg " + ex.Message); Console.WriteLine("StartCopyFromBlob error stack " + ex.StackTrace); throw; } } else { throw new NotImplementedException("Cannot copy blobs that are not block or page"); } }
// have destination location. // have original blob name and prefix // new name should be destination name + (blob.name - blob.prefix) public static BlobCopyData StartCopy(BasicBlobContainer origBlob, string DestinationUrl, DestinationBlobType destBlobType) { var client = AzureHelper.GetTargetCloudBlobClient(DestinationUrl); var opt = client.GetServiceProperties(); var containerName = AzureHelper.GetContainerFromUrl( DestinationUrl); var destBlobPrefix = AzureHelper.GetBlobFromUrl( DestinationUrl ); var container = client.GetContainerReference( containerName ); container.CreateIfNotExists(); ICloudBlob blob = null; var url = GeneratedAccessibleUrl(origBlob); string blobName; string blobWithoutPrefix = string.Empty; if (!string.IsNullOrWhiteSpace(origBlob.BlobPrefix) && origBlob.Name.Length > origBlob.BlobPrefix.Length) { blobWithoutPrefix = origBlob.Name.Substring(origBlob.BlobPrefix.Length); } if (!string.IsNullOrWhiteSpace(blobWithoutPrefix)) { blobName = string.Format("{0}/{1}", destBlobPrefix, blobWithoutPrefix); } else { // need to get just filename. ie last element of / var actualBlobName = origBlob.Name.Split('/').Last(); if (string.IsNullOrWhiteSpace(destBlobPrefix)) { blobName = actualBlobName; } else { blobName = string.Format("{0}/{1}", destBlobPrefix, actualBlobName); } } // include unknown for now. Unsure. if (destBlobType == DestinationBlobType.Block || destBlobType == DestinationBlobType.Unknown) { blob = container.GetBlockBlobReference(blobName); } else if (destBlobType == DestinationBlobType.Page) { blob = container.GetPageBlobReference(blobName); } if (blob != null) { try { // crazy large values, want to try and debug an issue. var brOptions = new BlobRequestOptions(); brOptions.MaximumExecutionTime = new TimeSpan(0, maxExecutionTimeInMins, 0); brOptions.ServerTimeout = new TimeSpan(0, maxServerTimeoutInMins, 0); // return copyID incase user wants to kill the process later. var copyID = blob.StartCopyFromBlob(new Uri(url), options: brOptions); var bcd = new BlobCopyData { CopyID = copyID, Blob = blob }; return bcd; } catch(Exception ex) { Console.WriteLine("StartCopyFromBlob error msg " + ex.Message); Console.WriteLine("StartCopyFromBlob error stack " + ex.StackTrace); throw; } } else { throw new NotImplementedException("Cannot copy blobs that are not block or page"); } }