如何获取刚刚复制到 Azure 存储帐户的 CloudBlockBlob 的大小?
How do I get the size of an CloudBlockBlob I just copied to Azure storage account?
我正在将文件从 Azure 文件共享复制到存储容器中的 CloudBlockBlob。在删除原始文件之前,我想验证两个位置的字节 (.Properties.Length) 是否相同。我认为这是获得对复制的 blob 的新引用的情况,但它始终为 -1。
复制工作正常,对文件 v blob 的目视检查显示字节相同,只是不确定如何在我的 C# 应用程序中复制它。
我遇到问题的那一行是定义 "copied" 对象的那一行。
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);
在获取属性/元数据之前,您需要先使用方法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
您也可以参考截图了解更多详情。
我正在将文件从 Azure 文件共享复制到存储容器中的 CloudBlockBlob。在删除原始文件之前,我想验证两个位置的字节 (.Properties.Length) 是否相同。我认为这是获得对复制的 blob 的新引用的情况,但它始终为 -1。
复制工作正常,对文件 v blob 的目视检查显示字节相同,只是不确定如何在我的 C# 应用程序中复制它。
我遇到问题的那一行是定义 "copied" 对象的那一行。
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);
在获取属性/元数据之前,您需要先使用方法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
您也可以参考截图了解更多详情。