How to set Expiry while uploading file in container using Azure.Storage.Blobs SDK?

Yuvraj Kanakiya 45 Reputation points
2023-04-10T11:14:00.5333333+00:00

I want to set expire time of the file which I am uploading at the same time. I know about Lifecycle management but it is not useful in my case because the file path of the file will be different for every file I will upload. Does there any method which set an expiration time for a specific path? I am attaching code for better understanding.

Note : The container will be the same for all files only the folder name will change.


string connection = _configuration.GetEnvironmentVariable("AzureBlobStorage", "ConnectionString");
string containerName = _configuration.GetEnvironmentVariable("AzureBlobStorage", "ContainerName");
string publicUrl = _configuration.GetEnvironmentVariable("AzureBlobStorage", "PublicUrl");
var filePath = $"{domainCode}/{folderName}/{randomFolder}/{fileName}";
var blobClient = new BlobContainerClient(connection, containerName);

var blob = blobClient.GetBlobClient(filePath);
var blobHeader = new BlobHttpHeaders { ContentType = "application/pdf" };

await blob.UploadAsync(stream, blobHeader);
return $"{publicUrl}/{containerName}/{filePath}";
                  
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,201 questions
{count} votes

2 answers

Sort by: Most helpful
  1. shiva patpi 13,366 Reputation points Microsoft Employee Moderator
    2023-04-10T16:46:50.72+00:00

    Hello @Yuvraj Kanakiya , Please use the below code. I just tested locally. We have to use RetentionDays attribute using Metadata of CloudBlockBlob class.

     public static void SetExpirationOfBlobWhileUploading()
            {
                string file_extension,
                filename_withExtension,
                connectionString;
                Stream file;
    
                //Copy the storage account connection string from Azure portal     
                connectionString = "DefaultEndpointsProtocol=https;AccountName=storageaccountname;AccountKey=key;EndpointSuffix=core.windows.net";
                string fileToUpload = "C:\\azure\\data.txt";
                string strContainerName = "test2";
                // << reading the file as filestream from local machine >>    
                file = new FileStream(fileToUpload, FileMode.Open);
    
                CloudStorageAccount mycloudStorageAccount = CloudStorageAccount.Parse(connectionString);
                CloudBlobClient blobClient = mycloudStorageAccount.CreateCloudBlobClient();
                CloudBlobContainer container = blobClient.GetContainerReference(strContainerName);
    
                //checking the container exists or not  
                if (container.CreateIfNotExists())
                {
    
                    container.SetPermissionsAsync(new BlobContainerPermissions
                    {
                        PublicAccess =
                      BlobContainerPublicAccessType.Blob
                    });
    
                }
    
    
                file_extension = Path.GetExtension(fileToUpload);
    
                filename_withExtension = Path.GetFileName(fileToUpload);
    
    
    
                CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(filename_withExtension);
    
                cloudBlockBlob.Properties.ContentType = file_extension;
    
                cloudBlockBlob.Metadata.Add("RetentionDays", "1"); //EXPIRATION of BLOB
    
                cloudBlockBlob.UploadFromStream(file); // << Uploading the file to the blob >>  
    
    
    
                Console.WriteLine("Upload Completed!");
    
    
    
            }
    
    
    
    
    
           
            
                filename_withExtension = Path.GetFileName(fileToUpload);
    
                CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(filename_withExtension);
                cloudBlockBlob.Properties.ContentType = file_extension;
                cloudBlockBlob.Metadata.Add("RetentionDays", "1");
                cloudBlockBlob.UploadFromStream(file); // << Uploading the file to the blob >>  
    
                Console.WriteLine("Upload Completed!");
    
            }
    
    1 person found this answer helpful.

  2. Kaja Sherief 115 Reputation points
    2023-04-30T16:25:04.5266667+00:00

    there is no direct way to set an expiration time for a specific blob path in Azure Blob Storage. However, you can achieve a similar result using a combination of Azure Functions and Blob Storage Lifecycle Management.

    Here are two approaches you can consider:

    Approach 1: Azure Functions

    1. Create an Azure Function that is triggered by a timer (e.g., running daily).
    2. In the function, list all blobs in the container and filter the ones that should be deleted based on their age and path.
    3. Delete the filtered blobs.

    Pros:

    • More control over the deletion process.
    • Can be used for specific blob paths.

    Cons:

    • Requires additional development and maintenance.
    • May incur additional costs for running Azure Functions.

    Approach 2: Blob Storage Lifecycle Management

    1. Create a Lifecycle Management rule for the container.
    2. Use a prefix filter to target specific folders within the container.
    3. Set the rule to delete blobs after a certain number of days.

    Pros:

    • Native solution provided by Azure Blob Storage.
    • Easier to set up and maintain.

    Cons:

    • Less control over the deletion process.
    • Cannot target individual blob paths, only prefixes (folder paths).

    You mentioned that Lifecycle Management is not useful in your case because the file path will change for every file you upload. However, if there is a common pattern in the folder structure, you can still use the prefix filter in Lifecycle Management to target specific folders.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.