Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Windows Store Apps > Windows Store and Windows Azure Storage (C#) > Windows Store Apps and Windows Azure Blob Storage (C#)



Clound SSD Virtual Server

Windows Store Apps and Windows Azure Blob Storage (C#)

Windows Store Apps and Windows Azure Blob Storage (C#) ในบทความแรกนี้จะเป็นการเขียน Windows Store Apps กับ Windows Azure Blob Storage กับการจัดเก็บข้อมูลบน Storage โดยจะยกตัวอย่างการ Upload ไฟล์รูปภาพ (Image) ไปจัดเก็บไว้บน Blob การเรียกใช้งานไฟล์บน Blob และการจัดเก็บไฟล์บน Blob ในรูปแบบต่าง ๆ (สำหรับบทความนี้จะใช้เป็นการจัดเก็บรูปภาพ แต่ทั้งนี้สามารถประยุกต์การใช้งานกับไฟล์ชนิดต่าง ๆ ได้อีกมากมาย)

Windows Store Apps and Windows Azure Blob Storage (C#)

โครงสร้างของ Blob จะแบ่งเป็นขั้นลำดับคือ Account -> Container -> Entity (ซึ่งไฟล์จะถูกจัดเก็บไว้ที่นี่)

Windows Store Apps and Windows Azure Blob Storage (C#)

ตัวอย่างการจัดเก็บไฟล์แบบ Blob มีโครงสร้างที่จัดเก็บง่ายและเรียกใช้งานง่าย ซึ่งเราสามารถสร้าง Container เพื่อแยกกลุ่มของ Blob

Windows Store Apps and Windows Azure Blob Storage (C#)

ตัวอย่างการจัดเก็บไฟล์แบบ Blob มีโครงสร้างที่จัดเก็บง่ายและเรียกใช้งานง่าย


การติดตั้ง Library API : Windows Store Apps and Windows Azure Storage


ขั้นตอนการเเขียน Windows Store Apps กับ Blob (Windows Azure Storage) ในขั้นแรกสุดเราหลังจากที่เราสร้าง Services ของ Storage บน Azure Portal Management เรียบร้อยแล้ว คือจะต้องสร้าง Container (เปรียบเหมือน Folder) ไว้สำหรับการจัดเก็บ Blob








เรียกใช้ Class ของ Azure Storage
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Auth;

สิ่งที่จะต้องเตรียมคือ Account และ API Key
DefaultEndpointsProtocol=https;AccountName=[yourAccount];AccountKey=[yourKey]

สร้าง Connection สำหรับการเชื่อมต่อ
// Create the connectionstring
String StorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=[yourAccount];AccountKey=[yourKey]";

ทำการเชื่อมต่อไปยัง Azure Storage
// Retrieve storage account from connection string.
 CloudStorageAccount storageAccount = CloudStorageAccount.Parse(StorageConnectionString);

การเรียกใช้งาน Blob Services
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();


หลังจากนั้นเราสามารถทำการสร้าง Container ตาม Code นี้

Code การสร้าง Container
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }


        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Create the connectionstring
            String StorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=[yourAccount];AccountKey=[yourKey]";

            // Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(StorageConnectionString);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container. (pictures)
            CloudBlobContainer container1 = blobClient.GetContainerReference("pictures");
            await container1.CreateIfNotExistsAsync();

            // Retrieve a reference to a container. (pictures)
            CloudBlobContainer container2 = blobClient.GetContainerReference("movies");
            await container2.CreateIfNotExistsAsync();
      
        }

    }


Windows Store Apps and Windows Azure Blob Storage (C#)

จาก Code จะได้ Container ชื่อว่า pictures และ movies

Example 1 การ Upload ไฟล์และจัดเก็บไว้บน Blob

Windows Store Apps and Windows Azure Blob Storage (C#)

ตัวอย่างไฟล์ที่อยู่บน Desktop โดยเราจะ Upload ไฟล์นี้ไปจัดเก็บไว้บน Blob

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }


        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Create the connectionstring
            String StorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=[yourAccount];AccountKey=[yourKey]";

            // Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(StorageConnectionString);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve reference to a previously created container.
            CloudBlobContainer container = blobClient.GetContainerReference("pictures");

            FileOpenPicker picker = new FileOpenPicker();
            picker.FileTypeFilter.Add(".jpg");
            picker.SuggestedStartLocation = PickerLocationId.Desktop;
            picker.ViewMode = PickerViewMode.Thumbnail;
            StorageFile file = await picker.PickSingleFileAsync();

            if (file != null)
            {
                using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                {
                    // Retrieve reference to a blob named
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(file.Name);
                    await blockBlob.UploadFromStreamAsync(fileStream);
                }
            }
      
        }

    }


Windows Store Apps and Windows Azure Blob Storage (C#)

หลังจากที่ Run โปรแกรมจะแสดง File Picker ให้เลือกเพื่อ Upload ไว้บน Blob

Windows Store Apps and Windows Azure Blob Storage (C#)

เปิดดูรายการไฟล์บน Azure Portal Management

Windows Store Apps and Windows Azure Blob Storage (C#)

จะเห็นว่ามีรายการไฟล์ที่ถูก Upload เข้าไป

Windows Store Apps and Windows Azure Blob Storage (C#)

เมื่อใช้ Azure Storage Explorer ดูในส่วนของไฟล์ ก็จะถูก Upload ขึ้นไป เลือกดูผ่านโปรแกรม Azure Storage Explorer เลือก View

Windows Store Apps and Windows Azure Blob Storage (C#)

กรณีที่เป็น Image ก็จะแสดงรูปภาพใน Tags ของ Image

Windows Store Apps and Windows Azure Blob Storage (C#)

เป็น URL ที่อยู่ของ Blob ซึ่งตอนที่เรียกใช้ไฟล์รูปภาพ เราสามารถเรียกใช้ผ่าน URL นี้

Windows Store Apps and Windows Azure Blob Storage (C#)

ทดสอบการ Upload ไฟล์อื่น ๆ ซึ่งเมื่อรายการไฟล์มีมากกว่า 1 ไฟล์ก็จะแสดงรายการดังภาพ








Example 2 การแสดง (List) รายการไฟล์ที่จัดเก็บไว้บน Blob

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }


        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Create the connectionstring
            String StorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=[yourAccount];AccountKey=[yourKey]";

            // Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(StorageConnectionString);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve reference to a previously created container.
            CloudBlobContainer container = blobClient.GetContainerReference("pictures");

            // Variable for parameters.
            BlobContinuationToken continuationToken = null;
            string prefix = null;
            bool useFlatBlobListing = true;
            BlobListingDetails blobListingDetails = BlobListingDetails.All;
            int maxBlobsPerRequest = 10;
            List<IListBlobItem> blobs = new List<IListBlobItem>();

            // Get list item blob.
            var listingResult = await container.ListBlobsSegmentedAsync(prefix, useFlatBlobListing, blobListingDetails, maxBlobsPerRequest, continuationToken, null, null);
            continuationToken = listingResult.ContinuationToken;
            blobs.AddRange(listingResult.Results);

            // Display blob item
            foreach (IListBlobItem item in blobs)
            {
                if (item.GetType() == typeof(CloudBlockBlob))
                {
                    CloudBlockBlob blob = (CloudBlockBlob)item;
                    // blob.Properties.Length + " - " + blob.Uri
                    this.lblResult.Text = this.lblResult.Text + blob.Properties.Length + " - " + blob.Uri + Environment.NewLine;
                }
            }
      
        }

    }


Windows Store Apps and Windows Azure Blob Storage (C#)

แสดงรายการไฟล์ที่อยู่บน Blob พร้อมทั้ง URL ที่สามารถเรียกใช้งานได้


Example 3 การลบไฟล์ (Delete) ที่อยู่บน Blob

Windows Store Apps and Windows Azure Blob Storage (C#)

เป็นไฟล์ที่อยู่บน Blob

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }


        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Create the connectionstring
            String StorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=[yourAccount];AccountKey=[yourKey]";

            // Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(StorageConnectionString);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve reference to a previously created container.
            CloudBlobContainer container = blobClient.GetContainerReference("pictures");

            // Retrieve reference to a blob named
            CloudBlockBlob blockBlob = container.GetBlockBlobReference("deawx.jpg");

            // Delete the blob.
            await blockBlob.DeleteAsync();
      
        }

    }


Windows Store Apps and Windows Azure Blob Storage (C#)

หลังจากที่ไฟล์ถูกลบแล้ว รายการบน Blob จะถูกลบออกไปด้วย


.

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2014-07-21 12:32:47 / 2017-03-19 14:27:44
  Download : No files
 Sponsored Links / Related

 
Windows Store Apps and Windows Azure Storage
Rating :

 
Windows Store Apps and Windows Azure Table Storage (C#)
Rating :

 
Windows Store Apps and Windows Azure Queue Storage (C#)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่