Search code examples
c#xamarin.formsazure-storage

Azure Storage upload Bug


I do not know if this is a bug or not, but I have made a Xamarin app, that will connect to Azure storage to upload a file.

It doesn't want to upload nd I get this error

Azure service, to upload the file

I made the same application, using a console app (for testing fester)

    var path = Path.Combine(projectPath, "universal.txt");

var fullpath = Path.GetFullPath(path);

var FileName = Path.GetFileName(fullpath);

using (StreamWriter sw = new StreamWriter(fullpath)) {

    sw.WriteLine("Hello I want to  go to Universal tomorrow");
}


var AzureStorage = new BlobContainerClient(ConectionString, ContanerName);

var blob = AzureStorage.GetBlobClient(FileName);
await blob.UploadAsync(fullpath);

My file get uploaded to Azure

File in storage


Solution

  • Use these functions to upload a file from xamarin.

    static CloudBlobContainer GetContainer(ContainerType containerType)
    {
      var account = CloudStorageAccount.Parse(Constants.StorageConnection);
      var client = account.CreateCloudBlobClient();
      return client.GetContainerReference(containerType.ToString().ToLower());
    }
    
    public static async Task<string> UploadFileAsync(ContainerType containerType, Stream stream)
    {
      var container = GetContainer(containerType);
      await container.CreateIfNotExistsAsync();
    
      var name = Guid.NewGuid().ToString();
      var fileBlob = container.GetBlockBlobReference(name);
      await fileBlob.UploadFromStreamAsync(stream);
    
      return name;
    }
    

    OR

    
    client = new BlobServiceClient(storageConnectionString);
    containerClient = await client.CreateBlobContainerAsync(containerName);
    
    blobClient = containerClient.GetBlobClient(fileName);
    await containerClient.UploadBlobAsync(fileName, memoryStreamFile);