/// <summary> /// List containers by container name prefix /// </summary> /// <param name="prefix">Container name prefix</param> /// <returns>An enumerable collection of cloudblobcontainer</returns> internal IEnumerable <Tuple <AzureStorageContainer, BlobContinuationToken> > ListContainersByPrefix(string prefix, Func <string, bool> containerFilter = null) { BlobServiceClient blobServiceClient = Util.GetTrack2BlobServiceClient(this.Channel.StorageContext, ClientOptions); BlobContainerTraits traits = BlobContainerTraits.Metadata; BlobContainerStates states = BlobContainerStates.None; if (this.IncludeDeleted.IsPresent) { states = BlobContainerStates.Deleted; } if (!string.IsNullOrEmpty(prefix) && !NameUtil.IsValidContainerPrefix(prefix)) { throw new ArgumentException(String.Format(Resources.InvalidContainerName, prefix)); } int listCount = InternalMaxCount; int MaxListCount = 5000; int requestCount = MaxListCount; int realListCount = 0; string continuationToken = this.ContinuationToken is null ? null : this.ContinuationToken.NextMarker; do { requestCount = Math.Min(listCount, MaxListCount); realListCount = 0; IEnumerator <Page <BlobContainerItem> > enumerator = blobServiceClient.GetBlobContainers(traits, states, prefix, this.CmdletCancellationToken) .AsPages(continuationToken, requestCount) .GetEnumerator(); Page <BlobContainerItem> page; enumerator.MoveNext(); page = enumerator.Current; foreach (BlobContainerItem item in page.Values) { if (containerFilter == null || containerFilter(item.Name)) { yield return(new Tuple <AzureStorageContainer, BlobContinuationToken>( new AzureStorageContainer(item, Channel.StorageContext, blobServiceClient), string.IsNullOrEmpty(page.ContinuationToken) ? null : new BlobContinuationToken() { NextMarker = page.ContinuationToken })); realListCount++; } realListCount++; } continuationToken = page.ContinuationToken; if (InternalMaxCount != int.MaxValue) { listCount -= realListCount; } }while (listCount > 0 && !string.IsNullOrEmpty(continuationToken)); }
public GetBlobContainersAsyncCollection( BlobServiceClient client, BlobContainerTraits traits, string prefix = default) { _client = client; _traits = traits; _prefix = prefix; }
/// <summary> /// Convert the details into ListContainersIncludeType values. /// </summary> /// <returns>ListContainersIncludeType values</returns> internal static IEnumerable <ListContainersIncludeType> AsIncludeItems(BlobContainerTraits traits, BlobContainerStates states) { var items = new List <ListContainersIncludeType>(); if ((states & BlobContainerStates.Deleted) == BlobContainerStates.Deleted) { items.Add(ListContainersIncludeType.Deleted); } if ((traits & BlobContainerTraits.Metadata) == BlobContainerTraits.Metadata) { items.Add(ListContainersIncludeType.Metadata); } return(items.Count > 0 ? items : null); }
/// <summary> /// Convert the details into ListContainersIncludeType values. /// </summary> /// <returns>ListContainersIncludeType values</returns> internal static IEnumerable <ListContainersIncludeType> AsIncludeItems(BlobContainerTraits traits, BlobContainerStates states) { // Remove this line Debug.Assert(states == BlobContainerStates.None); var items = new List <ListContainersIncludeType>(); // Uncomment when feature is re-enabled. //if ((states & BlobContainerStates.Deleted) == BlobContainerStates.Deleted) //{ // items.Add(ListContainersIncludeType.Deleted); //} if ((traits & BlobContainerTraits.Metadata) == BlobContainerTraits.Metadata) { items.Add(ListContainersIncludeType.Metadata); } return(items.Count > 0 ? items : null); }
/// <summary> /// The <see cref="GetBlobContainersInternal"/> operation returns a /// single segment of blob containers in the storage account, starting /// from the specified <paramref name="continuationToken"/>. Use an empty /// <paramref name="continuationToken"/> to start enumeration from the beginning /// and the <see cref="BlobContainersSegment.NextMarker"/> if it's not /// empty to make subsequent calls to <see cref="GetBlobContainersAsync"/> /// to continue enumerating the containers segment by segment. /// Containers are ordered lexicographically by name. /// /// For more information, see <see href="https://docs.microsoft.com/rest/api/storageservices/list-containers2"/>. /// </summary> /// <param name="continuationToken"> /// An optional string value that identifies the segment of the list /// of blob containers to be returned with the next listing operation. The /// operation returns a non-empty <see cref="BlobContainersSegment.NextMarker"/> /// if the listing operation did not return all blob containers remaining /// to be listed with the current segment. The NextMarker value can /// be used as the value for the <paramref name="continuationToken"/> parameter /// in a subsequent call to request the next segment of list items. /// </param> /// <param name="traits"> /// Specifies trait options for shaping the blob containers. /// </param> /// <param name="prefix"> /// Specifies a string that filters the results to return only containers /// whose name begins with the specified <paramref name="prefix"/>. /// </param> /// <param name="pageSizeHint"> /// Gets or sets a value indicating the size of the page that should be /// requested. /// </param> /// <param name="async"> /// Whether to invoke the operation asynchronously. /// </param> /// <param name="cancellationToken"> /// Optional <see cref="CancellationToken"/> to propagate /// notifications that the operation should be cancelled. /// </param> /// <returns> /// A <see cref="Response{BlobContainersSegment}"/> describing a /// segment of the blob containers in the storage account. /// </returns> /// <remarks> /// A <see cref="StorageRequestFailedException"/> will be thrown if /// a failure occurs. /// </remarks> internal async Task <Response <BlobContainersSegment> > GetBlobContainersInternal( string continuationToken, BlobContainerTraits traits, string prefix, int?pageSizeHint, bool async, CancellationToken cancellationToken) { using (Pipeline.BeginLoggingScope(nameof(BlobServiceClient))) { Pipeline.LogMethodEnter( nameof(BlobServiceClient), message: $"{nameof(Uri)}: {Uri}\n" + $"{nameof(continuationToken)}: {continuationToken}\n" + $"{nameof(traits)}: {traits}"); try { return(await BlobRestClient.Service.ListBlobContainersSegmentAsync( Pipeline, Uri, marker : continuationToken, prefix : prefix, maxresults : pageSizeHint, include : traits.AsIncludeType(), async : async, cancellationToken : cancellationToken) .ConfigureAwait(false)); } catch (Exception ex) { Pipeline.LogException(ex); throw; } finally { Pipeline.LogMethodExit(nameof(BlobServiceClient)); } } }
/// <summary> /// The <see cref="GetBlobContainersAsync"/> operation returns an async /// sequence of blob containers in the storage account. Enumerating the /// blob containers may make multiple requests to the service while fetching /// all the values. Containers are ordered lexicographically by name. /// /// For more information, see <see href="https://docs.microsoft.com/rest/api/storageservices/list-containers2"/>. /// </summary> /// <param name="traits"> /// Specifies trait options for shaping the blob containers. /// </param> /// <param name="prefix"> /// Specifies a string that filters the results to return only containers /// whose name begins with the specified <paramref name="prefix"/>. /// </param> /// <param name="cancellationToken"> /// Optional <see cref="CancellationToken"/> to propagate /// notifications that the operation should be cancelled. /// </param> /// <returns> /// An <see cref="AsyncPageable{T}"/> describing the /// containers in the storage account. /// </returns> /// <remarks> /// A <see cref="StorageRequestFailedException"/> will be thrown if /// a failure occurs. /// </remarks> public virtual AsyncPageable <BlobContainerItem> GetBlobContainersAsync( BlobContainerTraits traits = BlobContainerTraits.None, string prefix = default, CancellationToken cancellationToken = default) => new GetBlobContainersAsyncCollection(this, traits, prefix).ToAsyncCollection(cancellationToken);
/// <summary> /// Convert the details into a ListBlobContainersIncludeType value. /// </summary> /// <returns>A ListBlobContainersIncludeType value.</returns> internal static ListBlobContainersIncludeType?AsIncludeType(this BlobContainerTraits traits) => (traits & BlobContainerTraits.Metadata) == BlobContainerTraits.Metadata ? ListBlobContainersIncludeType.Metadata : (ListBlobContainersIncludeType?)null;