由于azure上各个云服务都是分开的,所以当一个地方生成文件后,我在别的地方想要读取他就变得困难,不过azure上面有解决方案,就是azure blob storage。
在现有的订阅账号下,新建一个存储blob,分私有和公共,私有的为私有存储,外部链接无法访问,公共则可以在外部直接通过链接进行存储内容的访问。
首先在cloud service和portal配置文件中均需添加一个key为storageAccount,value则为azure上的blobend,queueend,tableend,以及他的thumbprint。
在configuration文件中配置改存储名称。
public static string EmailFileContainer { get { return "email-response-save"; } }
建立一个BlobClient类,用来执行blob事件。
1 public class BlobClient 2 { 3 private static CloudBlobClient _cloudBlobClient; 4 5 static BlobClient() 6 { 7 _cloudBlobClient = CloudStorageAccount.Parse(Config.CloudStorageAccount).CreateCloudBlobClient(); 8 _cloudBlobClient.RetryPolicy = new LinearRetry(TimeSpan.FromSeconds(1), 3); 9 } 10 11 /// <summary> 12 /// Get file from blob 13 /// </summary> 14 /// <param name="containerName"></param> 15 /// <param name="blobName"></param> 16 /// <param name="blobPath"></param> 17 public static void GetBlob(string containerName, string blobName, string blobPath) 18 { 19 var container = _cloudBlobClient.GetContainerReference(containerName); 20 21 var blob = container.GetBlockBlobReference(blobName); 22 var mode = System.IO.FileMode.Create; 23 blob.DownloadToFileAsync(blobPath, mode); 24 25 return; 26 } 27 28 /// <summary> 29 /// Add file to blob 30 /// </summary> 31 /// <param name="containerName"></param> 32 /// <param name="blobName"></param> 33 /// <param name="blobPath"></param> 34 /// <param name="blobFileName"></param> 35 public static void AddBlob(string containerName, string blobName, string blobPath,string blobFileName) 36 { 37 var container = _cloudBlobClient.GetContainerReference(containerName); 38 container.CreateIfNotExists(); 39 40 var blob = container.GetBlockBlobReference(blobName); 41 var mode = System.IO.FileMode.Open; 42 blob.UploadFromFileAsync(blobPath+blobFileName, mode); 43 44 return; 45 } 46 47 public static void DeleteBlob(string containerName, string blobName) 48 { 49 var container = _cloudBlobClient.GetContainerReference(containerName); 50 container.CreateIfNotExists(); 51 52 var blob = container.GetBlockBlobReference(blobName); 53 blob.DeleteIfExistsAsync(); 54 55 return; 56 } 57 58 public static void DeleteBlobContainer(string containerName) 59 { 60 var container = _cloudBlobClient.GetContainerReference(containerName); 61 container.CreateIfNotExists(); 62 63 foreach (var item in container.ListBlobs(null, false)) 64 { 65 foreach (var item1 in ((CloudBlobDirectory)item).ListBlobs()) 66 { 67 foreach (var item2 in ((CloudBlobDirectory)item1).ListBlobs()) 68 { 69 ((CloudBlockBlob)item2).DeleteIfExistsAsync(); 70 } 71 } 72 } 73 } 74 }
执行写入blob storage步骤。
BlobClient.AddBlob(Config.EmailFileContainer, "MailExportTotalImage/"+ imgName, location, imgName);
读取blob storage中的文件。
BlobClient.GetBlob(Config.EmailFileContainer, "MailExportAttachment1/" + sFileName, uploadPath + sFileName);
将内存流数据存入TXT。
var fn = DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt"; byte[] bs = stream.ToArray(); String contentStr = System.Text.Encoding.UTF8.GetString(bs);
BlobClient.AddBlob(Config.FileContainer, fn, contentStr);
若要创建公共可访问文件的链接,需在对应云服务的configuration文件中申明一个设置名称。
<ConfigurationSettings> <!--Local--> <Setting name="aaaa" value="bbbbbbbbbbb" /> . . . <Setting name="EmailBlobStorage" value="http(s)://xxx.blob.core.chinacloudapi.cn/email-response-save/" /> </ConfigurationSettings>
核心configuration文件中建立一个指向云服务configuration特定设置名称的属性。
public static string EmailBlobStorage { get { return CloudConfigurationManager.GetSetting("EmailBlobStorage"); } }
使用:
var location = Config.EmailBlobStorage; var imgsrc = location + "MailExportTotalImage/" + string.Format("{0:yyyy-MM-dd}", DateTime.Today.ToGmt8()) + _title + ".png";