Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.WindowsAzure.Storage;
- using Microsoft.WindowsAzure.Storage.Blob;
- using System;
- using System.Threading.Tasks;
- namespace AzureStorageTestConsole {
- class Program {
- static async Task Main(string[] args) {
- // 儲存體連線字串
- string connectionString = "xxxxxx";
- // 嘗試剖析連線字串產生帳號實例
- if (CloudStorageAccount.TryParse(connectionString, out CloudStorageAccount storageAccount)) {
- //取得客戶端實例
- CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
- //取得並且嘗試建立儲存容器
- var cloudBlobContainer = cloudBlobClient.GetContainerReference("test");
- await cloudBlobContainer.CreateIfNotExistsAsync();
- // Set the permissions so the blobs are public.
- // 設定儲存容器
- BlobContainerPermissions permissions = new BlobContainerPermissions {
- PublicAccess = BlobContainerPublicAccessType.Blob,
- };
- await cloudBlobContainer.SetPermissionsAsync(permissions);
- // 建立Blob區塊
- CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("images/test.jpg");
- // 設定ContentType,預設為application/octet-stream
- cloudBlockBlob.Properties.ContentType = "image/jpeg";
- // 上傳檔案
- await cloudBlockBlob.UploadFromStreamAsync(System.IO.File.Open(@"C:\Users\XuPeiYao\Pictures\老虎.jpg", System.IO.FileMode.Open));
- CloudBlockBlob cloudBlockBlob2 = cloudBlobContainer.GetBlockBlobReference("index.html");
- cloudBlockBlob2.Properties.ContentType = "text/html";
- await cloudBlockBlob2.UploadFromStreamAsync(System.IO.File.Open(@"C:\Users\XuPeiYao\Pictures\index.html", System.IO.FileMode.Open));
- Console.WriteLine(cloudBlockBlob2.Uri);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment