Slides from a talk given at the Queensland Azure user group on using Azure Storage with mobile apps, either using the traditional WebApi approach or using the Azure libraries direct from the mobile
12. 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);
14. 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
15.
16. 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);
}
19. 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();
}
20. [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();
}
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.
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
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.
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
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
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.