FREE EBook–Pengenalan Azure Web Apps

12527819_10153838977016182_2146359183_n

FREE E-Book ini berisi penjelasan dan tutorial singkat tentang penggunaan layanan komputasi awan menggunakan platform Microsoft Azure terutama Azure Web Apps dan Mobile Services. Diharapkan setelah membaca e-book ini pembaca dapat lebih memahami apa itu platform Microsoft Azure, serta dapat membuat aplikasi berbasis web yang dipasang pada layanan Microsoft Azure Web Apps.

Buku ini memiliki tebal 72 halaman dengan daftar isi sebagai berikut :

  • BAB 1 Pengenalan Azure Website / Azure App Services. 1
    • Azure Website / Azure Web App. 2
    • Azure Mobile Services. 2
  • Bab 2 – Medaftar Akun Azure dan Memasang Web CMS pada Layanan Azure Web Apps. 4
    • Bagaimana Memperoleh Akun Microsoft Azure?. 4
    • Memasang Aplikasi CMS Open Source di Azure. 5
    • Langkah 1: Membuat MySQL Database. 5
    • Langkah 2: Memasang WordPress pada layanan Azure Web. 7
  • Bab 3 – PHP & MySQL dengan Layanan Azure Web Apps. 10
    • Membuat Database MySQL di Azure. 10
    • Membuat Aplikasi Web dengan PHP. 14
  • Bab 4 – ASP.NET MVC & Microsoft Azure. 22
    • Membuat Project ASP.NET MVC. 22
    • Membuat Web Site Instance di Windows Azure. 24
    • Upload Project di Visual Studio ke Microsoft Azure. 26
    • Menggunakan SQL Azure dan ASP.NET MVC. 29
    • Membuat Database pada Windows Azure. 31
    • Autentikasi Aplikasi ASP.NET MVC dengan Azure Active Directory. 34
    • Membuat Azure Active Directory. 34
  • Bab 5 – Windows Azure Mobile Services. 41
    • Memulai Membuat Azure Mobile Services. 41
    • Windows Store Client. 45
    • Mengakses REST Services dari Table di Mobile Services. 50
    • Menambahkan Data dan Table Baru pada Azure Mobile Services. 53
    • Menambahkan Script Pada Proses CRUD.. 58
    • Menggunakan Objek Request pada Script. 60
    • Menambahkan Field baru dengan Script. 62
    • Menggunakan Query Object pada SCRIPT. 64
  • Bab 6 -Identity dengan Azure Mobile Services. 66
    • Membuat Twitter Identity Provider. 66
    • Membuat Facebook Identity Provider. 69
    • Menambahkan Restriction Permission pada Table. 70
  • Bab 7 – Menggunakan Push Notification. 72

Anda dapat mendownload e-book ini pada tautan berikut: http://tinyurl.com/bukuazurewebapp

Azure Table Storage dengan ASP.NET MVC (Part 2)

Pada bagian 1 kita sudah membahas apa itu Azure Table Storage dan bagaimana cara membuat Storage Account pada layanan Azure. Selanjutnya kita akan membuat ASP.NET MVC Project yang dapat terhubung dengan layanan Storage Account yang sudah kita buat, kemudian menambahkan data pada Storage Account berupa Azure Table dan Azure Blob. Azure Table digunakan untuk menyimpan data standard yang bertipe string/number (selain blob). Azure Blob digunakan untuk menyimpan data blob seperti file image, video, pdf, dan format lain yang biasanya mempunyai ukuran relatif besar.

Membuat Project ASP.NET MVC

Buka Visual Studio 2015 kemudian buat ASP.NET MVC Project baru dengan nama  SampleTableStorage. Kemudian pada folder model tambahkan interface baru dengan nama ITableOperations.cs. Method pada interface ini akan diimplementasikan pada method selanjutnya.

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System.Collections.Generic;
using System.Configuration;
using System;

namespace SampleTableStorage.Models
{
    public interface ITableOperations
    {
        void CreateEntity(StorageEntity entity);
        List<StorageEntity> GetEntities(string filter);
        StorageEntity GetEntity(string partitionKey,string rowKey);
    } 
}

Masih pada folder Model, tambahkan class StorageEntity.cs. Class ini digunakan untuk mendefinisikan data model yang akan dibuat pada Azure Table. Class ini berisi definisi dari field-field yang akan dibuat pada Azure Table.

using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace SampleTableStorage.Models
{
    public class StorageEntity : TableEntity
    {
        public StorageEntity()
        { }

        public StorageEntity(int storageId,string city)
        {
            this.RowKey = storageId.ToString();
            this.PartitionKey = city;
        }

        public string StorageId { get; set; }
        public string Size { get; set; }
        public string Environment { get; set; }
        public decimal PriceDay { get; set; }
        public decimal PriceMonth { get; set; }
        public string Space { get; set; }
        public string Access { get; set; }
        public string Description { get; set; }

        [Required(ErrorMessage = "Title Required !")]
        public string Title { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
        public string Picture { get; set; }
        public double Length { get; set; }
        public double Height { get; set; }
        public string Phone { get; set; }

    }
}

Setelah membuat TableEntity, maka langkah selanjutnya adalah menambahkan operasi-operasi yang akan dilakukan di Table Storage seperti membuat dan mengambil data pada Azure Table. Pada folder Model, tambahkan class dengan nama TableOperations.cs.

Untuk bekerja dengan Storage Account yang ada di Azure anda harus menambahkan string koneksi pada project anda. Anda dapat mengambil string koneksi tersebut pada portal Azure.

image

kemudian tambahkan string koneksi tersebut pada file konfirgurasi di web.config

<appSettings>
  <add key="webpages:Version" value="3.0.0.0" />
  <add key="webpages:Enabled" value="false" />
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  <add key="keepStorage" value="DefaultEndpointsProtocol=https;AccountName=keepstorage;AccountKey=<key anda>;BlobEndpoint=https://keepstorage.blob.core.windows.net/;TableEndpoint=https://keepstorage.table.core.windows.net/;QueueEndpoint=https://keepstorage.queue.core.windows.net/;FileEndpoint=https://keepstorage.file.core.windows.net/" />
</appSettings>

Setelah string koneksi ditambahkan, anda dapat menggunakan string koneksi tersebut pada aplikasi anda untuk terhubung dengan Storage Account yang ada pada layanan Azure.

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System.Collections.Generic;
using System.Configuration;
using System;
using System.Linq;

namespace SampleTableStorage.Models
{
    public class TableOperations : ITableOperations
    {
        CloudStorageAccount storageAccount;
        CloudTableClient tableClient;

        public TableOperations()
        {
            storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["keepStorage"].ToString());
            tableClient = storageAccount.CreateCloudTableClient();

            CloudTable table = tableClient.GetTableReference("keepstorage");
            table.CreateIfNotExists();
        }
        public void CreateEntity(StorageEntity entity)
        {
            CloudTable table = tableClient.GetTableReference("keepstorage");
            TableOperation insertOperation = TableOperation.Insert(entity);
            table.Execute(insertOperation);
        }

        public List<StorageEntity> GetEntities(string filter)
        {
            //List<StorageEntity> storages = new List<StorageEntity>();
            CloudTable table = tableClient.GetTableReference("keepstorage");

            List<StorageEntity> query = (from entity in table.CreateQuery<StorageEntity>()
                                               where entity.PartitionKey == "City"
                                               select entity).ToList();

            return query;
        }

        public StorageEntity GetEntity(string partitionKey, string rowKey)
        {
            StorageEntity entity = null;
            CloudTable table = tableClient.GetTableReference("keepstorage");
            entity = (from e in table.CreateQuery<StorageEntity>()
                      where e.PartitionKey == partitionKey && e.RowKey == rowKey
                      select e).FirstOrDefault();

            return entity;
        }
    }
}

Tambahkan juga class dengan nama BlobOperations.cs. Class ini akan digunakan untuk membuat dan menambahkan file blob kedalam objek Azure Blob pada Storage Account.

using System;
using System.Web;

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Configuration;
using System.Threading.Tasks;
using System.IO;


namespace SampleTableStorage.Models
{
    
    public class BlobOperations
    {
        private static CloudBlobContainer storageBlobContainer;

        public BlobOperations()
        {
            var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["keepStorage"].ToString());
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            storageBlobContainer = blobClient.GetContainerReference("blobkeepstorage");
            storageBlobContainer.CreateIfNotExists();
        }

        public CloudBlockBlob UploadBlob(HttpPostedFileBase storagePicFile)
        {
            string blobName = Guid.NewGuid().ToString() + Path.GetExtension(storagePicFile.FileName);

            CloudBlockBlob storageBlob = storageBlobContainer.GetBlockBlobReference(blobName);
            using (var fs = storagePicFile.InputStream)
            {
                storageBlob.UploadFromStream(fs);
            }

            return storageBlob;
        }
    }
}

 

Kemudian langkah selanjutnya adalah membuat controller  dengan nama StorageListController.cs. Pada controller ini kita akan menambahkan method yang digunakan untuk menambahkan data ke Azure Table dan Azure Blob.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using SampleTableStorage.Models;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Blob;

namespace SampleTableStorage.Controllers
{
    public class StorageListController : Controller
    {
        BlobOperations blobOperations;
        TableOperations tableOperations;
               
        public StorageListController()
        {
            blobOperations = new BlobOperations();
            tableOperations = new TableOperations();
        }
        // GET: StorageList
        public ActionResult Index()
        {
            var storages = tableOperations.GetEntities("Spokane");
            return View(storages);
        }

        public ActionResult Create()
        {
            var objStorage = new StorageEntity();
            //objStorage.StorageId = Guid.NewGuid().ToString();
            objStorage.City = "Spokane";
            ViewBag.Environment = new SelectList(new List<string>() {
                "Outdoor","Indoor"
            });
            ViewBag.Access = new SelectList(new List<string>()
            {
                "Limited","Anytime"
            });

            return View();
        }

        [HttpPost]
        public ActionResult Create(StorageEntity obj,HttpPostedFileBase Picture)
        {
            //upload file to blob
            CloudBlockBlob storageBlob = null;
            if(Picture != null && Picture.ContentLength !=0)
            {
                storageBlob = blobOperations.UploadBlob(Picture);
                obj.Picture = storageBlob.Uri.ToString();
            }

            obj.City = "Spokane";
            obj.RowKey = Guid.NewGuid().ToString();
            obj.PartitionKey = obj.City;
            tableOperations.CreateEntity(obj);

            return RedirectToAction("Index","Home");
        }

    }
}

Setelah membuat controller maka untuk membuat form input yang akan kita gunakan untuk menambahkan data ke Azure Table anda dapat menambahkan view dengan nama Create.cshtml berikut.

@model SampleTableStorage.Models.StorageEntity

@{
    ViewBag.Title = "Create";
}

@using (Html.BeginForm("Create","StorageList",FormMethod.Post, 
    new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    
    

Storage Entity


@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Size, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Size, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Size, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.Environment, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.DropDownListFor(model=>model.Environment, (IEnumerable)ViewBag.Environment,new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Environment, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.PriceDay, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.PriceDay, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.PriceDay, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.PriceMonth, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.PriceMonth, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.PriceMonth, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.Space, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Space, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Space, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.Access, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.DropDownListFor(model => model.Access, (IEnumerable)ViewBag.Access ,new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Access, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.State, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.State, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.State, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.ZipCode, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.ZipCode, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.ZipCode, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.Picture, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.ValidationMessageFor(model => model.Picture, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.Length, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Length, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Length, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.Height, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Height, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Height, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</div> </div> }
@Html.ActionLink("Back to List", "Index")

Setelah itu anda dapat mencoba menjalankan aplikasi ini dan memasukan data kedalam Azure Table dan Azure Blob

image

Setelah berhasil anda dapat melihat isi dari Azure Table dan Azure Blob yang sudah anda buat di Visual Studio 2015 Cloud Explorer.

image

image

Pilih mana DocumentDb, Azure Table Storage, atau SQL Azure?

Microsoft Azure kini memiliki tiga macam services untuk penyimpanan data yang dapat digunakan oleh developer yaitu:

  • DocumentDb
  • Azure Table
  • SQL Azure

DocumentDb adalah layanan penyimpanan data No-SQL (bukan merupakan database relasional) yang menyimpan dokumen JSON secara native dan menyedikanan kemampuan index dan berbagai fitur yang lain.  Azure Table juga merupakan opsi lain untuk  penyimpanan data No-SQL yang mempunyai fitur lebih sedikit dibandingkan dengan DocumentDb. Sedangkan Azure SQL merupakan layanan penyimpanan data relasional

Dari perspektif penyimpanan data, Azure SQL adalah satu-satunya layanan database relasional yang disediakan, ini berarti bahwa Azure SQL akan menyediakan skema dan strongly typed data store. Ini membuat Azure SQL memiliki keunggulan pada performa pencarian data pada table dengan index yang kompleks. Table Storage dan DocumentDb adalah penyimpanan bertipe No-SQL yang tidak memiliki skema yang pasti. DocumentDb menyimpan data sebagai objek JSON, dan memperbolehkan attachment pada dokumen, sedangkan Table Storage secara default menyimpan data berupa dokumen XML (walaupun anda dapat mengambil data dalam format JSON). Kelebihan menyimpan data pada format JSON adalah kita dengan mudah dapat melakukan mapping data kedalam JSON object pada aplikasi client. Dibandingkan dengan documentDb, Azure Storage memiliki kapasitas yang lebih terbatas, yaitu hanya dapat menyimpan data sebesar 500GB untuk satu database.

Jika dilihat dari fitur indexing, ketiga model penyimpanan data tesebut mendukung indexing. Azure SQL menawarkan fitur index yang paling lengkap seperti penambahan index pada banyak kolom dan fitur yang lain seperti penggunaan keyword WHERE ketika memfilter data berdasarkan kolom tertentu. DocumentDb juga memiliki fitur penambahan index yang di buat secara otomatis pada dokumen dan juga fitur untuk membuat query yang komplek sama seperti Azure SQL. Azure Table Storage hanya mendukung index pada PartitionKey dan RowKey sehingga kita harus jeli dalam menentukan properti mana yang akan dipilih, karena untuk memfilter data menggunakan kolom lain akan berdampak pada kecepatan pencarian data.

Dari sisi fitur program, Azure SQL menyediakan fasilitas pemrograman server side seperti store procedure, trigger, integrity check, view, dan function. Bahasa pemrograman yang digunakan pada Azure SQL adalah T-SQL. DocumentDb juga memiliki fitur server side programming menggunakan native javascript yang akan memudahkan developer yang sudah familiar dengan pemrograman javascript. Azure Tables tidak menawarkan pemrograman server side.

Untuk fitur integritas data dan transaksi, Azure SQL mempunyai fitur RDBMS referential integrity, dan mendukung transaksi, dengan menggunakan T-SQL, developer dapat menggunakan objek transaksi di sisi server dan menjalankan perintah commit/rollback. DocumentDb tidak memiliki strong referential integrity seperti pada Azure SQL seperti konsep foreign key, tetapi DocumentDb mendukung transaksi melalui bahasa pemrograman javascript pada sisi server. Developer juga dapat menggunakan javascript untuk melakukan pengecekan referential integrity pada sisi server. Azure Table Storage hanya mendukung transaksi dasar menggunakan operasi batch dengan beberapa limitasi.

Untuk perbandingan berdasarkan harga layanan, SQL Azure menawarkan banyak alternatif pilihan berdasarkan kebutuhan. Sebagai contoh untuk layanan paling dasar (basic) dengan tipe single database dan ukuran database sampai dengan 2GB, harga yang ditawarkan adalah mulai dari $5 per bulan. Harga akan bertambah secara signifikan berdasarkan spesifikasi server dan kapasitas layanan yang ditawarkan. Harga paling mahal untuk model elastic database dan kapasistas sampai dengan 1TB yaitu $2700 per bulan.

image 

image

Untuk layanan DocumentDb harga yang ditawarkan mulai dengan $25 per bulan. Harga ditentukan berdasarkan besar penyimpanan dan request unit.

image

Sedangkan Azure Table Storage menawarkan harga yang sangat terjangkau yaitu mulai dari $0.048 per GB per bulan untuk tipe Geographically Redundant Storage (GRS).

image 

Kesimpulan dari pembahasan diatas adalah pemilihan jenis data storage yang disediakan pada layanan Azure dapat dipilih sesuai dengan kebutuhan dari aplikasi yang akan dikembangkan oleh developer, karena masing-masing memiliki keunggulan dan kelemahan. Azure SQL memiliki fitur referential integrity yang lebih baik namun kurang dari segi kapasitas penyimpanan data karena hanya mendukung penyimpanan data sampai dengan 1TB. DocumentDb menawarkan implementasi No-SQL dan pemrograman server side dengan javascript namun memiliki harga yang lebih mahal dibandingkan dengan Azure Table Storage. Sedangkan Azure Table Storage memiliki fitur yang paling sedikit dibandingkan kedua data storage yang lain namun memiliki harga yang sangat terjangkau dan sudah mendukung geographically redundant.