How to upgrade some commands from Azure SDK 11 to SDK 12 in Xamarin Forms

WillAutioItrax 201 Reputation points
2022-01-18T04:18:45.477+00:00

I've been tasked with switching out Azure SDK 11 and using 12. Below I have included a section of code that uses SDK11 commands followed by the same code that is partially converted to using SDK 12. There are a number of methods used in SDK 11 that I can't find equivalent commands in SDK 12.

Thanks for looking at this.

This is what was given to me in SDK11:
Azure SDK 11
// 1 doing some initial set up
CloudStorageAccount _cloudStorageAccount = CloudStorageAccount.Parse(utilBG.VideoStorage);
CloudBlobClient _blobClient = _cloudStorageAccount.CreateCloudBlobClient();

        bool blobExists = await _blobClient.GetContainerReference(containerName)
             .GetBlockBlobReference(blobTitle)
             .ExistsAsync().ConfigureAwait(false);

        // 2 changing the name of the blob if it exists already
        if (blobExists == true)
        {
            // change blobTitle - find the last "."; insert a "1"
            if (blobTitle.Length > 4)
            {
                int idx = blobTitle.LastIndexOf(".");
                string firstpart = blobTitle.Substring(0, idx);
                string secondpart = blobTitle.Substring(idx);
                blobTitle = firstpart + "1" + secondpart;
                Debug.WriteLine("new blobTitle:" + blobTitle);
            }
        }

        // 3 create the container if it doesn't exist already
        var blobContainer = _blobClient.GetContainerReference(containerName);
        await blobContainer.CreateIfNotExistsAsync().ConfigureAwait(false);

        // 4 setting up BlobRequestOptions
        CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobTitle);
        blockBlob.StreamWriteSizeInBytes = 256 * 1024; //256 k

        TimeSpan backOffPeriod = TimeSpan.FromSeconds(2);
        int retryCount = 3;

        var blobRequestOptions = new BlobRequestOptions()
        {
            SingleBlobUploadThresholdInBytes = 2 * 1024 * 1024, //maximum for 64MB,32MB by default     
            ParallelOperationThreadCount = 4,
            RetryPolicy = new ExponentialRetry(backOffPeriod, retryCount)
        };

        OperationContext operationContext = new OperationContext();
        CancellationToken cancellationToken = new CancellationToken();

        // 5 determining and setting ContentType
        // extract the extension to determine the content type
        string extension = Path.GetExtension(path).Substring(1);
        if (extension == "mp4")
        {
            blockBlob.Properties.ContentType = "video/mp4";
        }
        else if (extension == "png")
        {
            blockBlob.Properties.ContentType = "image/png";
        }

        // 6
        await blockBlob.UploadFromFileAsync(path,
            AccessCondition.GenerateIfNotExistsCondition(), blobRequestOptions, operationContext, cancellationToken)
            .ConfigureAwait(false);

And this is the partially converted code with additional questions about things that I can not find the corresponding method in SDK 12.

    Azure SDK 12
        // 1 doing some initial set up
        string storageConnectionString = utilBG.VideoStorage;
        BlobServiceClient blobServiceClient = new BlobServiceClient(storageConnectionString);

        BlobContainerClient container = blobServiceClient.GetBlobContainerClient(containerName);


        // 2 changing the name of the blob if it exists already
        if (container.Exists())
        {
            // change blobTitle - find the last "."; insert a "1"
            if (blobTitle.Length > 4)
            {
                int idx = blobTitle.LastIndexOf(".");
                string firstpart = blobTitle.Substring(0, idx);
                string secondpart = blobTitle.Substring(idx);
                blobTitle = firstpart + "1" + secondpart;
                Debug.WriteLine("new blobTitle:" + blobTitle);
            }
        }

        // 3 create the container if it doesn't exist already
        BlobClient blobClient = container.GetBlobClient(blobTitle);
        await container.CreateIfNotExistsAsync();

        // 4 setting up BlobRequestOptions Note: BlobRequestOptions is not in SDK 12
        //   so how can we do this section? Various methods and objects do not exist like: 
        //   StreamWriteSizeInBytes, ExponentialRetry, OperationContext
        // what can we used instead?

        blobClient.StreamWriteSizeInBytes = 256 * 1024; //256 k

        TimeSpan backOffPeriod = TimeSpan.FromSeconds(2);
        int retryCount = 3;

        var blobRequestOptions = new BlobRequestOptions()
        {
            SingleBlobUploadThresholdInBytes = 2 * 1024 * 1024, //maximum for 64MB,32MB by default     
            ParallelOperationThreadCount = 4,
            RetryPolicy = new ExponentialRetry(backOffPeriod, retryCount)
        };

        OperationContext operationContext = new OperationContext();
        CancellationToken cancellationToken = new CancellationToken();

        // 5 determining and setting ContentType
        // Don't know how to convert the SDK11 code to SDK12
        // Is there something similar to ContentType that we can/need to set?

        string extension = Path.GetExtension(path).Substring(1);
        if (extension == "mp4")
        {
            blockBlob.Properties.ContentType = "video/mp4";
        }
        else if (extension == "png")
        {
            blockBlob.Properties.ContentType = "image/png";
        }

        // 6 upload a blob from a file
        // What is the correct code to use here?
        await blobClient.UploadAsync(path, true);
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,301 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,486 questions
{count} votes

1 answer

Sort by: Most helpful
  1. WillAutioItrax 201 Reputation points
    2022-01-24T21:54:57.997+00:00

    @Rob Caplan - MSFT Thanks for your reply and suggestions for further reading.

    I was able to find a way to set the ContentType:

                    // 5 Setting ContentType  
                    // Get the existing properties  
                    BlobProperties properties = await blobClient.GetPropertiesAsync();  
      
                    string CopyContentType = "";  
                    string extension = Path.GetExtension(path).Substring(1);  
                    if (extension == "mp4")  
                    {  
                        //blobClient.Properties.ContentType = "video/mp4";  
                        CopyContentType = "video/mp4";  
                    }  
                    else if (extension == "png")  
                    {  
                        //blobClient.Properties.ContentType = "image/png";  
                        CopyContentType = "image/png";  
                    }  
      
                    BlobHttpHeaders headers = new BlobHttpHeaders  
                    {  
                        // Set the MIME ContentType every time the properties   
                        // are updated or the field will be cleared  
                        ContentType = CopyContentType,  
                        ContentLanguage = "en-us",  
      
                        // Populate remaining headers with   
                        // the pre-existing properties  
                        CacheControl = properties.CacheControl,  
                        ContentDisposition = properties.ContentDisposition,  
                        ContentEncoding = properties.ContentEncoding,  
                        ContentHash = properties.ContentHash  
                    };  
      
                    // Set the blob's properties.  
                    await blobClient.SetHttpHeadersAsync(headers);  
    

    A lot of this was taken from https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blob-properties-metadata?tabs=dotnet

    From my perspective, it looks like it should work. There are no compile errors. But I have a few more things to switch from SDK11 to SDK12. Then I can test it.

    @Sumarigo-MSFT I understand that you may be able to help me out here.

    Is there a replacement for BlobRequestOptions?
    Within BlobRequestOptions there is a reference to ExponentialRetry. What replaces ExponentialRetry?
    Is there a replacement for OperationContext? Do I even need it?

    Thanks!

    0 comments No comments