SlideShare a Scribd company logo
1 of 32
ASP.NET Server
File Storage
Databases
public class AzureStorageHelper
{
string settingName;
string containerName;
bool isPublic;
CloudBlobContainer container = null;
public AzureStorageHelper(string settingName,
string containerName, bool isPublic = true)
{
this.settingName = settingName;
this.containerName = containerName;
this.isPublic = isPublic;
}
public CloudBlobContainer GetBlobContainer()
{
if (container != null)
return container;
var connStr = GetConnectionString();
var storageAccount = CloudStorageAccount.Parse(connStr);
var blobClient = storageAccount.CreateCloudBlobClient();
var blobContainer =
blobClient.GetContainerReference(this.containerName);
if (blobContainer.CreateIfNotExists())
{
blobContainer.SetPermissions(new BlobContainerPermissions()
{
PublicAccess = isPublic ?
BlobContainerPublicAccessType.Blob :
BlobContainerPublicAccessType.Off
});
}
container = blobContainer;
return blobContainer;
}
public string UploadBlob(string filename, Stream stream)
{
if (stream.Length == 0)
throw new ApplicationException("There was no content for the file");
var reference = GetBlobContainer()
.GetBlockBlobReference(filename);
reference.UploadFromStream(stream);
return reference.Uri.AbsoluteUri;
}
Note:The non-async mechanisms are used for this demo for simplicity. Use the async version
for real power. Learn async if you haven’t used it before
public void DownloadToStream(string name, Stream stream)
{
Uri ur = new Uri(name);
string fname = Path.GetFileName(ur.LocalPath);
var foundFile = GetBlobContainer().GetBlockBlobReference(fname);
foundFile.DownloadToStream(stream);
}
public string UriForFile(string filePath)
{
return container.ServiceClient.BaseUri.AbsoluteUri +
containerName + "/"+ filePath;
}
public List<string> ListAllBlobs()
{
return GetBlobContainer()
.ListBlobs()
.Select(item => item.Uri.ToString())
.ToList();
}
public void DeleteBlob(string name)
{
Uri ur = new Uri(name);
string fname = Path.GetFileName(ur.LocalPath);
var foundFile = GetBlobContainer().GetBlockBlobReference(fname);
if (foundFile != null)
foundFile.Delete();
}
[HttpPost]
[Route("api/ImageService/UploadNewImage")]
public async Task<IHttpActionResult> DirectImageUpload()
{
if (!Request.Content.IsMimeMultipartContent())
throw new
HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable,
"This request is not properly formatted"));
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
var file = provider.Contents.First();
var filename = file.Headers.ContentDisposition.FileName.Trim('"');
var stream = await file.ReadAsStreamAsync();
GetAzureStorageConnection().UploadBlob(filename, stream);
return Ok();
}
• https://github.com/pierceboggan/Moments
Follow @pierceboggan onTwitter
for updates on the app news
• http://azure.microsoft.com/en-us/documentation/services/storage/
• http://azure.microsoft.com/en-us/documentation/videos/azure-storage-5-minute-overview/
• http://manage.windowsazure.com
• http://developer.xamarin.com/guides/cross-platform/azure/
• https://github.com/pierceboggan/Moments
Using Azure Storage for Mobile

More Related Content

What's hot

LiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data fileLiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data fileLarry Nung
 
Do something in 5 with gas 3-simple invoicing app
Do something in 5 with gas 3-simple invoicing appDo something in 5 with gas 3-simple invoicing app
Do something in 5 with gas 3-simple invoicing appBruce McPherson
 
MongoDB - Aggregation Pipeline
MongoDB - Aggregation PipelineMongoDB - Aggregation Pipeline
MongoDB - Aggregation PipelineJason Terpko
 
Discussion of NGRX-Entity
Discussion of NGRX-EntityDiscussion of NGRX-Entity
Discussion of NGRX-EntityNate Kidwell
 
#win8acad : Building Metro Style Apps with XAML for .NET Developers
#win8acad : Building Metro Style Apps with XAML for .NET Developers#win8acad : Building Metro Style Apps with XAML for .NET Developers
#win8acad : Building Metro Style Apps with XAML for .NET DevelopersFrederik De Bruyne
 
New text document
New text documentNew text document
New text documentTam Ngo
 
실시간 인벤트 처리
실시간 인벤트 처리실시간 인벤트 처리
실시간 인벤트 처리Byeongweon Moon
 
