internal static string GetAccountNameFromUri(Uri clientUri, bool?usePathStyleUris) { if (!usePathStyleUris.HasValue) { // Automatically determine whether to use path style vs host style uris usePathStyleUris = CommonUtility.UsePathStyleAddressing(clientUri); } string[] parts = clientUri.AbsoluteUri.Split(NavigationHelper.SlashAsSplitOptions, StringSplitOptions.RemoveEmptyEntries); if (usePathStyleUris.Value) { if (parts.Length < 3) { string errorMessage = string.Format(CultureInfo.InvariantCulture, SR.MissingAccountInformationInUri, clientUri.AbsoluteUri); throw new InvalidOperationException(errorMessage); } return(parts[2]); } else { if (parts.Length < 2) { string errorMessage = string.Format(CultureInfo.InvariantCulture, SR.MissingAccountInformationInUri, clientUri.AbsoluteUri); throw new InvalidOperationException(errorMessage); } int endIndex = parts[1].IndexOf(".", StringComparison.Ordinal); return(parts[1].Substring(0, endIndex)); } }
/// <summary> /// Retrieve the share name and the file or directory name from a file or directory address. /// </summary> /// <param name="fileAddress">The file address.</param> /// <param name="usePathStyleUris">If set to <c>true</c> use path style Uris.</param> /// <param name="shareName">The resulting share name.</param> /// <param name="fileName">The resulting file or directory name.</param> private static void GetShareNameAndFileName(Uri fileAddress, bool?usePathStyleUris, out string shareName, out string fileName) { CommonUtility.AssertNotNull("fileAddress", fileAddress); if (usePathStyleUris == null) { // Automatically determine whether to use path style vs host style uris usePathStyleUris = CommonUtility.UsePathStyleAddressing(fileAddress); } string[] addressParts = fileAddress.Segments; int shareIndex = usePathStyleUris.Value ? 2 : 1; if (addressParts.Length - 1 < shareIndex) { // No reference appears to any share or file string error = string.Format(CultureInfo.CurrentCulture, SR.MissingShareInformation, fileAddress); throw new ArgumentException(error, "fileAddress"); } else if (addressParts.Length - 1 == shareIndex) { // This is the root directory of a share string shareOrFileName = addressParts[shareIndex]; shareName = shareOrFileName.Trim(SlashChar); fileName = string.Empty; } else { // This is a file or a directory in a share shareName = addressParts[shareIndex].Trim(SlashChar); fileName = addressParts[addressParts.Length - 1]; } }
/// <summary> /// Get container name from address for styles of paths /// Example: http://test.blob.core.windows.net/container/blob => container /// http://127.0.0.1:10000/test/container/blob => container. /// </summary> /// <param name="uri">The container Uri.</param> /// <param name="usePathStyleUris">If set to <c>true</c> use path style Uris.</param> /// <returns>The container name.</returns> internal static string GetContainerNameFromContainerAddress(Uri uri, bool?usePathStyleUris) { if (usePathStyleUris == null) { // Automatically determine whether to use path style vs host style uris usePathStyleUris = CommonUtility.UsePathStyleAddressing(uri); } if (usePathStyleUris.Value) { string[] parts = uri.AbsolutePath.Split(NavigationHelper.SlashAsSplitOptions); if (parts.Length < 3) { string errorMessage = string.Format(CultureInfo.CurrentCulture, SR.MissingAccountInformationInUri, uri.AbsoluteUri); throw new InvalidOperationException(errorMessage); } return(parts[2]); } else { return(uri.AbsolutePath.Substring(1)); } }
/// <summary> /// Gets the service client base address. /// </summary> /// <param name="addressUri">The address Uri.</param> /// <param name="usePathStyleUris">The use path style Uris.</param> /// <returns>The base address of the client.</returns> /// <example> /// GetServiceClientBaseAddress("http://testaccount.blob.core.windows.net/testcontainer/blob1") /// returns "http://testaccount.blob.core.windows.net" /// </example> internal static Uri GetServiceClientBaseAddress(Uri addressUri, bool?usePathStyleUris) { if (addressUri == null) { return(null); } if (usePathStyleUris == null) { // Automatically determine whether to use path style vs host style uris usePathStyleUris = CommonUtility.UsePathStyleAddressing(addressUri); } Uri authority = new Uri(addressUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.UriEscaped)); if (usePathStyleUris.Value) { // Path style uri string[] segments = addressUri.Segments; if (segments.Length < 2) { string error = string.Format(CultureInfo.CurrentCulture, SR.PathStyleUriMissingAccountNameInformation); throw new ArgumentException("address", error); } return(new Uri(authority, segments[1])); } else { return(authority); } }
/// <summary> /// Retrieves the file and directory part of a storage Uri. /// </summary> /// <param name="fileAddress">The file address.</param> /// <param name="usePathStyleUris">If set to <c>true</c> use path style Uris.</param> /// <returns>The file name including directories.</returns> internal static string GetFileAndDirectoryName(Uri fileAddress, bool?usePathStyleUris) { CommonUtility.AssertNotNull("fileAddress", fileAddress); if (usePathStyleUris == null) { // Automatically determine whether to use path style vs host style uris usePathStyleUris = CommonUtility.UsePathStyleAddressing(fileAddress); } string[] addressParts = fileAddress.Segments; int shareIndex = usePathStyleUris.Value ? 2 : 1; if (addressParts.Length - 1 < shareIndex) { // No reference appears to any share or file. string error = string.Format(CultureInfo.CurrentCulture, SR.MissingShareInformation, fileAddress); throw new ArgumentException(error, "fileAddress"); } else if (addressParts.Length - 1 == shareIndex) { // This is root directory of a share. return(string.Empty); } else { // This is a file with directories (the relevant case). // Skip (shareIndex + 1) because Skip takes a count, not an index, as a param. return(Uri.UnescapeDataString(string.Concat(addressParts.Skip(shareIndex + 1)))); } }
/// <summary> /// Retrieve the container name and the blob name from a blob address. /// </summary> /// <param name="blobAddress">The blob address.</param> /// <param name="usePathStyleUris">If set to <c>true</c> use path style Uris.</param> /// <param name="containerName">The resulting container name.</param> /// <param name="blobName">The resulting blob name.</param> /// <returns>A bool representing whether the blob is in an explicit container or not.</returns> private static bool GetContainerNameAndBlobName(Uri blobAddress, bool?usePathStyleUris, out string containerName, out string blobName) { CommonUtility.AssertNotNull("blobAddress", blobAddress); if (usePathStyleUris == null) { // Automatically determine whether to use path style vs host style uris usePathStyleUris = CommonUtility.UsePathStyleAddressing(blobAddress); } string[] addressParts = blobAddress.Segments; int containerIndex = usePathStyleUris.Value ? 2 : 1; int firstBlobIndex = containerIndex + 1; if (addressParts.Length - 1 < containerIndex) { // No reference appears to any container or blob string error = string.Format(CultureInfo.CurrentCulture, SR.MissingContainerInformation, blobAddress); throw new ArgumentException(error, "blobAddress"); } else if (addressParts.Length - 1 == containerIndex) { // This is either the root directory of a container or a blob in the root container string containerOrBlobName = addressParts[containerIndex]; if (containerOrBlobName[containerOrBlobName.Length - 1] == SlashChar) { // This is the root directory of a container containerName = containerOrBlobName.Trim(SlashChar); blobName = string.Empty; } else { // This is a blob implicitly in the root container containerName = RootContainerName; blobName = containerOrBlobName; } } else { // This is a blob in an explicit container containerName = addressParts[containerIndex].Trim(SlashChar); string[] blobNameSegments = new string[addressParts.Length - firstBlobIndex]; Array.Copy(addressParts, firstBlobIndex, blobNameSegments, 0, blobNameSegments.Length); blobName = string.Concat(blobNameSegments); return(true); } return(false); }