Search code examples
azure-functionsazure-storageazure-blob-storage

Copy a file from one storage to another using Azure function throws error


I am trying to copy one file from one storage to a second storage. Basically, someone uploads a file to storage A and my Azure function automatically copy it over to storage B.

In Visual studio I have the following with the trigger pointing to the local development storage.

[FunctionName("CopyUserFileToTestStorage")]
public static async Task Run(
    [BlobTrigger("user-content", Connection = "")]CloudBlockBlob incomingBlob,
    [Blob("user-content2", FileAccess.Write, Connection = "SecondStorageConnectionString")] CloudBlobContainer outgoingBlobContainer, 
    TraceWriter log)
{
    log.Info($"Incoming blob to storage\n Name:{incomingBlob.Name} \n Path: {incomingBlob.Uri}");

    await outgoingBlobContainer.CreateIfNotExistsAsync();
    var outgoingBlob = outgoingBlobContainer.GetBlockBlobReference(incomingBlob.Name);
    await outgoingBlob.StartCopyAsync(incomingBlob);
}

The following code throws this error (5 times before it gives up):

[2021-05-13 14:39:48] Executing 'CopyUserFileToTestStorage' (Reason='New blob detected: user-content/test-image.jpg', Id=3f3c2341-6c1a-4fb6-b4af-5c71bfed47b2)
[2021-05-13 14:39:48] Incoming blob to storage
 Name:test-image.jpg
 Path: http://127.0.0.1:10000/devstoreaccount1/user-content/test-image.jpg
[2021-05-13 14:39:49] Executed 'CopyUserFileToTestStorage' (Failed, Id=3f3c2341-6c1a-4fb6-b4af-5c71bfed47b2)
[2021-05-13 14:39:49] System.Private.CoreLib: Exception while executing function: CopyUserFileToTestStorage. Microsoft.WindowsAzure.Storage: The specified resource does not exist.

I know that the connection string for the second storage works because the path "user-content2" is created when this triggers. The local development storage has read access as I can call the image in a browser and it shows just fine.

Can it be that the image has not yet been uploaded completely to the storage (I am testing using the Storage explorer) and thus does not exist?


Solution

  • The issue is that you can't copy blobs between storage account and an emulator using blob copy functionality.

    Blob copy functionality is a server-side functionality where Azure Storage Service asynchronously copies the blob from a source account to a target account. For copy operation to work, Azure Storage Service should be able to reach both source and target account. Considering your target account is the emulator account which is unreachable from Azure Storage Service, you're getting this error.

    Please try by copying between two actual storage accounts and your code should work just fine.