/// <summary> /// list blobs by blob name and container name /// </summary> /// <param name="containerName">container name</param> /// <param name="blobName">blob name pattern</param> /// <returns>An enumerable collection of IListBlobItem</returns> internal IEnumerable <Tuple <ICloudBlob, BlobContinuationToken> > ListBlobsByName(string containerName, string blobName) { CloudBlobContainer container = null; BlobRequestOptions requestOptions = RequestOptions; AccessCondition accessCondition = null; string prefix = string.Empty; if (String.IsNullOrEmpty(blobName) || WildcardPattern.ContainsWildcardCharacters(blobName)) { container = GetCloudBlobContainerByName(containerName); prefix = NameUtil.GetNonWildcardPrefix(blobName); WildcardOptions options = WildcardOptions.IgnoreCase | WildcardOptions.Compiled; WildcardPattern wildcard = null; if (!String.IsNullOrEmpty(blobName)) { wildcard = new WildcardPattern(blobName, options); } Func <ICloudBlob, bool> blobFilter = (blob) => wildcard == null || wildcard.IsMatch(blob.Name); IEnumerable <Tuple <ICloudBlob, BlobContinuationToken> > blobs = ListBlobsByPrefix(containerName, prefix, blobFilter); foreach (var blobInfo in blobs) { yield return(blobInfo); } } else { container = GetCloudBlobContainerByName(containerName, true); if (!NameUtil.IsValidBlobName(blobName)) { throw new ArgumentException(String.Format(Resources.InvalidBlobName, blobName)); } ICloudBlob blob = Channel.GetBlobReferenceFromServer(container, blobName, accessCondition, requestOptions, OperationContext); if (null == blob) { throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, containerName)); } else { yield return(new Tuple <ICloudBlob, BlobContinuationToken>(blob, null)); } } }
/// <summary> /// List containers by container name pattern. /// </summary> /// <param name="name">Container name pattern</param> /// <returns>An enumerable collection of cloudblob container</returns> internal IEnumerable <Tuple <AzureStorageContainer, BlobContinuationToken> > ListContainersByName(string name) { string prefix = string.Empty; if (String.IsNullOrEmpty(name) || WildcardPattern.ContainsWildcardCharacters(name)) { prefix = NameUtil.GetNonWildcardPrefix(name); WildcardOptions options = WildcardOptions.IgnoreCase | WildcardOptions.Compiled; WildcardPattern wildcard = null; if (!string.IsNullOrEmpty(name)) { wildcard = new WildcardPattern(name, options); } Func <string, bool> containerFilter = (containerName) => null == wildcard || wildcard.IsMatch(containerName); IEnumerable <Tuple <AzureStorageContainer, BlobContinuationToken> > containerList = ListContainersByPrefix(prefix, containerFilter); foreach (var containerInfo in containerList) { yield return(containerInfo); } } else { if (!NameUtil.IsValidContainerName(name)) { throw new ArgumentException(String.Format(Resources.InvalidContainerName, name)); } if (this.IncludeDeleted.IsPresent) { WriteWarning("Can't get single deleted container, so -IncludeDeleted will be omit when get single container with -Name."); } CloudBlobContainer container = Channel.GetContainerReference(name); BlobContainerClient containerClient = AzureStorageContainer.GetTrack2BlobContainerClient(container, this.Channel.StorageContext, ClientOptions); global::Azure.Storage.Blobs.Models.BlobContainerProperties properties = null; try { properties = containerClient.GetProperties(cancellationToken: this.CmdletCancellationToken); } catch (global::Azure.RequestFailedException e) when(e.Status == 404) { throw new ResourceNotFoundException(String.Format(Resources.ContainerNotFound, name)); } yield return(new Tuple <AzureStorageContainer, BlobContinuationToken>(new AzureStorageContainer(containerClient, Channel.StorageContext, properties), null)); } }
/// <summary> /// List containers by container name pattern. /// </summary> /// <param name="name">Container name pattern</param> /// <returns>An enumerable collection of cloudblob container</returns> internal IEnumerable <Tuple <CloudBlobContainer, BlobContinuationToken> > ListContainersByName(string name) { string prefix = string.Empty; BlobRequestOptions requestOptions = RequestOptions; AccessCondition accessCondition = null; if (String.IsNullOrEmpty(name) || WildcardPattern.ContainsWildcardCharacters(name)) { prefix = NameUtil.GetNonWildcardPrefix(name); WildcardOptions options = WildcardOptions.IgnoreCase | WildcardOptions.Compiled; WildcardPattern wildcard = null; if (!string.IsNullOrEmpty(name)) { wildcard = new WildcardPattern(name, options); } Func <CloudBlobContainer, bool> containerFilter = (container) => null == wildcard || wildcard.IsMatch(container.Name); IEnumerable <Tuple <CloudBlobContainer, BlobContinuationToken> > containerList = ListContainersByPrefix(prefix, containerFilter); foreach (var containerInfo in containerList) { yield return(containerInfo); } } else { if (!NameUtil.IsValidContainerName(name)) { throw new ArgumentException(String.Format(Resources.InvalidContainerName, name)); } CloudBlobContainer container = Channel.GetContainerReference(name); if (Channel.DoesContainerExist(container, requestOptions, OperationContext)) { //fetch container attributes Channel.FetchContainerAttributes(container, accessCondition, requestOptions, OperationContext); yield return(new Tuple <CloudBlobContainer, BlobContinuationToken>(container, null)); } else { throw new ResourceNotFoundException(String.Format(Resources.ContainerNotFound, name)); } } }
/// <summary> /// list blobs by blob name and container name /// </summary> /// <param name="containerName">container name</param> /// <param name="blobName">blob name pattern</param> /// <returns>An enumerable collection of IListBlobItem</returns> internal async Task ListBlobsByName(long taskId, IStorageBlobManagement localChannel, string containerName, string blobName, bool includeDeleted = false) { CloudBlobContainer container = null; BlobRequestOptions requestOptions = RequestOptions; AccessCondition accessCondition = null; string prefix = string.Empty; if (String.IsNullOrEmpty(blobName) || WildcardPattern.ContainsWildcardCharacters(blobName) || includeDeleted) { container = await GetCloudBlobContainerByName(localChannel, containerName).ConfigureAwait(false); prefix = NameUtil.GetNonWildcardPrefix(blobName); WildcardOptions options = WildcardOptions.IgnoreCase | WildcardOptions.Compiled; WildcardPattern wildcard = null; if (!String.IsNullOrEmpty(blobName)) { wildcard = new WildcardPattern(blobName, options); } Func <CloudBlob, bool> blobFilter = (blob) => wildcard == null || wildcard.IsMatch(blob.Name); await ListBlobsByPrefix(taskId, localChannel, containerName, prefix, blobFilter, includeDeleted).ConfigureAwait(false); } else { container = await GetCloudBlobContainerByName(localChannel, containerName, true).ConfigureAwait(false); if (!NameUtil.IsValidBlobName(blobName)) { throw new ArgumentException(String.Format(Resources.InvalidBlobName, blobName)); } CloudBlob blob = await localChannel.GetBlobReferenceFromServerAsync(container, blobName, accessCondition, requestOptions, OperationContext, CmdletCancellationToken).ConfigureAwait(false); if (null == blob) { throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, containerName)); } else { WriteCloudBlobObject(taskId, localChannel, blob); } } }
/// <summary> /// list blobs by blob name and container name /// </summary> /// <param name="containerName">container name</param> /// <param name="blobName">blob name pattern</param> /// <returns>An enumerable collection of IListBlobItem</returns> internal async Task ListBlobsByName(long taskId, IStorageBlobManagement localChannel, string containerName, string blobName, bool includeDeleted = false, bool includeVersion = false) { CloudBlobContainer container = null; BlobRequestOptions requestOptions = RequestOptions; AccessCondition accessCondition = null; string prefix = string.Empty; if (String.IsNullOrEmpty(blobName) || WildcardPattern.ContainsWildcardCharacters(blobName) || includeDeleted) { container = await GetCloudBlobContainerByName(localChannel, containerName).ConfigureAwait(false); prefix = NameUtil.GetNonWildcardPrefix(blobName); WildcardOptions options = WildcardOptions.IgnoreCase | WildcardOptions.Compiled; WildcardPattern wildcard = null; if (!String.IsNullOrEmpty(blobName)) { wildcard = new WildcardPattern(blobName, options); } Func <string, bool> blobFilter = (blobNameToFilte) => wildcard == null || wildcard.IsMatch(blobNameToFilte); await ListBlobsByPrefix(taskId, localChannel, containerName, prefix, blobFilter, includeDeleted, IncludeVersion).ConfigureAwait(false); } else { container = await GetCloudBlobContainerByName(localChannel, containerName, true).ConfigureAwait(false); BlobContainerClient track2container = AzureStorageContainer.GetTrack2BlobContainerClient(container, localChannel.StorageContext, ClientOptions); if (!NameUtil.IsValidBlobName(blobName)) { throw new ArgumentException(String.Format(Resources.InvalidBlobName, blobName)); } BlobBaseClient blobClient = null; if (UseTrack2Sdk()) // User Track2 SDK { blobClient = Util.GetTrack2BlobClient(track2container, blobName, localChannel.StorageContext, this.VersionId, false, this.SnapshotTime is null ? null : this.SnapshotTime.Value.ToString("o"), ClientOptions); global::Azure.Storage.Blobs.Models.BlobProperties blobProperties; try { blobProperties = blobClient.GetProperties(BlobRequestConditions, cancellationToken: CmdletCancellationToken); } catch (global::Azure.RequestFailedException e) when(e.Status == 404) { throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, containerName)); } blobClient = Util.GetTrack2BlobClient(track2container, blobName, localChannel.StorageContext, this.VersionId, blobProperties.IsLatestVersion, this.SnapshotTime is null ? null : this.SnapshotTime.Value.ToString("o"), ClientOptions, blobProperties.BlobType); AzureStorageBlob outputBlob = new AzureStorageBlob(blobClient, localChannel.StorageContext, blobProperties, ClientOptions); OutputStream.WriteObject(taskId, outputBlob); } else // Use Track1 SDK { CloudBlob blob = await localChannel.GetBlobReferenceFromServerAsync(container, blobName, this.SnapshotTime, accessCondition, requestOptions, OperationContext, CmdletCancellationToken).ConfigureAwait(false); if (null == blob) { throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, containerName)); } else { OutputStream.WriteObject(taskId, new AzureStorageBlob(blob, localChannel.StorageContext, ClientOptions)); } } } }