You can upload large files directly to the Azure Blob repository directly using the HTTP PUT verb, the largest file I tried with the code below is 4.6 GB. You can do it in C # as follows:
// write up to ChunkSize of data to the web request void WriteToStreamCallback(IAsyncResult asynchronousResult) { var webRequest = (HttpWebRequest)asynchronousResult.AsyncState; var requestStream = webRequest.EndGetRequestStream(asynchronousResult); var buffer = new Byte[4096]; int bytesRead; var tempTotal = 0; File.FileStream.Position = DataSent; while ((bytesRead = File.FileStream.Read(buffer, 0, buffer.Length)) != 0 && tempTotal + bytesRead < CHUNK_SIZE && !File.IsDeleted && File.State != Constants.FileStates.Error) { requestStream.Write(buffer, 0, bytesRead); requestStream.Flush(); DataSent += bytesRead; tempTotal += bytesRead; File.UiDispatcher.BeginInvoke(OnProgressChanged); } requestStream.Close(); if (!AbortRequested) webRequest.BeginGetResponse(ReadHttpResponseCallback, webRequest); } void StartUpload() { var uriBuilder = new UriBuilder(UploadUrl); if (UseBlocks) { // encode the block name and add it to the query string CurrentBlockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString())); uriBuilder.Query = uriBuilder.Query.TrimStart('?') + string.Format("&comp=block&blockid={0}", CurrentBlockId); } // with or without using blocks, we'll make a PUT request with the data var webRequest = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(uriBuilder.Uri); webRequest.Method = "PUT"; webRequest.BeginGetRequestStream(WriteToStreamCallback, webRequest); }
UploadUrl is generated by Azure itself and contains a shared signature, this SAS URL indicates where blob will be downloaded, and how long access to security is provided (write access in your case). You can create a SAS URL as follows:
readonly CloudBlobClient BlobClient; readonly CloudBlobContainer BlobContainer; public UploadService() { // Setup the connection to Windows Azure Storage var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); BlobClient = storageAccount.CreateCloudBlobClient(); // Get and create the container BlobContainer = BlobClient.GetContainerReference("publicfiles"); } string JsonSerializeData(string url) { var serializer = new DataContractJsonSerializer(url.GetType()); var memoryStream = new MemoryStream(); serializer.WriteObject(memoryStream, url); return Encoding.Default.GetString(memoryStream.ToArray()); } public string GetUploadUrl() { var sasWithIdentifier = BlobContainer.GetSharedAccessSignature(new SharedAccessPolicy { Permissions = SharedAccessPermissions.Write, SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(60) }); return JsonSerializeData(BlobContainer.Uri.AbsoluteUri + "/" + Guid.NewGuid() + sasWithIdentifier); }
I also have a topic on this subject, where you can find more information here. How to upload huge files to Azure blob from a web page
user152949 Jul 08 2018-11-11T00: 00Z
source share