我必须在同一存储帐户上的共享和 blob 之间移动一些文件。经过一番谷歌搜索后,我得到了以下代码:
CloudFileClient fileClient = account.CreateCloudFileClient();
CloudFileShare share = fileClient.GetShareReference("shareName");
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
CloudFileDirectory videoDirectory = rootDir.GetDirectoryReference(video.StoragePath);
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(video.StoragePath);
container.CreateIfNotExists();
foreach (var Files in videoDirectory.ListFilesAndDirectories())
{
var arr = Files.Uri.ToString().Split('/');
string strFileName = arr[arr.Length - 1];
CloudFile sourceFile = videoDirectory.GetFileReference(strFileName);
string fileSas = sourceFile.GetSharedAccessSignature(new SharedAccessFilePolicy()
{
Permissions = SharedAccessFilePermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
});
Uri fileSasUri = new Uri(sourceFile.StorageUri.PrimaryUri.ToString() + fileSas);
CloudBlockBlob blockBlob = container.GetBlockBlobReference(strFileName);
blockBlob.StartCopyAsync(fileSasUri).Wait(); //copy the file to blob storage and wait for the operation to complete
//sourceFile.DeleteAsync(); //delete the file
}
//videoDirectory.DeleteAsync(); //delete the directory
如果删除行未注释,则目标包含源文件夹中的所有文件,但大小为 0。
任何想法我做错了什么?
我还想在复制所有文件后删除共享目录。有没有办法检查文件是否在目标文件夹中?