如何获得我刚刚复制到Azure存储帐户的CloudBlockBlob的大小?
我正在将Azure文件共享中的文件复制到存储容器中的CloudBlockBlob。在删除原始文件之前,我想验证两个位置的字节(.Properties.Length)是否相同。我认为这将是一个获得对复制的blob的新引用的情况,但是它总是-1 拷贝工作正常,对文件v blob的目视检查显示字节是相同的,只是不确定如何在我的C#应用程序中复制它 我遇到的问题是定义“复制”对象的行如何获得我刚刚复制到Azure存储帐户的CloudBlockBlob的大小?,azure,azure-storage,azure-storage-blobs,azure-files,Azure,Azure Storage,Azure Storage Blobs,Azure Files,我正在将Azure文件共享中的文件复制到存储容器中的CloudBlockBlob。在删除原始文件之前,我想验证两个位置的字节(.Properties.Length)是否相同。我认为这将是一个获得对复制的blob的新引用的情况,但是它总是-1 拷贝工作正常,对文件v blob的目视检查显示字节是相同的,只是不确定如何在我的C#应用程序中复制它 我遇到的问题是定义“复制”对象的行 要获取属性/元数据,首先需要使用方法FetchAttributes(),该方法用于填充属性和元数据 请尝试以下代码:
要获取属性/元数据,首先需要使用方法
FetchAttributes()
,该方法用于填充属性和元数据
请尝试以下代码:
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();
}
测试结果如下:
源文件长度为:184227539
--开始复制--
目标blob长度为:184227539
您还可以参考屏幕截图了解更多详细信息
注意,您需要在
destBlob.StartCopy(fileUri)之后轮询复制状态代码>,因为复制是在服务器端异步完成的。这里有一个类似的问题:@ZhaoxingLu微软,谢谢你指出!我只是认为StartCopy()将在拷贝实际完成之前一直执行线程块。用轮询复制状态更新了我的答案。循环似乎还解决了另一个问题,即复制的blob是空的,这在大文件中更常见。我打算用异步方法重新编写这一部分,但您的代码似乎达到了相同的结果。我一开始并没有使用异步,因为我一次只执行一个拷贝。你能吗?谢谢
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();
}