1

I'm trying to rename an Azure File. To do this, we have to COPY then DELETE (because there is no Rename API).

Given this code, will the DELETE always kick off after the COPY has completed?

await destinationCloudFile.StartCopyAsync(sourceCloudFile);
await sourceCloudFile.DeleteAsync();

I'm confused because of the word Start in StartCopyAsyc... like .. it's going to start copying or something...

Also, I'm not doing a Task.WhenAll(both those tasks) ... which I would assume would be trying to do those at the same time.

Edit

This is a RENAME, so in effect, it will be copying the new file right next to the original file : same share, same container, same directory .. and of course, all in the same storage account.

halfer
  • 19,824
  • 17
  • 99
  • 186
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
  • 1
    You migth want to read this: https://blogs.msdn.microsoft.com/windowsazurestorage/2012/06/12/introducing-asynchronous-cross-account-copy-blob/ (section "Copy is now asynchronous"). And also this answer: http://stackoverflow.com/a/21340397/5311735 – Evk Mar 02 '17 at 07:43

1 Answers1

2

If you're simply renaming a file, then the code you're using should work just fine. Renaming is essentially copying a file in the same share/directory followed by a delete. That copy operation is synchronous.


However, if you're copying the files across storage accounts then the copy operation is asynchronous and you must wait for the copy operation to finish before taking any further action on the source file (like you want to perform a move operation)

For async copy operation when the following code finishes executing without any error:

await destinationCloudFile.StartCopyAsync(sourceCloudFile); 

What that means is that your request to copy blob has been accepted by the Storage Service and has been queued. Normally the copy happens very fast but there's no guarantee.

What you would need to do is periodically check if the copy operation has been finished or not. This you can do by fetching the properties of the destination file check the copy operation status.

For some reason I thought that this would be exposed in the storage client library (I checked Version 7.0.0) but it is not there :(. But you can get this information through Get File Properties REST API. You can check the x-ms-copy-status response header (pending, success, aborted or failed).

Edit: On storage client sdk (version 8.1.4) there is a property CopyState you can check the status of the copy

Ram Y
  • 1,944
  • 17
  • 23
Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • Hi @gaurav - i'm actually renaming a file from `test.txt` to `test.txt.lock`, in the same container + directory. – Pure.Krome Mar 02 '17 at 08:13
  • Then it will be synchronous. Sorry, should have read the question more carefully :). – Gaurav Mantri Mar 02 '17 at 08:16
  • Sorry - i should have _explicity_ stated. – Pure.Krome Mar 02 '17 at 08:21
  • You did...Very first line: `I'm trying to rename an Azure File` :). Anyways I have updated my answer. I left the async part in there just in case someone is trying to move the files across storage account. – Gaurav Mantri Mar 02 '17 at 08:23
  • Great! so that means I don't have to worry then :) Still feels weird how .. i'm getting the value of my '_Please can you copy this_' result .. versus ... '_copy this now_'. – Pure.Krome Mar 02 '17 at 08:24
  • I'm not sure I understand this: `i'm getting the value of my 'Please can you copy this' result .. versus ... 'copy this now'`. Would you mind explaining. – Gaurav Mantri Mar 02 '17 at 08:26
  • the `StartCopyAsync` method (https://msdn.microsoft.com/en-us/library/azure/mt433401.aspx) returns a string .. what's that value? – Pure.Krome Mar 02 '17 at 08:31
  • This is what they call a `CopyId`. You can use this value to cancel a pending copy operation. Please see description of `x-ms-copy-id` header here: https://learn.microsoft.com/en-us/rest/api/storageservices/fileservices/copy-file. HTH. – Gaurav Mantri Mar 02 '17 at 08:33
  • Ahh!! gotcha! Hmm .. nothing in the link mentions about how a copy on the same storage account/share/container/directory is sync thouugh? – Pure.Krome Mar 02 '17 at 08:49