public static void SaveFileToBlobStorage( BlobStorageRequest request) { new BlobFuService(request.ConnectionStringName) .SaveToBlobStorage(request); }
public BlobFuService SaveToBlobStorage(BlobStorageRequest request) { VerifyContainer(request.Container); if ((request.StreamOfDataToStore != null && request.StreamOfDataToStore.Length > 0) && (request.DataToStore != null && request.DataToStore.Length > 0)) { throw new ApplicationException("Don't pass in a stream AND a byte array, I don't know which one to save"); } if ((request.DataToStore != null && request.DataToStore.Length > 0) && (request.StreamOfDataToStore == null || request.StreamOfDataToStore.Length == 0)) { request.StreamOfDataToStore = new MemoryStream(request.DataToStore); } // http://wely-lau.net/2012/02/26/uploading-big-files-in-windows-azure-blob-storage-with-putlistblock/ CloudBlockBlob blob = this._container.GetBlockBlobReference(request.Filename.ToLower()); //blob.Properties.CacheControl = ""; //blob.SetProperties(); int maxSize = 32 * 1024 * 1024; // 32 MB //int maxSize = 1 * 1024 * 1024; // 1 MB if (request.StreamOfDataToStore.Length > maxSize) { int id = 0; long byteslength = request.StreamOfDataToStore.Length; int bytesread = 0; int index = 0; List <string> blocklist = new List <string>(); int numBytesPerChunk = 1 * 1024 * 1024; //1MB per block //int numBytesPerChunk = 50 * 1024; //50KB per block byte[] buffer = new byte[numBytesPerChunk]; do { bytesread += request.StreamOfDataToStore.Read(buffer, 0, buffer.Length); string blockIdBase64 = Convert.ToBase64String(System.BitConverter.GetBytes(id)); blob.PutBlock(blockIdBase64, new MemoryStream(buffer, true), null); blocklist.Add(blockIdBase64); id++; } while (byteslength - bytesread > numBytesPerChunk); long final = byteslength - bytesread; byte[] finalbuffer = new byte[final]; bytesread += request.StreamOfDataToStore.Read(finalbuffer, 0, finalbuffer.Length); string blockId = Convert.ToBase64String(System.BitConverter.GetBytes(id)); blob.PutBlock(blockId, new MemoryStream(finalbuffer, true), null); blocklist.Add(blockId); blob.PutBlockList(blocklist); } else { blob.UploadFromStream(request.StreamOfDataToStore); } if (request.BlobSavedCallback != null) { request.BlobSavedCallback(blob.Uri); } return(this); }