Add Cloud Storage to Xamarin Apps with Azure Blob Storage

BrandonMinnickBrandonMinnick USXamarin Team Xamurai
Azure Blob Storage makes it extremely easy for Xamarin developers to implement Cloud Storage code in your cross-platform apps, allowing us to share 100% code across iOS, Android, and Windows.

https://blog.xamarin.com/xamarin-plus-azure-blob-cloud-storage/

Posts

  • AlanDayAlanDay USMember

    Hi Brandon,
    Appreciate the sample. Is there any chance the Readme could be a little bit more detailed. The Blog doesn't seem to align to the code. e.g.: I am struggling to find where I insert my Connection String.
    Thanks.

  • BrandonMinnickBrandonMinnick USXamarin Team Xamurai

    Hi @AlanDay!

    Sure! Which step in the Readme is causing the trouble?

  • AlanDayAlanDay USMember

    more so with the Azure function set up and which file to place my connection string.. Thanks,

  • BrandonMinnickBrandonMinnick USXamarin Team Xamurai

    The Connection String goes in the Application Settings under "Connection Strings".

    Check out this screenshot from a different sample app: https://github.com/brminnick/XamList#6-connect-sql-database-to-the-azure-function-app

  • FarmerDevFarmerDev Member ✭✭

    Brandon, your Blog "blog.xamarin.com/xamarin-plus-azure-blob-cloud-storage/" looks nice but leaving things like the using statements out (and maybe other things) makes it a little harder for newbies like me. Is there a GitHub for your blob example? I like the video with James and your serverless approach is great, but for me I need to build up to that, and not finding source or having complete code in the blog listing is a bit of a downer. If I'm missing something please point it out to me! Thanks.

  • BrandonMinnickBrandonMinnick USXamarin Team Xamurai

    @FarmerDev Great Idea!

    Here's the complete code: https://github.com/brminnick/XamarinBlobStorageApp

    I'll add this link to the blog post too!

  • FarmerDevFarmerDev Member ✭✭

    Wow Brandon thank you! Since I have your attention...would you happen to have some cod that uses the SavePhoto method? I am having a hard time finding a good example of turning an imagesource into a byte array. I see where image.source is not really an imagesource and somewhere I came across where a stream was turned into a memorystream and I then possibly a byte array??? Thank you in advance for any direction you can point me! Dan

  • BrandonMinnickBrandonMinnick USXamarin Team Xamurai

    No problem! Here's the code to turn a Stream into a byte[]

    public static byte[] ConvertStreamToByteArrary(Stream stream)
    {
        using (var memoryStream = new MemoryStream())
        {
            stream.CopyTo(memoryStream);
            return memoryStream.ToArray();
        }
    }
    
  • PandukaWeerasekaraPandukaWeerasekara USMember ✭✭

    Hi Brandon its very useful video and tutorial, really appreciate your work, by the way i have few concerns , since i am quit new to xamarin, how do i have to setup the sql database to sync with the blob-storage because i have created blob storage already and its up and running , even i can take photos from phone and it instantly update to my blob-storage but not text, so if i use your sample project can you tell me how do you sync the blob-storage with the sql database and how does the function takes place updating the database details, once again thank you and i will be waiting for your favorable response .

  • BrandonMinnickBrandonMinnick USXamarin Team Xamurai

    Hi @PandukaWeerasekara! Is your question "How to create a SQL Database in Azure"?

  • PandukaWeerasekaraPandukaWeerasekara USMember ✭✭

    Hi @Brandon , thank you for the reply, actually i want to find out what is the function you created and the code related to that because i search all over , unfortunately i didn't come across even example similar for the last few days, also i have created few functions but non of those functions insert the blob storage details ( image and text fields) to the azure SQL . so 1) i just really want to know what is the function( or trigger) type and the code you used to enter the blob storage data to AzureSQL database
    2) and what is the function you used to retrieve the data back ( from Azure SQL to the xamarin app)to the app and the code you used
    Thanks.

  • BrandonMinnickBrandonMinnick USXamarin Team Xamurai

    @PandukaWeerasekara The code for the Functions are included in the AzureBlobStorageSampleApp solution: https://github.com/brminnick/AzureBlobStorageSampleApp/tree/master/AzureBlobStorageSampleApp.Functions

    Here's how to publish the Functions from Visual Studio: https://tutorials.visualstudio.com/first-azure-function/publish

  • PandukaWeerasekaraPandukaWeerasekara USMember ✭✭

    Hi Brandon Thanks for the reply, i understand the function code and i went through earlier when i download the github repo, the thing i have created the funtion on the cloud , like this ( my table rows and values are shown below in this function)

    #r "System.Configuration"
    #r "System.Data"
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Threading.Tasks;
    using System;
    
    public static void Run(byte[]myBlob, string name, TraceWriter log)
    {
        log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
        string detail = ($"{name}");
    
    
        var str = ConfigurationManager.ConnectionStrings["sqldb_connection"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(str))
        {
            conn.Open();
            var text = "INSERT INTO PhotoTable(ID,CreatedAt,UpdatedAt,IsDeleted, Url, Title) " +
               "VALUES (555,SYSDATETIMEOFFSET(),SYSDATETIMEOFFSET(), 'true', 'yrhrh', @Name)";
    
            using (SqlCommand cmd = new SqlCommand(text, conn))
            {
                cmd.Parameters.AddWithValue("@Name", name);
                // Execute the command and log the # rows affected.
                var rows = cmd.ExecuteNonQueryAsync();
                log.Info($"{rows} rows were updated");
            }
        }
    }
    

    but this code doesn't do any thing i mean it doesn't insert data to the database , so having bit trouble of inserting to the database first,( if i can insert only i can retrieve data right??

  • BrandonMinnickBrandonMinnick USXamarin Team Xamurai

    @PandukaWeerasekara
    Do two things:
    1. Publish the functions project to Azure from Visual Studio
    2. Add the database connection string to the Functions Application Settings

    Publish the Azure Functions code to Azure from Visual Studio:
    1. Open the AzureBlobStorageSampleApp solution in Visual Studio on the PC
    2. Right-click on the AzureBlobStorageSampleApp.Functions project
    3. Select Publish to Azure
    4. Follow the prompts in Visual Studio

    Add the Database Connection String to the Azure Function:
    1. In the Azure portal, navigate to the Function App
    2. Select Application Settings
    3. In the Application Settings, scroll down to "Connection Strings"
    4. Create a new connection string
    5. Set the name as PhotoDatabaseConnectionString
    - Make sure to use this exact name, otherwise the source code will not work
    6. From the drop-down menu, Select SQLDatabase
    7. Click Save

  • PandukaWeerasekaraPandukaWeerasekara USMember ✭✭

    @Brandon, Thank you so much for your quick reply, i will do the instructions as you mention above and send you the feedback once i done it, Thank you so much for your help

  • PandukaWeerasekaraPandukaWeerasekara USMember ✭✭

    @Brandon when i create the function this massage thrown , but i have two storage accounts for the Images

    Template deployment failed. Deployment operation statuses:
    Failed: /subscriptions/69ce6e8b-87af-4089-864b-943902a61d2c/resourceGroups/AcountsGroup/providers/Microsoft.Storage/storageAccounts/wpanduka ()
    error (ResourceNotFound): The Resource 'Microsoft.Storage/storageAccounts/wpanduka' under resource group 'AcountsGroup' was not found.
    Succeeded: /subscriptions/69ce6e8b-87af-4089-864b-943902a61d2c/resourceGroups/AcountsGroup/providers/Microsoft.Web/sites/FunctionApp20180615105949 ()

  • PandukaWeerasekaraPandukaWeerasekara USMember ✭✭

    I change the get and post urls as well in BackendConstants.cs( according to the function app i created according to your instructions , and i get an error like this

Sign In or Register to comment.