Trouble Upgrading from Azure Storage 1.7 to 2.0

佐手、 提交于 2019-12-13 04:01:46

问题


I am currently involved in upgrading from Azure 1.7 to 2.2 and am encountering breaking changes with Storage. All the storage calls in the libraries are covered by Unit Tests and I've ironed out most of the changes.

I am completely stuck on one of our core methods which gets a list of subdirectories in a directory. I know they are not actual directories, but parts of blob names, but the functionality existed before 2.0 and we make heavy use of it across nearly 30 different services.

The storage blob address is testdata/test/test1/blob.txt

And the test

/// Unit Test
[Test]
public void BuildDirectoryAndRetrieveUsingSubDirectory()
{
  CloudBlobDirectory subDirectory = GetBlobDirectory("testdata/test/");
  IEnumerable<CloudBlobDirectory> dirs = 
    subDirectory.ListBlobs().OfType<CloudBlobDirectory>();
  Assert.AreEqual(1, dirs.Count());
}

The old 1.7 code for GetBlobDirectory returned a list of every directory blob in testdata/test/, so in this case would return test1

/// Azure Storage 1.7
public static CloudBlobDirectory GetBlobDirectory(string directoryReference)
{
  return BlobClient.GetBlobDirectoryReference(directoryReference);
}

I have tried in vain to get the same results from using 2.0

/// Azure Storage 2.0
public static CloudBlobDirectory GetBlobDirectory(string directoryReference)
{
  string containerName = GetContainerNameFromDirectoryName(directoryReference);
  CloudBlobContainer container = BlobClient.GetContainerReference(containerName);
  return container.GetBlobDirectoryReference(directoryReference);
}

However back in the test the dirs just returns "the enumeration yielded no results".

Can anyone help - I want very much to leave the test code alone, but return the same results from the method.

Thanks


回答1:


Found the answer, which was surprisingly simple.

In StorageClient 1.7, the prefix value that you passed in included the container name and had to end with a "/".

So basically the containerName becomes "testdata" and the directoryPrefix becomes "test".

In the latest version the prefix value is anything not including the container name, so the function has changed to be

public static CloudBlobDirectory GetBlobDirectory(string directoryReference)
{
    string containerName = GetContainerNameFromDirectoryName(directoryReference);
    string directoryPrefix = GetPrefixFormDirectoryName(directoryReference);
    CloudBlobContainer container = BlobClient.GetContainerReference(containerName);
    var blobs = container.ListBlobs(directoryPrefix, false);
    return (CloudBlobDirectory)blobs.Where(b => b as CloudBlobDirectory !=null).First();
}


来源:https://stackoverflow.com/questions/25306855/trouble-upgrading-from-azure-storage-1-7-to-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!