I'm trying to copy files from one folder to another in the same blob container (IE: everything in container/processing
to container/archived
). The following code creates the target folder and files but it's always 0 bytes. What is wrong with this code? I've tried using just a single stream, same problem. _blobServiceClient is the type BlobServiceClient. If I download and serialize the object to a C# class and then re-upload it, it works. But I'd prefer to make this as efficient as possible since I'm just moving things around in BlobStorage and don't actually need to use the contents in my code.
var containerClient = _blobServiceClient.GetBlobContainerClient(container);
var blobHierarchyItems = containerClient.GetBlobsByHierarchy(BlobTraits.None, BlobStates.None, null, sourceFolder);
foreach (var bhi in blobHierarchyItems)
{
using (var downStream = new MemoryStream())
using (var upStream = new MemoryStream())
{
var blobClient = containerClient.GetBlobClient(bhi.Blob.Name);
blobClient.DownloadTo(downStream);
containerClient.UploadBlob($"{targetFolder}/{bhi.Blob.Name}", upStream);
downStream.Position = 0;
await downStream.CopyToAsync(upStream);
}
}
I've also tried using a BlockBlobClient.StartCopyFromUriAsync
function. The results are the same, it creates the target folder and blob but no data. Here's that code:
var containerClient = _blobServiceClient.GetBlobContainerClient(container);
var blobHierarchyItems = containerClient.GetBlobs(BlobTraits.None, BlobStates.None, sourceFolder);
var targetBlockClient = containerClient.GetBlockBlobClient(targetFolder);
foreach (var bhi in blobHierarchyItems)
{
var blobUri = new Uri($"{containerClient.Uri}/{bhi.Name}");
var copyOp = await targetBlockClient.StartCopyFromUriAsync(blobUri);
await copyOp.WaitForCompletionAsync();
}
Source folder
Target folder