Intoroduce milkcocoa for english
Intoroduce milkcocoa for englishIntoroduce milkcocoa for english
Intoroduce milkcocoa for englishSyuhei Hiya
 
Investigation of Transactions in Cassandra
Investigation of Transactions in CassandraInvestigation of Transactions in Cassandra
Investigation of Transactions in Cassandradatastaxjp
 
Do something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as databaseDo something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as databaseBruce McPherson
 
C# Application program UNIT III
C# Application program UNIT IIIC# Application program UNIT III
C# Application program UNIT IIIMinu Rajasekaran
 
Coding using jscript test complete
Coding using jscript test completeCoding using jscript test complete
Coding using jscript test completeViresh Doshi
 
Modern Android app library stack
Modern Android app library stackModern Android app library stack
Modern Android app library stackTomáš Kypta
 
Spring data ii
Spring data iiSpring data ii
Spring data ii명철 강
 
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheetDo something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheetBruce McPherson
 
Correcting Common Async/Await Mistakes in .NET
Correcting Common Async/Await Mistakes in .NETCorrecting Common Async/Await Mistakes in .NET
Correcting Common Async/Await Mistakes in .NETBrandon Minnick, MBA
 
SH 1 - SES 7 - Change-Streams-Tel-Aviv.pptx
SH 1 - SES 7 - Change-Streams-Tel-Aviv.pptxSH 1 - SES 7 - Change-Streams-Tel-Aviv.pptx
SH 1 - SES 7 - Change-Streams-Tel-Aviv.pptxMongoDB
 
MongoDB Aggregation
MongoDB Aggregation MongoDB Aggregation
MongoDB Aggregation Amit Ghosh
 

What's hot (20)

LiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data fileLiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data file
 
Do something in 5 with gas 3-simple invoicing app
Do something in 5 with gas 3-simple invoicing appDo something in 5 with gas 3-simple invoicing app
Do something in 5 with gas 3-simple invoicing app
 
MongoDB - Aggregation Pipeline
MongoDB - Aggregation PipelineMongoDB - Aggregation Pipeline
MongoDB - Aggregation Pipeline
 
Discussion of NGRX-Entity
Discussion of NGRX-EntityDiscussion of NGRX-Entity
Discussion of NGRX-Entity
 
#win8acad : Building Metro Style Apps with XAML for .NET Developers
#win8acad : Building Metro Style Apps with XAML for .NET Developers#win8acad : Building Metro Style Apps with XAML for .NET Developers
#win8acad : Building Metro Style Apps with XAML for .NET Developers
 
New text document
New text documentNew text document
New text document
 
Ajax - a quick introduction
Ajax - a quick introductionAjax - a quick introduction
Ajax - a quick introduction
 
실시간 인벤트 처리
실시간 인벤트 처리실시간 인벤트 처리
실시간 인벤트 처리
 
Intoroduce milkcocoa for english
Intoroduce milkcocoa for englishIntoroduce milkcocoa for english
Intoroduce milkcocoa for english
 
Investigation of Transactions in Cassandra
Investigation of Transactions in CassandraInvestigation of Transactions in Cassandra
Investigation of Transactions in Cassandra
 
Do something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as databaseDo something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as database
 
C# Application program UNIT III
C# Application program UNIT IIIC# Application program UNIT III
C# Application program UNIT III
 
Coding using jscript test complete
Coding using jscript test completeCoding using jscript test complete
Coding using jscript test complete
 
Modern Android app library stack
Modern Android app library stackModern Android app library stack
Modern Android app library stack
 
Spring data ii
Spring data iiSpring data ii
Spring data ii
 
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheetDo something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet
 
Correcting Common Async/Await Mistakes in .NET
Correcting Common Async/Await Mistakes in .NETCorrecting Common Async/Await Mistakes in .NET
Correcting Common Async/Await Mistakes in .NET
 
SH 1 - SES 7 - Change-Streams-Tel-Aviv.pptx
SH 1 - SES 7 - Change-Streams-Tel-Aviv.pptxSH 1 - SES 7 - Change-Streams-Tel-Aviv.pptx
SH 1 - SES 7 - Change-Streams-Tel-Aviv.pptx
 
MongoDB Aggregation
MongoDB Aggregation MongoDB Aggregation
MongoDB Aggregation
 
Apache Spark - Aram Mkrtchyan
Apache Spark - Aram MkrtchyanApache Spark - Aram Mkrtchyan
Apache Spark - Aram Mkrtchyan
 

Similar to Using Azure Storage for Mobile

MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012
MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012
MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012Amazon Web Services
 
