public void SaveThumbnail(int id, string fileName, System.IO.Stream strm) { var product = this.DB.Products.Where(a => a.ProductID == id).FirstOrDefault(); if (product == null) { throw new Exception("Product is not found"); } TransactionOptions topts = new System.Transactions.TransactionOptions(); topts.Timeout = TimeSpan.FromSeconds(60); topts.IsolationLevel = System.Transactions.IsolationLevel.Serializable; using (TransactionScope trxScope = new TransactionScope(TransactionScopeOption.Required, topts)) using (DbConnection conn = DBConnectionFactory.GetRIAppDemoConnection()) { System.IO.BinaryReader br = new System.IO.BinaryReader(strm); byte[] bytes = br.ReadBytes(64 * 1024); string fldname = "ThumbNailPhoto"; BlobStream bstrm = new BlobStream(conn as SqlConnection, "[SalesLT].[Product]", fldname, string.Format("WHERE [ProductID]={0}", id)); bstrm.InitColumn(); bstrm.Open(); while (bytes != null && bytes.Length > 0) { bstrm.Write(bytes, 0, bytes.Length); bytes = br.ReadBytes(64 * 1024);; } bstrm.Close(); br.Close(); trxScope.Complete(); } product.ThumbnailPhotoFileName = fileName; this.DB.SubmitChanges(); }
public static void UploadBlobFile(byte[] fileBytes, string fileName) { try { string storageAccountConnection = string.Empty; storageAccountConnection = ConfigurationManager.AppSettings["StorageAccount.ConnectionString"].ToString(); // If you want to use Windows Azure cloud storage account, use the following // code (after uncommenting) instead of the code above. cloudStorageAccount = CloudStorageAccount.Parse(storageAccountConnection); // Create the blob client, which provides // authenticated access to the Blob service. blobClient = cloudStorageAccount.CreateCloudBlobClient(); string deploymentPackageFolderString = string.Empty; deploymentPackageFolderString = ConfigurationManager.AppSettings["DeploymentPackageFolder"].ToString(); // Get the container reference. blobContainer = blobClient.GetContainerReference(deploymentPackageFolderString); // Create the container if it does not exist. blobContainer.CreateIfNotExist(); // Set permissions on the container. containerPermissions = new BlobContainerPermissions(); // This sample sets the container to have public blobs. Your application // needs may be different. See the documentation for BlobContainerPermissions // for more information about blob container permissions. containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; blobContainer.SetPermissions(containerPermissions); blob = blobContainer.GetBlobReference(fileName); // Open a stream using the cloud object using (BlobStream blobStream = blob.OpenWrite()) { blobStream.Write(fileBytes, 0, fileBytes.Count()); blobStream.Flush(); blobStream.Close(); } } catch (System.Exception ex) { Logger.Write(string.Format("Error in UploadBlobFile() Error: {0}", ex.Message)); } }
public int WriteFile( string filename, byte[] buffer, ref uint writtenBytes, long offset, DokanFileInfo info) { Trace.WriteLine(string.Format("WriteFile {0}", filename)); BlobStream stream = blobsWriting.GetOrAdd(filename, str => this.GetBlob(filename, false).OpenWrite()); stream.Write(buffer, (int)0, buffer.Length); writtenBytes = (uint)buffer.Length; info.IsDirectory = false; return(0); }
private void UploadBlob(string connectionString, string containerName, string blobFilename, string filename, bool deleteAfterUpload, bool updateProgress) { if (!this.cancelled) { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(containerName.ToLower()); int progressPercentage; // Create the container if it doesn't exist container.CreateIfNotExist(); CloudBlockBlob blob = container.GetBlockBlobReference(blobFilename); // Open a str3am for writing using (FileStream inStream = new FileStream(filename, FileMode.Open)) using (BlobStream outStream = blob.OpenWrite()) { long totalSize = inStream.Length; long totalRead = 0; int bufferSize = 4096; byte[] buffer = new byte[bufferSize]; while (true) { int read = inStream.Read(buffer, 0, buffer.Length); if (read <= 0) { break; } outStream.Write(buffer, 0, read); totalRead += read; if (updateProgress) { progressPercentage = (int)(((double)totalRead / (double)totalSize) * 100); OnProgress(progressPercentage); } } }; if (deleteAfterUpload) { File.Delete(filename); } } }
public void GetBlobTest() { string containerName = "foo"; string blobName = "bar"; CloudBlobContainer container = AzureHelper.StorageAccount.CreateCloudBlobClient().GetContainerReference(containerName); container.CreateIfNotExist(); CloudBlob blob = container.GetBlobReference(blobName); using (BlobStream stream = blob.OpenWrite()) { byte[] bytes = new System.Text.UTF8Encoding().GetBytes("hello world"); stream.Write(bytes, 0, bytes.Length); } Assert.AreEqual(blob.Properties.Length, AzureHelper.GetBlob(blob.Uri).Properties.Length); }
public void PlainBlobTest() { // Set up the blob CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount; CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference("testcontainer"); container.CreateIfNotExist(); CloudBlob blob = container.GetBlobReference("testblob"); // Set up the data to write to a block byte[] bytes = new byte[8]; // Just a bunch of zeros // Put the blob using (BlobStream blobStream = blob.OpenWrite()) { blobStream.Write(bytes, 0, bytes.Length); } }
private static void UploadBlobStream(CloudBlob blob, string sourceFile) { FileStream fileStream = File.OpenRead(sourceFile); using (fileStream) { byte[] numArray = new byte[0x20000]; BlobStream blobStream = blob.OpenWrite(); using (blobStream) { blobStream.BlockSize = (long)0x20000; while (true) { int num = fileStream.Read(numArray, 0, (int)numArray.Length); if (num <= 0) { break; } blobStream.Write(numArray, 0, num); } } } }
private static void UploadBlobStream(CloudBlob blob, string sourceFile) { using (FileStream readStream = File.OpenRead(sourceFile)) { byte[] buffer = new byte[1024 * 128]; using (BlobStream blobStream = blob.OpenWrite()) { blobStream.BlockSize = 1024 * 128; while (true) { int bytesCount = readStream.Read(buffer, 0, buffer.Length); if (bytesCount <= 0) { break; } blobStream.Write(buffer, 0, bytesCount); } } } }
/// <summary> /// Writes the bytes from the specified stream. /// </summary> /// /// <param name="stream"> /// The stream to get the bytes from to write to the blob. /// </param> /// /// <exception name="ArgumentNullException"> /// If <paramref name="stream"/> is <b>null</b>. /// </exception> /// /// <exception cref="HealthHttpException"> /// If there is an error writing the data to HealthVault. /// </exception> /// public void Write(Stream stream) { Validator.ThrowIfArgumentNull(stream, nameof(stream), Resources.ArgumentNull); int bufferSize = DefaultStreamBufferSize; if (stream.CanSeek) { bufferSize = (int)Math.Min(stream.Length, DefaultStreamBufferSize); } byte[] bytes = new byte[bufferSize]; using (BlobStream blobStream = GetWriterStream()) { int bytesRead; while ((bytesRead = stream.Read(bytes, 0, bufferSize)) > 0) { blobStream.Write(bytes, 0, bytesRead); } } IsDirty = true; }