Guest User

Untitled

a guest
May 20th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. using Microsoft.WindowsAzure.Storage;
  2. using Microsoft.WindowsAzure.Storage.Blob;
  3. using System;
  4. using System.Threading.Tasks;
  5.  
  6. namespace AzureStorageTestConsole {
  7. class Program {
  8. static async Task Main(string[] args) {
  9. // 儲存體連線字串
  10. string connectionString = "xxxxxx";
  11.  
  12. // 嘗試剖析連線字串產生帳號實例
  13. if (CloudStorageAccount.TryParse(connectionString, out CloudStorageAccount storageAccount)) {
  14.  
  15. //取得客戶端實例
  16. CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
  17.  
  18. //取得並且嘗試建立儲存容器
  19. var cloudBlobContainer = cloudBlobClient.GetContainerReference("test");
  20. await cloudBlobContainer.CreateIfNotExistsAsync();
  21.  
  22. // Set the permissions so the blobs are public.
  23. // 設定儲存容器
  24. BlobContainerPermissions permissions = new BlobContainerPermissions {
  25. PublicAccess = BlobContainerPublicAccessType.Blob,
  26. };
  27. await cloudBlobContainer.SetPermissionsAsync(permissions);
  28.  
  29. // 建立Blob區塊
  30. CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("images/test.jpg");
  31. // 設定ContentType,預設為application/octet-stream
  32. cloudBlockBlob.Properties.ContentType = "image/jpeg";
  33. // 上傳檔案
  34. await cloudBlockBlob.UploadFromStreamAsync(System.IO.File.Open(@"C:\Users\XuPeiYao\Pictures\老虎.jpg", System.IO.FileMode.Open));
  35.  
  36. CloudBlockBlob cloudBlockBlob2 = cloudBlobContainer.GetBlockBlobReference("index.html");
  37. cloudBlockBlob2.Properties.ContentType = "text/html";
  38. await cloudBlockBlob2.UploadFromStreamAsync(System.IO.File.Open(@"C:\Users\XuPeiYao\Pictures\index.html", System.IO.FileMode.Open));
  39.  
  40.  
  41. Console.WriteLine(cloudBlockBlob2.Uri);
  42. }
  43. }
  44. }
  45. }
Add Comment
Please, Sign In to add comment