public static void RunAppendBlob(CloudBlobContainer container, int times) { CloudAppendBlob appendBlob = container.GetAppendBlobReference("x/test.csv"); appendBlob.CreateOrReplace(); int nLabels = 10; string record = FillRecord("thing", nLabels); var watcher = Stopwatch.StartNew(); appendBlob.AppendText(record); for (int i = 0; i < times; i++) { Console.WriteLine(((i + 1.0) / times) * 100 + "%"); appendBlob.AppendText(record); } Console.WriteLine("ElapsedTime: " + watcher.Elapsed); Console.WriteLine("Blob length (bytes): " + appendBlob.Properties.Length); Console.Read(); }
private void uploadToBlob(string fileName, string message) { CloudAppendBlob blob = this.container.GetAppendBlobReference(fileName); //make sure the file exsits if (!blob.Exists()) { blob.CreateOrReplace(); } try { blob.AppendText(message); } catch (StorageException e1) { //retry after sleep System.Threading.Thread.Sleep(10); try { blob.AppendText(message); } catch (StorageException e) { writeToLocalFile(fileName, message + "\r\n" + e.ToString() + "\r\n"); } } }
public static void Read([QueueTrigger("customers", Connection = "AzureWebJobsStorage")] string message, TraceWriter log) { //Deserialize customer from the queue Customer customer = JsonConvert.DeserializeObject <Customer>(message); #if Debug var builder = new ConfigurationBuilder() .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true); IConfiguration Configuration = builder.Build(); string con = Configuration["Values:AzureWebJobsStorage"]; #endif string con = System.Environment.GetEnvironmentVariable("AzureWebJobsStorage"); CloudBlobClient blobClient = CloudStorageAccount.Parse(con).CreateCloudBlobClient(); var folder = "files"; CloudBlobContainer container = blobClient.GetContainerReference(folder); container.CreateIfNotExists(); CloudAppendBlob blob = container.GetAppendBlobReference("customers.txt"); if (!blob.Exists()) { blob.CreateOrReplace(); } blob.AppendText(customer.Name + ";" + customer.Age + ";" + customer.Phone + "\n"); // blob.UploadText(customer.Name + ";" + customer.Age + ";" + customer.Phone); }
public void UploadToAppendBlob() { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference("my-append-blobs"); container.CreateIfNotExists(); CloudAppendBlob appendBlob = container.GetAppendBlobReference("append-blob.log"); if (!appendBlob.Exists()) { appendBlob.CreateOrReplace(); } int numBlocks = 10; Random rnd = new Random(); byte[] bytes = new byte[numBlocks]; rnd.NextBytes(bytes); //Simulate a logging operation by writing text data and byte data to the end of the append blob. for (int i = 0; i < numBlocks; i++) { appendBlob.AppendText(string.Format("Timestamp: {0:u} \tLog Entry: {1}{2}", DateTime.UtcNow, bytes[i], Environment.NewLine)); } Console.WriteLine(appendBlob.DownloadText()); }
public static void WriteLogs(string log) { string connectionString = @"DefaultEndpointsProtocol=https;AccountName=hotelmanagerblob;AccountKey=0rIspWPXwm7U22uvlHZshW6mHeJKLw1W5Tu3OQbi8jUasSRVju61eU0teHvNHwtzuGW68vmtRH9uE/rlhoqAnQ==;EndpointSuffix=core.windows.net"; string file = "appendBlop.txt"; CloudStorageAccount account = CloudStorageAccount.Parse(connectionString); if (account == null) return; CloudBlobClient client = account.CreateCloudBlobClient(); if (client == null) return; CloudBlobContainer container = client.GetContainerReference("hotelmanagerlog"); try { container.CreateIfNotExists(); } catch (Exception e) { Debug.WriteLine(e.Message); } CloudAppendBlob blob = container.GetAppendBlobReference(file); if (!blob.Exists()) blob.CreateOrReplace(); blob.AppendText($"{log} {Environment.NewLine}"); }
protected override void Write(LogEventInfo logEvent) { if (_client == null) { return; } string containerName = Container.Render(logEvent); string blobName = BlobName.Render(logEvent); if (_container == null || _container.Name != containerName) { _container = _client.GetContainerReference(containerName); _blob = null; } if (_blob == null || _blob.Name != blobName) { _blob = _container.GetAppendBlobReference(blobName); if (!_blob.Exists()) { try { _blob.Properties.ContentType = "text/plain"; _blob.CreateOrReplace(AccessCondition.GenerateIfNotExistsCondition(), null, null); } catch (StorageException) { } } } var logMessage = this.Layout.Render(logEvent); _blob.AppendText(logMessage + "\r\n", Encoding.UTF8); }
/// <summary> /// Writes logging event to the log target. /// classes. /// </summary> /// <param name="logEvent">Logging event to be written out.</param> protected override void Write(LogEventInfo logEvent) { if (String.IsNullOrEmpty(logEvent.Message)) { return; } var cn = Container.Render(logEvent); var bn = BlobName.Render(logEvent); var containerName = CheckAndRepairContainerNamingRules(cn); var blobName = CheckAndRepairBlobNamingRules(bn); var logMessage = string.Concat(Layout.Render(logEvent), Environment.NewLine); InitializeContainer(containerName); InitializeBlob(blobName); _appendBlob.AppendText(logMessage); }
public static void Run([TimerTrigger("0 0 */4 * * *")] TimerInfo myTimer, TraceWriter log) { bool found = true; storageAccount = CloudStorageAccount.Parse(storageAcct); queueClient = storageAccount.CreateCloudQueueClient(); loggingQueue = queueClient.GetQueueReference(loggingQueueName); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer blobContainer = blobClient.GetContainerReference("logging"); blobContainer.CreateIfNotExists(); loggingQueue.FetchAttributes(); int?cachedMessageCount = loggingQueue.ApproximateMessageCount; if (cachedMessageCount == 0 || cachedMessageCount == null) { return; } string createddate = Convert.ToDateTime(DateTime.UtcNow).ToString("yyyy-MM-dd/HH-mm/"); string blobName = "log/" + createddate + "telemetry.json"; CloudAppendBlob appendBlob = blobContainer.GetAppendBlobReference(blobName); appendBlob.Properties.ContentType = "text/json"; appendBlob.CreateOrReplace(); while (cachedMessageCount > 0 && found) { found = false; int?messagesToRetrieve = cachedMessageCount > 32 ? 32 : cachedMessageCount; cachedMessageCount = cachedMessageCount - messagesToRetrieve; foreach (CloudQueueMessage message in loggingQueue.GetMessages((int)messagesToRetrieve, TimeSpan.FromMinutes(2))) { found = true; try { appendBlob.AppendText(message.AsString + Environment.NewLine); loggingQueue.DeleteMessage(message); } catch { log.Info("problem logging data"); } } } }
public void AddStringToFile(string containerName, string fileName, string content) { var container = OpenOrInitializeContainer(containerName); CloudAppendBlob appendBlob = container.GetAppendBlobReference(fileName); //Create the append blob. Note that if the blob already exists, the CreateOrReplace() method will overwrite it. //You can check whether the blob exists to avoid overwriting it by using CloudAppendBlob.Exists(). appendBlob.CreateOrReplace(); appendBlob.AppendText($"Timestamp: {DateTime.UtcNow} \tLog Entry: {content}"); appendBlob.DownloadText(); }
protected override void Write(LogEventInfo logEvent) { var connectionString = ConnectionString.Render(logEvent); if (_client == null || !string.Equals(_connectionString, connectionString, System.StringComparison.OrdinalIgnoreCase)) { _client = CloudStorageAccount.Parse(connectionString).CreateCloudBlobClient(); InternalLogger.Debug("Initialized connection to {0}", connectionString); } string containerName = Container.Render(logEvent); string blobName = BlobName.Render(logEvent); if (_container == null || _container.Name != containerName) { _container = _client.GetContainerReference(containerName); InternalLogger.Debug("Got container reference to {0}", containerName); if (_container.CreateIfNotExists()) { InternalLogger.Debug("Created container {0}", containerName); } _blob = null; } if (_blob == null || _blob.Name != blobName) { _blob = _container.GetAppendBlobReference(blobName); if (!_blob.Exists()) { try { _blob.Properties.ContentType = "text/plain"; _blob.CreateOrReplace(AccessCondition.GenerateIfNotExistsCondition()); InternalLogger.Debug("Created blob: {0}", blobName); } catch (StorageException ex) when(ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.Conflict) { // to be expected } } } _blob.AppendText(Layout.Render(logEvent) + "\r\n", Encoding.UTF8); }
public static void WriteUsage(KloggerDetail infoToLog) { Log.Logger.Write(LogEventLevel.Information, "{@KloggerDetail}", infoToLog); CloudBlobClient cloudBlobClient = accountBlob.CreateCloudBlobClient(); CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("app-logs"); cloudBlobContainer.CreateIfNotExists(); DateTime today = DateTime.Today; var year = today.Year; var month = today.Month; var day = today.Day; CloudAppendBlob cloudAppendBlob = cloudBlobContainer.GetAppendBlobReference(string.Format("{0}/{1}/{2}/log.csv", year, month, day)); cloudAppendBlob.AppendText("Content added"); }
protected override void SendBuffer(LoggingEvent[] events) { CloudAppendBlob appendBlob = _cloudBlobContainer.GetAppendBlobReference(Filename(DirectoryName)); if (!appendBlob.Exists()) { appendBlob.CreateOrReplace(); } StringBuilder sb = new StringBuilder(); foreach (var loggingEvent in events) { sb.Append(RenderLoggingEvent(loggingEvent)); } appendBlob.AppendText(sb.ToString()); }
private void WriteToAppendBlob(string logLevel, string log) { DateTime date = DateTime.Today; DateTime dateLogEntry = DateTime.UtcNow; try { CloudAppendBlob appBlob = _container.GetAppendBlobReference(string.Format("{0}{1}", date.ToString("yyyy-MM-dd"), ".log")); if (!appBlob.Exists()) { appBlob.CreateOrReplace(); } appBlob.AppendText(string.Format("{0}\t{1}\t{2}\r\n", dateLogEntry.ToString("o"), logLevel, log)); } catch (Exception) { ; } }
static bool SendJsonToStorage(CloudBlobClient blobClient, string fullText, string timeStamp) { BlobContinuationToken continuationToken = null; BlobResultSegment resultSegment = null; try { //Convert the unix timestamp to a date DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); dtDateTime = dtDateTime.AddSeconds(Double.Parse(timeStamp)); String outputPath = String.Format(@"logs/post/{0}/{1}/{2}", dtDateTime.Year, dtDateTime.Month, dtDateTime.Day); // Retrieve reference to a previously created container https: //pcldevbgwilkinson01.blob.core.windows.net/sensor-hub/logs/pre/2018/04/06/0_8b385536905846c68aa7a3e4e476200b_1.json CloudBlobContainer container = blobClient.GetContainerReference("sensor-hub"); resultSegment = container.ListBlobsSegmented(outputPath, true, BlobListingDetails.All, 1000, continuationToken, null, null); if (resultSegment.Results.Count() == 1) { fullText = Environment.NewLine + fullText; CloudAppendBlob blob = (CloudAppendBlob)resultSegment.Results.First(); blob.AppendText(fullText); } else { CloudAppendBlob blob = container.GetAppendBlobReference(outputPath + "/" + Guid.NewGuid() + ".json"); blob.UploadText(fullText); } } catch (Exception e) { Console.WriteLine("Failue uploading the blob: " + e.Message); return(false); } return(true); }
protected override void Write(LogEventInfo logEvent) { if (_client == null) { return; } string containerName = Container.Render(logEvent); string blobName = BlobName.Render(logEvent); if (_container == null || _container.Name != containerName) { _container = _client.GetContainerReference(containerName); _blob = null; } if (_blob == null || _blob.Name != blobName) { _blob = _container.GetAppendBlobReference(blobName); if (!_blob.Exists()) { try { _blob.Properties.ContentType = "text/plain"; _blob.CreateOrReplace(AccessCondition.GenerateIfNotExistsCondition()); } catch (StorageException ex) when(ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.Conflict) { // to be expected } } } _blob.AppendText(Layout.Render(logEvent) + "\r\n", Encoding.UTF8); }
public void Append(string fileName, string content) { CloudAppendBlob blockBlob = this.cloudBlobContainer.GetAppendBlobReference(fileName); blockBlob.AppendText(content); }
private static async Task AppendBlobProcessAsync() { CloudStorageAccount storageAccount = null; CloudBlobContainer cloudBlobContainer = null; string sourceFile = null; string destinationFile = null; // Retrieve the connection string for use with the application. The storage connection string is stored // in an environment variable on the machine running the application called storageconnectionstring. // If the environment variable is created after the application is launched in a console or with Visual // Studio, the shell needs to be closed and reloaded to take the environment variable into account. string storageConnectionString = ConfigurationManager.AppSettings["StorageConnectionString"]; //Environment.GetEnvironmentVariable("StorageConnectionString"); //storageAccount = CloudStorageAccount.DevelopmentStorageAccount; // Check whether the connection string can be parsed. if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount)) { try { // Create the CloudBlobClient that represents the Blob storage endpoint for the storage account. ////CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); //127.0.0.1:10000/devstoreaccount1/testcont // Create a container called 'quickstartblobs' and append a GUID value to it to make the name unique. ////cloudBlobContainer = cloudBlobClient.GetContainerReference("testcontappendblob");// "quickstartblobs" + Guid.NewGuid().ToString()); ////await cloudBlobContainer.CreateIfNotExistsAsync(); ////Console.WriteLine("Created container '{0}'", cloudBlobContainer.Name); ////Console.WriteLine(); ////// Set the permissions so the blobs are public. ////BlobContainerPermissions permissions = new BlobContainerPermissions ////{ //// PublicAccess = BlobContainerPublicAccessType.Blob ////}; ////await cloudBlobContainer.SetPermissionsAsync(permissions); // Get a reference to the blob address, then upload the file to the blob. // Use the value of localFileName for the blob name. CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); cloudBlobContainer = cloudBlobClient.GetContainerReference("testcont"); cloudBlobContainer.CreateIfNotExists(); CloudAppendBlob cloudAppendBlob = cloudBlobContainer.GetAppendBlobReference("1.txt"); cloudAppendBlob.CreateOrReplace(); cloudAppendBlob.AppendText("Content added"); cloudAppendBlob.AppendText("More content added"); cloudAppendBlob.AppendText("Even more content added"); string appendBlobContent = cloudAppendBlob.DownloadText(); //cloudAppendBlob.UploadFromFile(sourceFile); // List the blobs in the container. Console.WriteLine("Listing blobs in container."); BlobContinuationToken blobContinuationToken = null; do { var results = await cloudBlobContainer.ListBlobsSegmentedAsync(null, blobContinuationToken); // Get the value of the continuation token returned by the listing call. blobContinuationToken = results.ContinuationToken; foreach (IListBlobItem item in results.Results) { Console.WriteLine(item.Uri); } } while (blobContinuationToken != null); // Loop while the continuation token is not null. destinationFile = sourceFile.Replace(".txt", "_DOWNLOADED.txt"); Console.WriteLine("Downloading blob to {0}", destinationFile); Console.WriteLine(); await cloudAppendBlob.DownloadToFileAsync(destinationFile, FileMode.Create); File.WriteAllText(sourceFile, "Hello, World, I am good, how is your health!"); await cloudAppendBlob.UploadFromFileAsync(sourceFile); await cloudAppendBlob.DeleteAsync(); } catch (StorageException ex) { Console.WriteLine("Error returned from the service: {0}", ex.Message); } finally { // Console.WriteLine("Press any key to delete the sample files and example container."); Console.ReadLine(); // Clean up resources. This includes the container and the two temp files. //Console.WriteLine("Deleting the container and any blobs it contains"); if (cloudBlobContainer != null) { // await cloudBlobContainer.DeleteIfExistsAsync(); } File.Delete(sourceFile); File.Delete(destinationFile); } } else { Console.WriteLine( "A connection string has not been defined in the system environment variables. " + "Add a environment variable named 'storageconnectionstring' with your storage " + "connection string as a value."); } }
static void Main(string[] args) { Assembly execAssembly = Assembly.GetCallingAssembly(); AssemblyName assemblyName = execAssembly.GetName(); int traceLevel = 3; int event_count = 0; int total_event_count = 0; int updowncount = 0; List <EventData> e = new List <EventData>(); //EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(connectionString); EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(CloudConfigurationManager.GetSetting("EventHubConnectionString")); // Parse the connection string and return a reference to the storage account. CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); // Create the blob client. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Retrieve reference to a previously created container. CloudBlobContainer container = blobClient.GetContainerReference("syslogs"); // Retrieve reference to a previously created container for output logs CloudBlobContainer containerlogs = blobClient.GetContainerReference("logs"); // Create the logs container if it does not already exist containerlogs.CreateIfNotExists(); CloudAppendBlob logout = containerlogs.GetAppendBlobReference("ideapoceh04out"); if (!logout.Exists()) { logout.CreateOrReplace(); } logout.AppendText(String.Format("Time: {0:u} : Assembly: {1} \t : Beginning execution {2}", DateTime.UtcNow, assemblyName, Environment.NewLine)); if (traceLevel == 4) { // Loop over items within the container and output the length and URI. foreach (IListBlobItem item in container.ListBlobs(null, false)) { if (item.GetType() == typeof(CloudBlockBlob)) { CloudBlockBlob blob = (CloudBlockBlob)item; //Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri); logout.AppendText(String.Format("Time: {0:u} : Assembly: {1} \t : Block blob of length {2}: {3} {4}", DateTime.UtcNow, assemblyName, blob.Properties.Length, blob.Uri, Environment.NewLine)); } else if (item.GetType() == typeof(CloudPageBlob)) { CloudPageBlob pageBlob = (CloudPageBlob)item; //Console.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri); logout.AppendText(String.Format("Time: {0:u} : Assembly: {1} \t : Page blob of length {2}: {3} {4}", DateTime.UtcNow, assemblyName, pageBlob.Properties.Length, pageBlob.Uri, Environment.NewLine)); } else if (item.GetType() == typeof(CloudBlobDirectory)) { CloudBlobDirectory directory = (CloudBlobDirectory)item; //Console.WriteLine("Directory: {0}", directory.Uri); logout.AppendText(String.Format("Time: {0:u} : Assembly: {1} \t Directory {2}: {3}", DateTime.UtcNow, assemblyName, directory.Uri, Environment.NewLine)); } else if (item.GetType() == typeof(CloudAppendBlob)) { CloudAppendBlob appendBlob = (CloudAppendBlob)item; //Console.WriteLine("Append blob of length: {0}: {1}", appendBlob.Properties.Length, appendBlob.Uri); logout.AppendText(String.Format("Time: {0:u} : Assembly: {1} \t : Append blob of length {2}: {3} {4}", DateTime.UtcNow, assemblyName, appendBlob.Properties.Length, appendBlob.Uri, Environment.NewLine)); } } } //Console.ReadLine(); // Retrieve reference to a blob named "myblob.txt" CloudBlockBlob blockBlob2 = container.GetBlockBlobReference("mls_syslog"); string line; //string json_eh_message; DateTime startTime = DateTime.UtcNow; //using (var memoryStream = new MemoryStream()) using (var stream = blockBlob2.OpenRead()) { using (StreamReader reader = new StreamReader(stream)) { // while ( !reader.EndOfStream) while ((line = reader.ReadLine()) != null) { // Console.WriteLine(reader.ReadLine()); //Console.WriteLine(line); if (traceLevel == 4) { logout.AppendText(String.Format("Time: {0:u} : Assembly: {1} \t : {2} {3}", DateTime.UtcNow, assemblyName, line, Environment.NewLine)); } //Console.ReadLine(); string[] items = line.Split('\t'); syslog sl = new syslog(); sl.time = items[0]; sl.facility = items[1]; sl.address = items[2]; sl.message = items[3]; bool testupdown = sl.message.StartsWith("%LINK-3-UPDOWN:"); /* Console.WriteLine("Found UPDOWN? {0}", testupdown); */ if (testupdown) { updowncount++; string[] messageItems = sl.message.Split(' '); string msgInterface = messageItems[2]; /* Console.WriteLine("Interface is {0}", msgInterface); */ sl.netInterface = msgInterface; } // string json_eh_message = JsonConvert.SerializeObject(sl); json_eh_message = JsonConvert.SerializeObject(sl); if (event_count > 500) { try { //eventHubClient.SendBatchAsync(e); eventHubClient.SendBatch(e); logout.AppendText(String.Format("Time: {0:u} : Assembly: {1} \t : Submitting a batch of {2} events to EventHub {3}", DateTime.UtcNow, assemblyName, event_count, Environment.NewLine)); //Console.WriteLine("Submitting batch of {0} events", event_count); } catch (Exception exception) { //Console.ForegroundColor = ConsoleColor.Red; //Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message); //Console.ResetColor(); logout.AppendText(String.Format("Time: {0:u} : Assembly: {1} \t : Exception raised: {2} {3}", DateTime.UtcNow, assemblyName, exception.Message, Environment.NewLine)); } total_event_count = total_event_count + event_count; event_count = 0; //Console.WriteLine("Total events submitted: {0}", total_event_count); e.Clear(); e.Add(new EventData(Encoding.UTF8.GetBytes(json_eh_message))); event_count++; //Console.WriteLine(json_eh_message); } else { e.Add(new EventData(Encoding.UTF8.GetBytes(json_eh_message))); event_count++; //Console.WriteLine(json_eh_message); } } // EOF -- submit final batch of remaining events try { //eventHubClient.SendBatchAsync(e); eventHubClient.SendBatch(e); //Console.WriteLine("Submitting final batch of {0}", event_count); logout.AppendText(String.Format("Time: {0:u} : Assembly: {1} \t : Submitting final batch of {2} events to EventHub {3}", DateTime.UtcNow, assemblyName, event_count, Environment.NewLine)); } catch (Exception exception) { //Console.ForegroundColor = ConsoleColor.Red; //Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message); //Console.ResetColor(); logout.AppendText(String.Format("Time: {0:u} : Assembly: {1} \t : Exception occurred while submitting batch to EventHub: {2} {3}", DateTime.UtcNow, assemblyName, exception.Message, Environment.NewLine)); } total_event_count += event_count; } } DateTime endTime = DateTime.UtcNow; var ts = new TimeSpan(startTime.Ticks - endTime.Ticks); double delta = Math.Abs(ts.TotalSeconds); logout.AppendText(String.Format("Time: {0:u} : Assembly: {1} \t : {2} link up/down events processed {3}", DateTime.UtcNow, assemblyName, updowncount, Environment.NewLine)); logout.AppendText(String.Format("Time: {0:u} : Assembly: {1} \t : {2} total events processed {3}", DateTime.UtcNow, assemblyName, total_event_count, Environment.NewLine)); logout.AppendText(String.Format("Time: {0:u} : Assembly: {1} \t : {2} events per second {3}", DateTime.UtcNow, assemblyName, total_event_count / delta, Environment.NewLine)); logout.AppendText(String.Format("Time: {0:u} : Assembly: {1} \t : Ending execution {2}", DateTime.UtcNow, assemblyName, Environment.NewLine)); //Console.WriteLine("Port flap event count: {0}", updowncount); //Console.WriteLine("Total events: {0}", total_event_count); //Console.ReadLine(); //using (var memoryStream2 = new BufferedStream(memoryStream)) /* { * blockBlob2.DownloadToStream(memoryStream); * //while ((text = memoryStream2.Read) * text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray()); * text = memoryStream. * Console.WriteLine(text); * Console.ReadLine(); * } */ }
static void AzureStorageBlobExamples() { // Getting configuration from the App.config file and get access to the CloudStorageAccount. // Azure access Key should is stored in the App.config String storageKeyValue = CloudConfigurationManager.GetSetting("AzureStorageAccount"); // Just in case to check whether reading the correct value Console.WriteLine("Storage Account Key Used" + storageKeyValue); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageKeyValue); // Getting the Azure Client CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); // Getting reference for the container CloudBlobContainer cloudContainer = cloudBlobClient.GetContainerReference("images"); // Create the container (folder) if does not exists cloudContainer.CreateIfNotExists(); // Get the reference for the Blob in Azure CloudBlockBlob blobCloud = cloudContainer.GetBlockBlobReference("photo.png"); // Learning 1: Copy a file from local path in to Azure Blob storage //Open a local file and upload to the Azure using (var fileStream = System.IO.File.OpenRead(@"C:\Users\Administrator\Pictures\photo.jpg")) { blobCloud.UploadFromStream(fileStream); Console.WriteLine("File is uploaded to the cloud"); } // Learning 2: Download a blob from Azure to the local host // Just copy a blob from cloud to a local file. Assume the same file which has been uploaded in the previous step blobCloud.DownloadToFile(@"C:\Users\Administrator\Pictures\photocopy.jpg", FileMode.CreateNew); // Learning 3: List all blob objects URI from a container //List all blobs in a container var blobs = cloudContainer.ListBlobs(); foreach (var blob in blobs) { Console.Write(blob.Uri); } // Learning 4: List properties of a continer (Folder) and listing metadata of a container cloudContainer.FetchAttributes(); Console.WriteLine(cloudContainer.Properties.LastModified); Console.WriteLine(cloudContainer.Properties.ETag); var metaData = cloudContainer.Metadata; foreach (var metaDataItem in metaData) { Console.Write("Key " + metaDataItem.Key + " & "); Console.WriteLine("Value" + metaDataItem.Value); } // Learning 5: Setting metaData for a container // Method 1 cloudContainer.Metadata.Add("SampleKey", "SampleValue"); // Method 2 cloudContainer.Metadata["SecondSample"] = "Second Value"; // Dont ask me why FetchAttributes() and SetMetadata() ! why not SetAttributes() or GetMetaData? Microsoft Way! cloudContainer.SetMetadata(); // Learning 6: Setting permission for a container BlobContainerPermissions permissions = cloudContainer.GetPermissions(); Console.WriteLine("Container permission " + permissions.PublicAccess.ToString()); foreach (var sharedAccessPolicy in permissions.SharedAccessPolicies) { Console.WriteLine(sharedAccessPolicy.Key.ToString() + " = " + sharedAccessPolicy.Value.ToString()); } // In order to as per the parent container permissions.PublicAccess = BlobContainerPublicAccessType.Container; // In order to remove the public access permissions.PublicAccess = BlobContainerPublicAccessType.Off; //Finally set the permission cloudContainer.SetPermissions(permissions); // Learning 7: Azure copy from one blob to another // Create a new Block Blog Reference CloudBlockBlob copyBlob = cloudContainer.GetBlockBlobReference("photo-copy.jpg"); // Copy the original - blobCloud to the copyBlob copyBlob.StartCopy(new Uri(blobCloud.Uri.AbsoluteUri)); // Learning 8: Copy all blobs from one container to another CloudBlobContainer sourceContainer = cloudContainer; CloudBlobContainer targetContainer = cloudBlobClient.GetContainerReference("newimages"); targetContainer.CreateIfNotExists(); foreach (var blob in sourceContainer.ListBlobs()) { var sourceBlob = blob as CloudBlob; Console.WriteLine("Source Blob " + sourceBlob.Name); CloudBlockBlob newBlob = targetContainer.GetBlockBlobReference(sourceBlob.Name); newBlob.StartCopy(new Uri(blob.Uri.AbsoluteUri)); } // Learning 9: Rename the blob CloudBlockBlob sourceBlockBlob = cloudContainer.GetBlockBlobReference("photo-copy.jpg"); CloudBlockBlob targetBlockBlob = cloudContainer.GetBlockBlobReference("copy-photo.jpg"); targetBlockBlob.StartCopy(new Uri(sourceBlockBlob.Uri.AbsoluteUri)); while (targetBlockBlob.CopyState.Status == CopyStatus.Pending) { // Sleep for 3 seconds System.Threading.Thread.Sleep(2000); } sourceBlockBlob.Delete(); // Learning 10: Appending to a blob DateTime date = DateTime.Today; CloudBlobContainer logContainer = cloudBlobClient.GetContainerReference("logs"); CloudAppendBlob logBlog = logContainer.GetAppendBlobReference(string.Format("{0}{1}", date.ToString("yyyyMMdd"), ".log")); logContainer.CreateIfNotExists(); // If the append blog does not exists, create one if (!logBlog.Exists()) { logBlog.CreateOrReplace(); } // AppendText logBlog.AppendText(string.Format("{0} : Azure is rocking in the cloud space at ", date.ToString("HH:MM:ss"))); // Similar to the AppendText, there are // logBlog.AppendBlock // logBlog.AppendFromByteArray // logBlog.AppendFromFile // logBlog.AppendFromStream // Finally display the content of the log file. Console.WriteLine(logBlog.DownloadText()); // Learning 11: Multiple Chunk of file upload to Azure AzureStorageReference.Program.uploadLargeFiles(cloudBlobClient, @"C:\Users\Administrator\Pictures"); //Learning 12: Upload using Async AsyncCallback callBack = new AsyncCallback(x => Console.WriteLine("Copy Async Completed")); CloudBlockBlob copyAsync = cloudContainer.GetBlockBlobReference("newphoto.png"); copyAsync.BeginStartCopy(blobCloud, callBack, null); Console.WriteLine("Press any key to continue"); Console.ReadKey(); }
/// <summary> /// Define how to write out the content of this file /// </summary> /// <param name="content">Text content to write out</param> protected override void WriteContent(string content) { _appendBlob.AppendText(content, _encoding); }
/// <summary> /// Write data to blob storage /// </summary> /// <param name="txt">text to be written to storage</param> private void Write(string txt) { _blockBlob.AppendText(txt); }