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!");
}