0

I can see that renaming a blob is not natively supported but there must be a way to workaround this limitation in some way

I have an azure function that is using a blob trigger. A file is dropped in a container every 30 miniutes. The azure function start and process the file(Blob) and store the file in archive container. But every file that is dropped have the same filename so I must in some way append a timestamp to the filename(Blob) before I save it to the archive container. If the blob trigger was activated at 2023-05-12T08:18:03 and the filename is foo.xml then the renamed file should be foo2023-05-12T08:18:03.xml. I have been searching but haven't find a way so far. I use C# as the programing language in the azure function.

rene
  • 41,474
  • 78
  • 114
  • 152
tony
  • 37
  • 5
  • You can't rename, but you can copy a blob (without down- and upload) directly as a server action and afterwards delete the original blob. https://stackoverflow.com/questions/14152087/copying-one-azure-blob-to-another-blob-in-azure-storage-client-2-0 – Oliver May 12 '23 at 07:13
  • But my intention is to keep the filename with appended timestamp. – tony May 12 '23 at 07:22

1 Answers1

0

Firstly, I created a Blob in my Storage account container as below, enter image description here

Then I tried the below c# code, I am able to rename the Blob,

string connectionString = "your-connection-string";
string containerName = "your-container-name";
string blobName = "your-blob-name";

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

string timestamp = DateTime.UtcNow.ToString("yyyyMMddTHHmmss");
string newBlobName = $"{Path.GetFileNameWithoutExtension(blobName)}{timestamp}.xml";
CloudBlockBlob newBlob = container.GetBlockBlobReference(newBlobName);
await newBlob.StartCopyAsync(blob);
await blob.DeleteAsync();

Console.WriteLine($"Blob renamed to {newBlobName}");

Output:

Local output: enter image description here

Portal output: enter image description here

Dasari Kamali
  • 811
  • 2
  • 2
  • 6
  • How do I get the filename from [BlobTrigger("input-xml/{name}.xml")] Stream inputBlob – tony May 12 '23 at 08:41
  • Your example code must change the filename in the inputBlob. If I just do string blobName = "foo.xml" doesn't change the filename in the inputBlob – tony May 12 '23 at 08:57
  • You might know what I can do to make your code work the way I want it to work which is the blob that is stored in archive should be appended with a timestamp. – tony May 12 '23 at 09:41
  • I don't understand how to make you code work the way I want it to work meaning appending the blob with a timestamp when store it archive container. – tony May 12 '23 at 09:48
  • You say you have updated the answer but I don't see any change. I have skipped the await in your code and I don't think that is the problem. Here is the attribute and argument for the run method public void Run( [BlobTrigger("input-xml/{name}.xml")] Stream inputBlob, [Blob("archive/{name}",FileAccess.Write)] Stream outputBlob, string name, ILogger log) { – tony May 12 '23 at 11:24