Search code examples
c#azureazure-storageazure-storage-emulator

Is there a way to copy a blob from the local storage emulator to a remote azure storage?


I'm trying to copy a video file from the local azure storage, to a remote storage in C#, to be able to encode it using Azure Media Services.

After retrieving the blobs for the copy using the StartCopy method, I get a 404 NOT FOUNT exception returned.

destinationBlob.StartCopy(new Uri(sourceBlob.Uri.AbsoluteUri + signature));

The value of sourceBlob.Uri.AbsoluteUri is a local Uri (http://127.0.0.1/ params)

I would expect the copy to be executed but instead I get a 404 Error.


Solution

    • You need two connections to Azure storage.
    • You need to connect to your local video file in the emulator and treat it like a stream (call it localStream).
    • You then need to open you destination (remote) endpoint in Azure Storage and open it for Write as a stream (call it remoteStream)
    • You can then do a localStream.CopyTo(remoteStream);

    This example allows you to pass in a Task to this function which accepts the Stream as an object to deal with. You will need to adjust the utils.GetBlockBlobReference to point to your actual blob reference. This method opens the stream for Wtire, you may also need one that opens a stream for reading. I've different providers for reading/writing to storage.

        public async Task Use(string pointer, Func<System.IO.Stream, Task> useAction)
        {
            if (useAction == null)
            {
                throw new ArgumentNullException(nameof(useAction));
            }
    
            var blobRef = await utils.GetBlockBlobReference(storageFactory, pointer);
            using (var cloudStream = await blobRef.OpenWriteAsync())
            {
                await useAction(cloudStream);
            }
        }