Reputation: 313
I'm copying files from Azure file shares to CloudBlockBlob in a storage container. I want to verify that the bytes (.Properties.Length) is the same in both locations before I delete the original. I thought it would be a case of getting a new reference to the copied blob, however it is always -1.
The copy works fine and a visual inspection of the file v blob shows the bytes are identical, just not sure how to replicate this in my C# application.
The line I'm having issues with is the one that defines the "copied" object.
string myfile = @"junk.txt";
CloudFile sourcefile =
fileStorage.Share.GetRootDirectoryReference().GetFileReference(myfile);
CloudBlockBlob destBlob =
destStorage.Container.GetBlockBlobReference(myfile);
string fileSAS = sourcefile.GetSharedAccessSignature(new
SharedAccessFilePolicy()
{
Permissions = SharedAccessFilePermissions.Read,
SharedAccessExpiryTime = DateTime.Now.AddHours(24)
});
Uri fileUri = new Uri(sourcefile.StorageUri.PrimaryUri.ToString() + fileSAS);
CloudBlockBlob destBlob = destStorage.Container.GetBlockBlobReference(file.Path);
destBlob.StartCopy(fileUri);
CloudBlockBlob copied = destStorage.Container.GetBlockBlobReference(myfile);
Upvotes: 3
Views: 2491
Reputation: 29985
Before you want to fetch property / metadata, you need to use the method FetchAttributes()
first, which is used to populate properties and metadata.
Please try the code below:
static void Main(string[] args)
{
string myfile = "123.txt";
CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials("account_name", "account_key"), true);
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
CloudFileShare fileShare = fileClient.GetShareReference("test");
CloudFile sourceFile = fileShare.GetRootDirectoryReference().GetFileReference(myfile);
sourceFile.FetchAttributes();
Console.WriteLine("The source file length is: "+sourceFile.Properties.Length);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("aa1");
CloudBlockBlob destBlob = container.GetBlockBlobReference(myfile);
string fileSAS = sourceFile.GetSharedAccessSignature(new SharedAccessFilePolicy() {
Permissions = SharedAccessFilePermissions.Read,
SharedAccessExpiryTime=DateTime.Now.AddHours(24)
});
Uri fileUri = new Uri(sourceFile.StorageUri.PrimaryUri.ToString() + fileSAS);
Console.WriteLine("--copy started--");
destBlob.StartCopy(fileUri);
destBlob = container.GetBlockBlobReference(myfile);
destBlob.FetchAttributes();
//use poll to check if the copy is completed or not.
while (destBlob.CopyState.Status == CopyStatus.Pending)
{
Thread.Sleep(500);
destBlob.FetchAttributes();
}
//when the copy completed, then check the copied file length.
if (destBlob.CopyState.Status == CopyStatus.Success)
{
Console.WriteLine("the dest blob length is: " + destBlob.Properties.Length);
}
else
{
Console.WriteLine("the copy operation is failed!");
}
Console.ReadLine();
}
Test result as below:
The source file length is: 184227539
--copy started--
the dest blob length is: 184227539
You can also refer to the screenshot for more details.
Upvotes: 8