0
votes

I've spent the past few days experimenting with Azure blob storage to try and put together a cloud database for my UWP app.

I managed to get the functionality of uploading files to Azure storage but the file's contents get replaced with what I want to call junk, but there's a pattern to it that I'm sure indicates I'm doing something wrong.

The files I'm trying to upload are simple .txt files with lines of information separated by characters.

The contents of the .txt file should be formatted like this:

:SKU|:brand|:brand |:Train smarter and go farther with the brand, a product designed to help you reach your peak performance.|:249.95|:|:|:|:|:|:|:https://link.com|

:SKU|:brand|:brand Bluetooth Speaker |:Get up to 87 hours of great-sounding wireless music with the brand Bluetooth Speaker .|:269.00|:|:|:|:|:|:|:https://link.com|

:SKU|:brand|:brand Docking Station - USB 3.0 |:This USB 3.0 brand docking station connects  up to three additional monitors, external devices, and the Internet .|:169.99|:|:|:|:|:|:|:https://link.com|

But after being uploaded to my blob storage the contents look like this:

:SKU|:|:-//W3C//DTD XHTML 1.0 Strict//EN|://DTD XHTML 1.0 Strict//EN|:/W3C//DTD XHTML 1.0 Strict//EN|:|:|:|:|:|:|:-//W3C//DTD XHTML 1.0 Strict//EN|

:SKU|:|:-//W3C//DTD XHTML 1.0 Strict//EN|://DTD XHTML 1.0 Strict//EN|:/W3C//DTD XHTML 1.0 Strict//EN|:|:|:|:|:|:|:-//W3C//DTD XHTML 1.0 Strict//EN|

:SKU|:|:-//W3C//DTD XHTML 1.0 Strict//EN|://DTD XHTML 1.0 Strict//EN|:/W3C//DTD XHTML 1.0 Strict//EN|:|:|:|:|:|:|:-//W3C//DTD XHTML 1.0 Strict//EN|

:SKU|:|:-//W3C//DTD XHTML 1.0 Strict//EN|://DTD XHTML 1.0 Strict//EN|:/W3C//DTD XHTML 1.0 Strict//EN|:|:|:|:|:|:|:-//W3C//DTD XHTML 1.0 Strict//EN|

:SKU|:|:-//W3C//DTD XHTML 1.0 Strict//EN|://DTD XHTML 1.0 Strict//EN|:/W3C//DTD XHTML 1.0 Strict//EN|:|:|:|:|:|:|:-//W3C//DTD XHTML 1.0 Strict//EN|

:SKU|:brand|:brand 1TB Hard Drive for Laptops|:Upgrade your laptop to a  1 TB of storage with a high quality, reliable brand hard drive.|:68.85|:|:|:|:|:|:|:https://link.com|

My first blob storage experiments went about uploading the file itself directly but that resulted in this same issue so I tried a different method (stream reading/writing) thinking that one just didn't work for what I was trying to do. Right now I'm using the stream method (and will stick with unless anyone has other recommendations) but being so new to the cloud connected part of this (and UWP in general) I don't know what I could be doing wrong.

Here is the code I'm using to upload to my blob storage:

public class DatabaseHelper
{
    CloudBlockBlob blobb;

    public DatabaseHelper()
    {

        StorageCredentials credentials = new StorageCredentials("nameommited", "key ommitted");
        CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, true);
        var blobClient = storageAccount.CreateCloudBlobClient();
        var container = blobClient.GetContainerReference("testcontainer");
        container.CreateIfNotExistsAsync();

        CloudBlockBlob blob = container.GetBlockBlobReference("testfile.txt");
        blob.DeleteIfExistsAsync();
        blobb = blob;
    }

    public async void UploadFile(StorageFile fileToUpload)
    {

        var fileStream = await fileToUpload.OpenAsync(FileAccessMode.Read);
        ulong size = fileStream.Size;

        // Get a data stream from somewhere.
        Stream inputStream = fileStream.AsStream(); ;
       await blobb.UploadFromStreamAsync(inputStream);

    }
}
1

1 Answers

2
votes

Based on your DatabaseHelper, I built my app to test this issue as follows:

private async void btnUploadFile_Click(object sender, RoutedEventArgs e)
{
    var picker = new Windows.Storage.Pickers.FileOpenPicker();
    picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
    picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
    picker.FileTypeFilter.Add(".txt");

    Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
    if (file != null)
    {
        var fileHelper = new DatabaseHelper();
        await fileHelper.UploadFile(file);
    }
}

Here is my txt file for tesing, you could refer to test.txt.

RESULT

enter image description here

I noticed that you invoked async method in your DatabaseHelper constructor, I assumed that there be some risk if your container is not existed. Additionally, you could use Microsoft Azure Storage Explorer for uploading your txt file directly to azure blob, in order to isolate this issue. Also, there is a sample about operate Azure blob storage in UWP apps, you could refer to it.