안드로이드 데이터 바인딩
안드로이드 데이터 바인딩안드로이드 데이터 바인딩
안드로이드 데이터 바인딩GDG Korea
 
Spring 4 final xtr_presentation
Spring 4 final xtr_presentationSpring 4 final xtr_presentation
Spring 4 final xtr_presentationsourabh aggarwal
 
Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...go_oh
 
Book integrated assignment
Book integrated assignmentBook integrated assignment
Book integrated assignmentAkash gupta
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android InfrastructureAlexey Buzdin
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android InfrastructureC.T.Co
 
Better Software: introduction to good code
Better Software: introduction to good codeBetter Software: introduction to good code
Better Software: introduction to good codeGiordano Scalzo
 
Data Binding Intro (Windows 8)
Data Binding Intro (Windows 8)Data Binding Intro (Windows 8)
Data Binding Intro (Windows 8)Gilbok Lee
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency GotchasAlex Miller
 
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップUnity Technologies Japan K.K.
 
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップUnite2017Tokyo
 

Similar to Using Azure Storage for Mobile (20)

MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012
MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012
MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012
 
Android Data Persistence
Android Data PersistenceAndroid Data Persistence
Android Data Persistence
 
안드로이드 데이터 바인딩
안드로이드 데이터 바인딩안드로이드 데이터 바인딩
안드로이드 데이터 바인딩
 
F3
F3F3
F3
 
Spring 4 final xtr_presentation
Spring 4 final xtr_presentationSpring 4 final xtr_presentation
Spring 4 final xtr_presentation
 
Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...
 
Ejb examples
Ejb examplesEjb examples
Ejb examples
 
Book integrated assignment
Book integrated assignmentBook integrated assignment
Book integrated assignment
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
Better Software: introduction to good code
Better Software: introduction to good codeBetter Software: introduction to good code
Better Software: introduction to good code
 
Data Binding Intro (Windows 8)
Data Binding Intro (Windows 8)Data Binding Intro (Windows 8)
Data Binding Intro (Windows 8)
 
hibernate with JPA
hibernate with JPAhibernate with JPA
hibernate with JPA
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
Apache Beam de A à Z
 Apache Beam de A à Z Apache Beam de A à Z
Apache Beam de A à Z
 
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
 
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
 
Spring data
Spring dataSpring data
Spring data
 
Caching a page
Caching a pageCaching a page
Caching a page
 
Unit - IV.pptx
Unit - IV.pptxUnit - IV.pptx
Unit - IV.pptx
 

Using Azure Storage for Mobile

Editor's Notes

  1. Hi Everyone, welcome to everyone that’s shown up. I’m here today to discuss the storage for a mobile app. My day to day is working with mobile applications, but to really create a great experience for mobile you often need to have some server capabilities and that might mean talking to app using Azure Mobile Services or it may mean using a hosted ASP.NET server where you connect into the infrastructure. We’re going to look at a couple of the options for this. I’m going to show you a mobile app running on Android using Xamarin.Forms that is connecting to Azure storage for its app and we’ll step through the code to see how its linking up. We’ll also look at an ASP.NET application that is connecting to Azure Storage. In this scenario you may have some fine control and you are exposing this connectivity and control through ASP.NET MVC Web Api which your mobile applications connect to via rest.
  2. Why is storage so important. Think about the types of servers that you would be running. You need to store files for images, for documents, for audio.In fact any file that might be unique for a server you will need to store it. The problem is that your servers normally have a finite storage capacity by default, that is if you are running Virtual Machines or similar. This is where one of my rules come into effect
  3. Rule #1 is simple. The server should not use any resources on your machine so that the server will reach the limits of its capacity. You should look to offload those kinds of mechanisms to connected servers. This includes databases as well as storage. Databases have been discussed to no-end so we will look at the Storage capabilities This directly affects scalability and the sooner you can separate it, the sooner you can manage the scalability of your services.
  4. W e’ll take a look at connecting to storage first via the mobile app and then via a web api service. Lets start with the mobile app service
  5. Regardless of what you kind of system you are making, these tend to be the common scenarios that you will be running through. You need to make sure you have a container which means you need to set one up in Azure You need to get the details of the connection string for the Storage system as well as the container name. We will then use these systems to connect in
  6. I’m a big fan of having a simple API that is testable using my scenarios, so I tend to: Create a façade around the object Unit test the façade to make sure it works the way I expect Then I have a level of comfort about how it will work when I’ve deployed it.