private const string MimeTypeName = "text/plain"; // since these are assumed to #endregion Fields #region Methods /// <summary> /// ugliness of downloading and reuploading whole blob. /// </summary> /// <param name="blob"></param> /// <param name="toAdd"></param> public static void appendToBlob(CloudBlob blob, string toAdd) { string oldLogData = ""; if (Exists(blob)) oldLogData = blob.DownloadText(); blob.DeleteIfExists(); blob.UploadText(oldLogData + "\r\n" + toAdd); }
Tuple<string, IHtmlString> Pygmentize(CloudBlob blob) { var filename = Uri.UnescapeDataString(blob.Uri.Segments.Last()); var extension = Path.GetExtension(filename).ToLower(); if (extension == ".mdown") { return new Tuple<string, IHtmlString>(null, new MvcHtmlString(new Markdown().Transform(blob.DownloadText()))); } var formatters = new Dictionary<string, string>() { { ".cscfg", "xml" }, { ".csdef", "xml" }, { ".config", "xml" }, { ".xml", "xml" }, { ".cmd", "bat" }, { ".rb", "ruby" }, { ".cs", "csharp" }, { ".html", "html" }, { ".cshtml", "html" }, { ".css", "css" }, { ".erb", "erb" }, { ".haml", "haml" }, { ".js", "javascript" }, { ".php", "php" }, { ".py", "python" }, { ".yaml", "yaml" }, { ".yml", "yaml" }, { ".txt", "text" } }; var executable = "pygmentize"; if (RoleEnvironment.IsAvailable) { executable = Path.Combine(RoleEnvironment.GetLocalResource("Python").RootPath, @"scripts\pygmentize.exe"); } if (!formatters.ContainsKey(extension)) { extension = ".txt"; } var startInfo = new ProcessStartInfo(executable, string.Format("-f html -l {0}", formatters[extension])) { UseShellExecute = false, CreateNoWindow = true, RedirectStandardInput = true, RedirectStandardOutput = true, }; var proc = Process.Start(startInfo); proc.StandardInput.Write(blob.DownloadText().Trim(new char[]{'\uFEFF'})); proc.StandardInput.Close(); return new Tuple<string, IHtmlString>(filename, new MvcHtmlString(proc.StandardOutput.ReadToEnd())); }
public static void RenderTemplateWithContentToBlob(CloudBlob template, CloudBlob renderTarget, InformationSource setAsDefaultSource = null) { InformationSourceCollection sources = renderTarget.GetBlobInformationSources(); if(sources == null) { sources = CreateDefaultSources(template); } if (setAsDefaultSource != null) { sources.SetDefaultSource(setAsDefaultSource); } string templateContent = template.DownloadText(); List<ContentItem> existingRoots = GetExistingRoots(sources); string renderResult = RenderTemplateWithContentRoots(templateContent, existingRoots); bool rerenderRequired = UpdateMismatchedRootsToSources(sources, existingRoots, renderTarget); renderTarget.SetBlobInformationSources(sources); renderTarget.UploadBlobText(renderResult, StorageSupport.InformationType_RenderedWebPage); if(rerenderRequired) { RenderTemplateWithContentToBlob(template, renderTarget); } else { sources.SubscribeTargetToSourceChanges(renderTarget); } }
public static string ReadBlobFile(string fileName) { // byte[] fileBytes = null; string fileBytes = string.Empty; 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); BlobRequestOptions blobReqOptions = new BlobRequestOptions(); blobReqOptions.Timeout = new TimeSpan(0, 5, 0); fileBytes = blob.DownloadText(blobReqOptions); } catch (System.Exception ex) { Logger.Write(string.Format("Error in ReadBlobFile() Error: {0}", ex.Message)); fileBytes = null; } return fileBytes; }
private static string DownloadText(CloudBlob blob) { return blob.DownloadText(); }