我是從一本書上看到了Azure Blob,而後在網上浪了一下子,發現了這篇文章,裏面他已經把重點的則重地方講完了,「飛機票:http://www.cnblogs.com/sparkdev/p/6441421.html#commentform」
可是對於我來講,一開始仍是有點困惑的。裏面的文章已經講得很詳細了,只是在作這個事情以前,你須要引入一個package(我爲了找這個package也是煞費苦心,後來才發現前一章節有講到。。。)
就是
Install-Package WindowsAzure.Storagehtml
安裝完成後大概就是這個樣子嘍app
好像還有些別的東西。。嗯。。。無論了。學習
在上面那個地址中已經介紹了添加文件,刪除文件啊一些基本操做,噢,對,千萬別忘記下一個這個軟件,url
連接: http://pan.baidu.com/s/1c15dhfi 密碼: rcarspa
原地址只有外網才能夠訪問的到,因此我放到了百度雲裏面,原做者應該不介意把。。。介意的話告訴我我去立馬刪除。。。
這個軟件很好用的,並且so easy,點那個像插頭同樣的圖標,而後就盡情的下一步吧~code
我把原做者寫的幾個示例集合到了一個方法中(也許這個方法還有不少須要改進的地方。。)
我貼到這裏來給你們用,這樣我走的坑,你們就儘可能別再掉進去了orm
1 public class BlobCommon 2 { 3 private CloudBlobContainer container; 4 5 public BlobCommon(string connectionUrl,string containerName) 6 { 7 CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionUrl); 8 CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 9 container = blobClient.GetContainerReference(containerName); 10 container.CreateIfNotExists(); 11 } 12 13 public BlobCommon() 14 { 15 try 16 { 17 var connectionUrl = ConfigurationManager.ConnectionStrings["BlobConnectionUrl"].ConnectionString; 18 var containerName = ConfigurationManager.AppSettings["BlobContainer"].ToString(); 19 CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionUrl); 20 CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 21 container = blobClient.GetContainerReference(containerName); 22 container.CreateIfNotExists(); 23 } 24 catch (Exception ex) 25 { 26 } 27 28 } 29 30 public bool SaveImage(string imageName,string file) 31 { 32 try 33 { 34 CloudBlockBlob blockBlob = container.GetBlockBlobReference(imageName); 35 using (var fileStream = System.IO.File.OpenRead(file)) 36 { 37 // 這是一個同步執行的方法 38 blockBlob.UploadFromStream(fileStream); 39 } 40 return true; 41 } 42 catch (Exception ex) 43 { 44 return false; 45 } 46 } 47 48 public bool SaveImage(string imageName, byte[] imageData) 49 { 50 try 51 { 52 CloudBlockBlob blockBlob = container.GetBlockBlobReference(imageName); 53 blockBlob.UploadFromByteArray(imageData,0, imageData.Length); 54 //using (var fileStream = System.IO.File.OpenRead(file)) 55 //{ 56 // // 這是一個同步執行的方法 57 // blockBlob.UploadFromStream(fileStream); 58 //} 59 return true; 60 } 61 catch (Exception ex) 62 { 63 return false; 64 } 65 } 66 67 public List<SupervisorReportImageModel> GetBlobImageList() 68 { 69 try 70 { 71 var list = new List<SupervisorReportImageModel>(); 72 foreach (IListBlobItem item in container.ListBlobs(null, false)) 73 { 74 if (item.GetType() == typeof(CloudBlockBlob)) 75 { 76 CloudBlockBlob blob = (CloudBlockBlob)item; 77 // todo something 78 list.Add(new SupervisorReportImageModel() 79 { 80 Name = blob.Name.ToString(), 81 Url = blob.SnapshotQualifiedStorageUri.SecondaryUri.ToString() 82 }); 83 } 84 else if (item.GetType() == typeof(CloudAppendBlob)) 85 { 86 CloudAppendBlob appendBlob = (CloudAppendBlob)item; 87 // todo something 88 list.Add(new SupervisorReportImageModel() 89 { 90 Name = appendBlob.Name.ToString(), 91 Url = appendBlob.SnapshotQualifiedStorageUri.SecondaryUri.ToString() 92 }); 93 } 94 else if (item.GetType() == typeof(CloudPageBlob)) 95 { 96 CloudPageBlob pageBlob = (CloudPageBlob)item; 97 // todo something 98 list.Add(new SupervisorReportImageModel() 99 { 100 Name = pageBlob.Name.ToString(), 101 Url = pageBlob.SnapshotQualifiedStorageUri.SecondaryUri.ToString() 102 }); 103 } 104 else if (item.GetType() == typeof(CloudBlobDirectory)) 105 { 106 CloudBlobDirectory directory = (CloudBlobDirectory)item; 107 // todo something 108 list.Add(new SupervisorReportImageModel() 109 { 110 Name = directory.Uri.ToString(), 111 Url = directory.StorageUri.SecondaryUri.ToString() 112 }); 113 } 114 } 115 return list; 116 } 117 catch (Exception ex) 118 { 119 return null; 120 } 121 } 122 123 public bool DownloadImage(string imageName,string saveUrl) 124 { 125 try 126 { 127 // 建立名稱爲 mypicture.png 的 Blob 對象的引用。 128 CloudBlockBlob blockBlob = container.GetBlockBlobReference(imageName); 129 // 把文件保存到本地。 130 using (var fileStream = System.IO.File.OpenWrite(saveUrl)) 131 { 132 blockBlob.DownloadToStream(fileStream); 133 } 134 return true; 135 } 136 catch (Exception ex) 137 { 138 return false; 139 } 140 } 141 142 public bool SetupBlobRole() 143 { 144 try 145 { 146 BlobContainerPermissions permissions = container.GetPermissions(); 147 // Container 中的全部 Blob 都能被訪問 148 permissions.PublicAccess = BlobContainerPublicAccessType.Container; 149 container.SetPermissions(permissions); 150 return true; 151 } 152 catch (Exception ex) 153 { 154 return false; 155 } 156 } 157 158 public bool DeleteImage(string imageName) 159 { 160 try 161 { 162 CloudBlockBlob blockBlob = container.GetBlockBlobReference(imageName); 163 blockBlob.DeleteIfExists(); 164 return true; 165 } 166 catch (Exception ex) 167 { 168 return false; 169 } 170 } 171 172 public SupervisorReportImageModel GetBlobImageByImageName(string imageName) 173 { 174 try 175 { 176 List<IListBlobItem> imageData = container.ListBlobs(imageName, false).ToList(); 177 foreach (var item in imageData) 178 { 179 if (item.GetType() == typeof(CloudBlockBlob)) 180 { 181 CloudBlockBlob blob = (CloudBlockBlob)item; 182 // todo something 183 return new SupervisorReportImageModel() 184 { 185 Name = blob.Name.ToString(), 186 Url = blob.SnapshotQualifiedStorageUri.SecondaryUri.ToString() 187 }; 188 } 189 else if (item.GetType() == typeof(CloudAppendBlob)) 190 { 191 CloudAppendBlob appendBlob = (CloudAppendBlob)item; 192 // todo something 193 return new SupervisorReportImageModel() 194 { 195 Name = appendBlob.Name.ToString(), 196 Url = appendBlob.SnapshotQualifiedStorageUri.SecondaryUri.ToString() 197 }; 198 } 199 else if (item.GetType() == typeof(CloudPageBlob)) 200 { 201 CloudPageBlob pageBlob = (CloudPageBlob)item; 202 // todo something 203 return new SupervisorReportImageModel() 204 { 205 Name = pageBlob.Name.ToString(), 206 Url = pageBlob.SnapshotQualifiedStorageUri.SecondaryUri.ToString() 207 }; 208 } 209 else if (item.GetType() == typeof(CloudBlobDirectory)) 210 { 211 CloudBlobDirectory directory = (CloudBlobDirectory)item; 212 // todo something 213 return new SupervisorReportImageModel() 214 { 215 Name = directory.Uri.ToString(), 216 Url = directory.StorageUri.SecondaryUri.ToString() 217 }; 218 } 219 } 220 return null; 221 } 222 catch (Exception ex) 223 { 224 return null; 225 } 226 } 227 }
----------------------------------------------------我是分割線-------------------------------------------------------
寫完以後我不禁得有一些困惑,我看到其中有一些方法能夠建立文件夾啊什麼的,但是暫時還不會用,還須要學習一會兒。或者有老司機教教我,給我一個使用文檔也能夠啦~htm
我如今有一個最大的難題,就是文件權限的問題。在文檔中,我看到了能夠獲取權限的方法,但是,若是個人圖片,提供url出來以後,我怎麼才能使用這個權限去下載圖片呢。。。對象