Search code examples
c#azurexamarin.formsazure-active-directoryazure-storage

Rename blob then save new


I am trying to use Xamarin Blob to save the new version of a image after renaming the previous one. for example my old image is in the container named "imagecontainer" and has Id named like "xama_452"

what I would like to do is :

1- Rename the old image name : for example 'xama_452_11_2018"
2-Move it in a container "oldcontainer"
3- then save the new image in "imagecontainer"

I have tried some code I can upload a image/blob but I cannot rename it and move it to another container.

 protected static async Task<CloudBlockBlob> SaveBlockBlob(string containerName, byte[] blob, string blobTitle)
        {

            var blobContainer = GetBlobContainer(containerName);
            var blockBlob = blobContainer.GetBlockBlobReference(blobTitle);

            var oldBlob = blobContainer.GetBlockBlobReference(blockBlob.Uri.ToString());
            var newBlob = blobContainer.GetBlockBlobReference(blockBlob.Uri.ToString().Replace(blobTitle, DateTime.UtcNow.ToString()+ blobTitle));


            await newBlob.StartCopyAsync(oldBlob);

// here is the methode to upload
            // await blockBlob.UploadFromByteArrayAsync(blob, 0, blob.Length).ConfigureAwait(false);

            return blockBlob;
        }

// method to get blob's container 
 static CloudBlobContainer GetBlobContainer(string containerName) => BlobClient.GetContainerReference(containerName);

Thanks in advance


Solution

  • 1- Rename the old image name : for example 'xama_452_11_2018"

    Due to the lack of API to rename the blob file on Azure, you could set newBlobName with the format you want and copy the source to the destination. Refer to this article.

    2-Move it in a container "oldcontainer"

    You could get the desctionation container's blob to copy the source. Refer to this one.

    3- then save the new image in "imagecontainer"

    Upload blob to the sourcecontainer. Refer to this article.

    The whole codes are as below:

    public static void RenameBlob(string containerName, string destContainer,string blobName,string newblobname)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
        CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer imgcontainer = cloudBlobClient.GetContainerReference(containerName);
        string[] name = blobName.Split('.');
        //rename blob
        string newBlobName = name[0] + "_"+DateTime.Now.ToString("MM")+"_"+DateTime.Now.ToString("yyyy") + "." + name[1];
        CloudBlobContainer oldcontainer = cloudBlobClient.GetContainerReference(destContainer);
        if (!oldcontainer.Exists())
        {
            throw new Exception("Destination container does not exist.");
        }
        CloudBlockBlob blobCopy = oldcontainer.GetBlockBlobReference(newBlobName);
        if (!blobCopy.Exists())
        {
            CloudBlockBlob blob = imgcontainer.GetBlockBlobReference(blobName);
            if (blob.Exists())
            {
                //move blob to oldcontainer
                blobCopy.StartCopy(blob);
                blob.Delete();
            }
        }
        //upload blob to imagecontainer
        CloudBlockBlob cloudblobnew = imgcontainer.GetBlockBlobReference(newblobname);
        cloudblobnew.UploadFromFileAsync(newfile);
    }
    

    If your still have any problem, please feel free to let me know. Hope it helps you.