<PackageReference Include="Azure.Storage.Blobs" Version="12.24.0" />

BlobUriBuilder

public class BlobUriBuilder
The BlobUriBuilder class provides a convenient way to modify the contents of a Uri instance to point to different Azure Storage resources like an account, container, or blob. For more information, see Naming and Referencing Containers, Blobs, and Metadata.
using Azure.Core; using Azure.Storage.Sas; using Azure.Storage.Shared; using System; using System.Text; namespace Azure.Storage.Blobs { public class BlobUriBuilder { private Uri _uri; private readonly bool _isPathStyleUri; private string _scheme; private string _host; private int _port; private string _accountName; private string _containerName; private string _blobName; private string _snapshot; private string _versionId; private BlobSasQueryParameters _sas; private string _query; public bool TrimBlobNameSlashes { get; } public string Scheme { get { return _scheme; } set { ResetUri(); _scheme = value; } } public string Host { get { return _host; } set { ResetUri(); _host = value; } } public int Port { get { return _port; } set { ResetUri(); _port = value; } } public string AccountName { get { return _accountName; } set { ResetUri(); _accountName = value; } } public string BlobContainerName { get { return _containerName; } set { ResetUri(); _containerName = value; } } public string BlobName { get { return _blobName; } set { ResetUri(); _blobName = value; } } public string Snapshot { get { return _snapshot; } set { ResetUri(); _snapshot = value; } } public string VersionId { get { return _versionId; } set { ResetUri(); _versionId = value; } } public BlobSasQueryParameters Sas { get { return _sas; } set { ResetUri(); _sas = value; } } public string Query { get { return _query; } set { ResetUri(); _query = value; } } public BlobUriBuilder(Uri uri) : this(uri, true) { } public BlobUriBuilder(Uri uri, bool trimBlobNameSlashes) { Uri uri2 = uri; if ((object)uri2 == null) throw new ArgumentNullException("uri"); uri = uri2; TrimBlobNameSlashes = trimBlobNameSlashes; Scheme = uri.Scheme; Host = uri.Host; Port = uri.Port; AccountName = ""; BlobContainerName = ""; BlobName = ""; Snapshot = ""; VersionId = ""; Sas = null; Query = ""; if (!string.IsNullOrEmpty(uri.AbsolutePath)) { string text = uri.GetPath() + uri.Fragment; int num = 0; if (uri.IsHostIPEndPointStyle()) { _isPathStyleUri = true; int num2 = text.IndexOf("/", StringComparison.InvariantCulture); if (num2 == -1) { AccountName = text; num = text.Length; } else { AccountName = text.Substring(0, num2); num = num2 + 1; } } else AccountName = (uri.GetAccountNameFromDomain("blob") ?? string.Empty); int num3 = text.IndexOf("/", num, StringComparison.InvariantCulture); if (num3 == -1) BlobContainerName = text.Substring(num); else { BlobContainerName = text.Substring(num, num3 - num); BlobName = text.Substring(num3 + 1).UnescapePath(TrimBlobNameSlashes); } } UriQueryParamsCollection uriQueryParamsCollection = new UriQueryParamsCollection(uri.Query); if (uriQueryParamsCollection.TryGetValue("snapshot", out string value)) { Snapshot = value; uriQueryParamsCollection.Remove("snapshot"); } if (uriQueryParamsCollection.TryGetValue("versionid", out string value2)) { VersionId = value2; uriQueryParamsCollection.Remove("versionid"); } if (!string.IsNullOrEmpty(Snapshot) && !string.IsNullOrEmpty(VersionId)) throw new ArgumentException("Snapshot and VersionId cannot both be set."); if (uriQueryParamsCollection.ContainsKey("sv")) Sas = new BlobSasQueryParameters(uriQueryParamsCollection); Query = uriQueryParamsCollection.ToString(); } public Uri ToUri() { if (_uri == (Uri)null) _uri = BuildUri().ToUri(); return _uri; } public override string ToString() { return ((object)BuildUri()).ToString(); } private void ResetUri() { _uri = null; } private RequestUriBuilder BuildUri() { StringBuilder stringBuilder = new StringBuilder(""); if (_isPathStyleUri && !string.IsNullOrWhiteSpace(AccountName)) stringBuilder.Append('/').Append(AccountName); if (!string.IsNullOrWhiteSpace(BlobContainerName)) { stringBuilder.Append('/').Append(BlobContainerName); if (BlobName != null && BlobName.Length > 0) stringBuilder.Append('/').Append(BlobName.EscapePath(TrimBlobNameSlashes)); } StringBuilder stringBuilder2 = new StringBuilder(Query); if (!string.IsNullOrWhiteSpace(Snapshot)) { if (stringBuilder2.Length > 0) stringBuilder2.Append('&'); stringBuilder2.Append("snapshot").Append('=').Append(Snapshot); } if (!string.IsNullOrWhiteSpace(VersionId)) { if (stringBuilder2.Length > 0) stringBuilder2.Append('&'); stringBuilder2.Append("versionid").Append('=').Append(VersionId); } string value = Sas?.ToString(); if (!string.IsNullOrWhiteSpace(value)) { if (stringBuilder2.Length > 0) stringBuilder2.Append('&'); stringBuilder2.Append(value); } RequestUriBuilder val = new RequestUriBuilder(); val.set_Scheme(Scheme); val.set_Host(Host); val.set_Port(Port); val.set_Path(stringBuilder.ToString()); val.set_Query((stringBuilder2.Length > 0) ? ("?" + stringBuilder2.ToString()) : null); return val; } } }