BlobSync.AzureOps.UploadFile C# (CSharp) Method

UploadFile() public method

public UploadFile ( string containerName, string blobName, string localFilePath, int parallelFactor = 2 ) : long
containerName string
blobName string
localFilePath string
parallelFactor int
return long
        public long UploadFile(string containerName, string blobName, string localFilePath, int parallelFactor=2)
        {
            var fileLength = CommonOps.GetFileSize(localFilePath);
            var sw = new Stopwatch();
            sw.Start();
            var md5ForFile = GetFileMD5(localFilePath);

            // 1) Does remote blob exist?
            // 2) if so, download existing signature for blob.
            if (AzureHelper.DoesBlobExist(containerName, blobName) && AzureHelper.DoesBlobSignatureExist(containerName, blobName))
            {
                var md5ForBlob = GetBlobMD5(containerName, blobName);

                // only continue if files are actually different.
                if (md5ForBlob != md5ForFile)
                {
                    // 3) If blob exists and have signature, then let the magic begin.
                    // 3.1) Download existing blob signature from Azure.
                    // 3.2) Search through local file for matches in existing blob signature.
                    // 3.3) Upload differences to Azure
                    // 3.4) Upload new signature.s

                    var blobSig = DownloadSignatureForBlob(containerName, blobName);
                    Console.WriteLine(string.Format("Dowloaded sig {0}ms", sw.ElapsedMilliseconds));

                    var searchResults = CommonOps.SearchLocalFileForSignatures(localFilePath, blobSig);

                    Console.WriteLine(string.Format("Searched for common {0}ms", sw.ElapsedMilliseconds));

                    var allBlocks = UploadDelta(localFilePath, searchResults, containerName, blobName, parallelFactor: parallelFactor);
                    var sig = CommonOps.CreateSignatureFromNewAndReusedBlocks(allBlocks);

                    UploadSignatureForBlob(blobName, containerName, sig);

                    // set md5 for entire blob
                    AzureHelper.SetBlobMD5(containerName, blobName, md5ForFile);

                    long bytesUploaded = allBlocks.Where(b => b.IsNew).Select(b => b.Size).Sum();

                    return bytesUploaded;
                }

                return 0;   // no bytes changed, no bytes uploaded
            }
            else
            {
                // 4) If blob or signature does NOT exist, just upload as normal. No tricky stuff to do here.
                // 4.1) Generate signature and upload it.

                var remainingBytes = new RemainingBytes()
                {
                    BeginOffset = 0,
                    EndOffset = fileLength - 1
                };

                var allUploadedBlocks = UploadBytesParallel(remainingBytes, localFilePath, containerName, blobName, parallelFactor: parallelFactor);
                var res = (from b in allUploadedBlocks orderby b.Offset ascending select b.BlockId);
                PutBlockList(res.ToArray(), containerName, blobName);

                var sig = CommonOps.CreateSignatureForLocalFile(localFilePath);
                UploadSignatureForBlob(blobName, containerName, sig);

                // set md5 for entire blob
                AzureHelper.SetBlobMD5(containerName, blobName, md5ForFile);

                return fileLength;
            }
        }

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            string command;
            string fileName;

            int parallelFactor = ConfigHelper.ParallelFactor;

            var sw = new Stopwatch();
            sw.Start();

            if (args.Length == 4)
            {
                command = args[0];
                fileName = args[1];
                var containerName = args[2];
                var blobName = args[3];
                var azureOps = new AzureOps();

                switch (command)
                {
                    case "upload":
                        var bytesUploaded = azureOps.UploadFile(containerName, blobName, fileName, parallelFactor);
                        Console.WriteLine("Uploaded {0} bytes", bytesUploaded);
                        break;
                    case "download":
                        var bytesDownloaded = azureOps.DownloadBlob(containerName, blobName, fileName, parallelFactor);
                        Console.WriteLine("Downloaded {0} bytes", bytesDownloaded);
                        break;
                    case "estimate":
                        var estimate = azureOps.CalculateDeltaSize(containerName, blobName, fileName);
                        Console.WriteLine( string.Format("Estimate to upload {0} bytes", estimate));
                        break;
                    default:
                        ShowExamples();
                        break;
                }
            }
            else if (args.Length == 3)
            {
                command = args[0];
                fileName = args[1];
                var localSigPath = args[2];
                var azureOps = new AzureOps();

                switch (command)
                {
                    case "estimatelocal":
                        var estimatelocal = azureOps.CalculateDeltaSizeFromLocalSig(localSigPath, fileName);
                        Console.WriteLine(string.Format("Estimate to upload {0} bytes", estimatelocal));
                        break;
                    case "createdeltasig":
                        var sig = azureOps.GenerateDeltaSigFromLocalResources(localSigPath, fileName);

                        var sigFileName = fileName + ".sig";
                        using (Stream s = new FileStream(sigFileName, FileMode.Create))
                        {
                            SerializationHelper.WriteBinarySizedBasedSignature(sig, s);
                        }
                        break;
                    case "showblocklist":

                        azureOps.GetBlockListInfo(args[1], args[2]);
                        break;
                    case "defrag":
                        azureOps.DefragBlob(args[1], args[2]);
                        break;

                    default:
                        ShowExamples();
                        break;
                }
            }
            else
            if (args.Length == 2)
            {
                command = args[0];
                fileName = args[1];
                var azureOps = new AzureOps();
                switch (command)
                {
                    case "createsig":
                        var sig = CommonOps.CreateSignatureForLocalFile(fileName);

                        var sigFileName = fileName + ".sig";
                        using (Stream s = new FileStream(sigFileName, FileMode.Create))
                        {
                            SerializationHelper.WriteBinarySizedBasedSignature(sig, s);
                        }

                        break;
                    case "showsig":
                        using (var fs = new FileStream(fileName, FileMode.Open))
                        {
                            var loadedSig = SerializationHelper.ReadSizeBasedBinarySignature(fs);

                            foreach( var sigSize in loadedSig.Signatures)
                            {
                                foreach( var s in sigSize.Value.SignatureList.OrderBy( s => s.Offset))
                                {
                                    Console.WriteLine(string.Format("{0}:{1}", s.Offset, s.Size));

                                }
                            }
                        }
                        break;
                    case "defraglocal":
                        azureOps.DefragBlob(args[1]);
                        break;
                    default:
                        ShowExamples();
                          break;
                }

            }
            else
            {
                ShowExamples();

            }

            sw.Stop();
            Console.WriteLine("Took {0}s", (double)sw.ElapsedMilliseconds / 1000.0);
        }