/// <summary> /// Initializes a new instance of the <see cref="CloudBlobDirectory"/> class given an address and a client. /// </summary> /// <param name="address">The blob directory's address.</param> /// <param name="service">The client to use.</param> internal CloudBlobDirectory(string address, CloudBlobClient service) { CommonUtils.AssertNotNullOrEmpty("address", address); CommonUtils.AssertNotNull("service", service); this.ServiceClient = service; if (!address.EndsWith(this.ServiceClient.DefaultDelimiter)) { address = address + this.ServiceClient.DefaultDelimiter; } this.Uri = NavigationHelper.AppendPathToUri(service.BaseUri, address); }
/// <summary> /// Retreives the parent name from a storage Uri. /// </summary> /// <param name="blobAddress">The BLOB address.</param> /// <param name="delimiter">The delimiter.</param> /// <param name="usePathStyleUris">If set to <c>true</c> use path style Uris.</param> /// <returns>The name of the parent.</returns> /// <remarks> /// Adds the trailing delimiter as the prefix returned by the storage REST api always contains the delimiter. /// </remarks> /// <example> /// GetParentName(new Uri("http://test.blob.core.windows.net/mycontainer/myfolder/myblob", "/")) will return "/mycontainer/myfolder/" /// GetParentName(new Uri("http://test.blob.core.windows.net/mycontainer/myfolder|myblob", "|") will return "/mycontainer/myfolder|" /// GetParentName(new Uri("http://test.blob.core.windows.net/mycontainer/myblob", "/") will return "/mycontainer/" /// GetParentName(new Uri("http://test.blob.core.windows.net/mycontainer/", "/") will return "/mycontainer/" /// </example> internal static string GetParentName(Uri blobAddress, string delimiter, bool usePathStyleUris) { CommonUtils.AssertNotNull("blobAbsoluteUriString", blobAddress); CommonUtils.AssertNotNullOrEmpty("delimiter", delimiter); string containerName; Uri containerUri; GetContainerNameAndAddress(blobAddress, usePathStyleUris, out containerName, out containerUri); // Get the blob path as the rest of the Uri var blobPathUri = containerUri.MakeRelativeUri(blobAddress); var blobPath = blobPathUri.OriginalString; if (blobPath.EndsWith(delimiter)) { blobPath = blobPath.Substring(0, blobPath.Length - delimiter.Length); } string parentName = null; if (string.IsNullOrEmpty(blobPath)) { // Case 1 /<ContainerName>[Delimiter]*? => /<ContainerName> // Parent of container is container itself parentName = containerName + delimiter; } else { var parentLength = blobPath.LastIndexOf(delimiter); if (parentLength <= 0) { // Case 2 /<Container>/<folder> // Parent of a folder is container parentName = containerName + delimiter; } else { // Case 3 /<Container>/<folder>/[<subfolder>/]*<BlobName> // Parent of blob is folder parentName = blobPath.Substring(0, parentLength + delimiter.Length); } } return(parentName); }