So I would like to copy an Azure blob and its metadata to a new blob. I have the method
public void CopyBlob(CloudBlockBlob blob, CloudBlockBlob newBlob)
{
CopyStatus copy = CopyStatus.Pending;
while (copy != CopyStatus.Success)
{
newBlob.StartCopyFromBlob(blob);
copy = CheckIsDoneCopying(newBlob, "MyContainerName");
}
}
public CopyStatus CheckIsDoneCopying(CloudBlockBlob blob, string containerName)
{
while (blob.CopyState.Status == CopyStatus.Pending)
{
Thread.Sleep(TimeSpan.FromSeconds(20));
blob = GetBlob(blob.Name, containerName);
}
return blob.CopyState.Status;
}
These methods work fine for copying a blob but doesn't copy existing metadata from my existing blob to the new one. Is this possible?
Looking at the REST API documentation for Copy Blob
one thing caught my eye (Under Request Headers
section, x-ms-meta-name:value
):
If no name-value pairs are specified, the operation will copy the source blob metadata to the destination blob. If one or more name-value pairs are specified, the destination blob is created with the specified metadata, and metadata is not copied from the source blob.
Now when I took a look at your source code, you're actually setting the metadata on the new blob before copying.
newBlob.FetchAttributes();
if (newBlob.Metadata.ContainsKey(StorageManager.IsLive))
{
newBlob.Metadata[StorageManager.IsLive] = "N";
}
else
{
newBlob.Metadata.Add(new KeyValuePair<string, string>(StorageManager.IsLive, "N"));
}
Since metadata already exists for the new blob, the copy blob operation will not copy the metadata from source blob to the destination.
If you remove the code above, the metadata from source blob should be copied over to the new blob.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments