using azure storage for mobile
TRANSCRIPT
![Page 1: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/1.jpg)
Azure Mobile Storage
Azure Storage in the Mobile World@glenntstephens
![Page 2: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/2.jpg)
Welcome everyoneWhat will we cover?
![Page 3: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/3.jpg)
In case we’ve not met...
I train developers To be Xamarin certified mobile
developers for Xamarin University
I develop appsABBA – Always Be Building
Apps
I play musicIn case you want to discuss
anything else. Politics, Comedy and Jackie Chan movies are good too
![Page 4: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/4.jpg)
The need for Storage
Server Rule #1
![Page 5: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/5.jpg)
From little things...
ASP.NET Server
File Storage
Databases
![Page 6: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/6.jpg)
The two ways we’ll look atBlob Storage is what we’ll be looking for
1. Calling using WebApi2. Calling directly from a mobile app
![Page 7: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/7.jpg)
Looking at the APIBlobContainer storage
![Page 8: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/8.jpg)
Azure Storage API Housekeeping• Before we access it via the API:• Ensure the Azure Account is setup• Ensure we have a container setup
• Like many Azure services, the Portal has great documentation• Have a look at the services you can connect to
![Page 9: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/9.jpg)
Common Scenarios• Here is our mobile app day-to-day:• Upload a file to the server• Download the file from the server (if authorisation is
required)• Get the public URL for a file (if its public)• Delete a file• List the files in the container
![Page 10: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/10.jpg)
Encapsulate all the things• You will need to connect
in. • I find its easy to make a
façade to manage storage for Access• CloudHop
![Page 11: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/11.jpg)
Common Scenario: Connecting• We need to connect to the container 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; }
![Page 12: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/12.jpg)
Getting the Blob container (1/2)• Its access to the blob container that lets you perform
all the operations you need:
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);
![Page 13: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/13.jpg)
Getting the Blob container (2/2)• Its access to the blob container that lets you perform
all the operations you need: if (blobContainer.CreateIfNotExists()) { blobContainer.SetPermissions(new BlobContainerPermissions() { PublicAccess = isPublic ? BlobContainerPublicAccessType.Blob : BlobContainerPublicAccessType.Off }); }
container = blobContainer; return blobContainer;}
![Page 14: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/14.jpg)
Common Scenario: Uploading• Uploading a single file to the container
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
![Page 15: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/15.jpg)
Common Scenario: Downloading• Two options here• For public collections, provide the Url• For private collections use the API
![Page 16: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/16.jpg)
Downloading to a stream• For when you want to manage the resources a little
more elegantlypublic 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);}
![Page 17: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/17.jpg)
Common Scenario: The public Url• Gather the Url from the source• Use HttpClient or other mechanisms to download the
resource
public string UriForFile(string filePath){ return container.ServiceClient.BaseUri.AbsoluteUri + containerName + "/"+ filePath;}
![Page 18: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/18.jpg)
Common Scenario: Listing the files• Need to find what is there• I tend to use Guids as a way of keeping the files
uniquepublic List<string> ListAllBlobs(){ return GetBlobContainer() .ListBlobs() .Select(item => item.Uri.ToString()) .ToList();}
![Page 19: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/19.jpg)
Common Scenario: Deleting a file• When you need to make sure the file is no longer there
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();}
![Page 20: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/20.jpg)
Exposing it via Web Api[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();}
![Page 21: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/21.jpg)
Connecting from a mobile app• Easiest way is to use HttpClient to connect to the URL
to access the file contained in the Blob• If it is an image, you could use other mechanisms such
as Xamarin.Forms Image and ImageSource classes
![Page 22: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/22.jpg)
DemonstrationLets run some code
![Page 23: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/23.jpg)
Azure Mobile ServicesBlobContainer direct from the device
![Page 24: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/24.jpg)
The Mobile App• Moments - Like a Snapchat application.• Credit goes to Pierce Boggan from the Xamarin team
for building the application.
![Page 25: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/25.jpg)
What is Moments?• A snapchat clone built on
Xamarin.Forms• https://github.com/pierceboggan/Moments
• Connects to Azure for several services• Azure Mobile Services• Azure Storage
• Currently on the Android Play Store• Will be on the iOS Store at
some point Follow @pierceboggan on Twitter for updates on the app news
![Page 26: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/26.jpg)
Moments DemoLets run it and see...
![Page 27: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/27.jpg)
The Project Setup• Xamarin.Forms project• Services for the app indicate data access or implemented
capabilities such as Camera access• FriendService, AccountService for AMS Data Access• MobileServiceClientFactory for mobile access• MomentService for uploading an image and storing the moment on
the service• Custom renderers for iOS and Android for Camera Access• Shared Project setup
• Better approach for Azure and Xamarin.Forms• This configuration is not ideal for XAML though
![Page 28: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/28.jpg)
A look at the code• Core Access:• Using Identity Service to identify users• Using AMS to store data
• Services:• Moment Service
• SendMoment• DestroyMoment• GetMoments
![Page 29: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/29.jpg)
Code AnalysisLets see some code
![Page 30: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/30.jpg)
Review• Most mobile apps need some level of server capability.• Especially connected apps
• Storage is an essential service that needs to be managed• Azure Storage gives you that capability to easily run it
regardless of the way you need to execute it• Using it in any configuration can be easily done using
Azure• Mobile Apps• WebApi or Web Apps
![Page 31: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/31.jpg)
Resources• http://azure.microsoft.com/en-us/documentation/services/storage/• http://azure.microsoft.com/en-us/documentation/videos/azure-storage-5-minute-over
view/
• http://manage.windowsazure.com• http://developer.xamarin.com/guides/cross-platform/azure/• https://github.com/pierceboggan/Moments
![Page 32: Using Azure Storage for Mobile](https://reader036.vdocument.in/reader036/viewer/2022062523/58e984741a28aba6498b5925/html5/thumbnails/32.jpg)
ThanksKeep in touch via twitter @glenntstephens or email: [email protected]