/// <summary> /// remove azure blob /// </summary> /// <param name="container">CloudBlobContainer object</param> /// <param name="blobName">blob name</param> /// <returns>true if the blob is removed successfully, false if user cancel the remove operation</returns> internal async Task RemoveAzureBlob(long taskId, IStorageBlobManagement localChannel, CloudBlobContainer container, string blobName) { if (!NameUtil.IsValidBlobName(blobName)) { throw new ArgumentException(String.Format(Resources.InvalidBlobName, blobName)); } ValidatePipelineCloudBlobContainer(container); AccessCondition accessCondition = null; BlobRequestOptions requestOptions = null; ICloudBlob blob = await localChannel.GetBlobReferenceFromServerAsync(container, blobName, accessCondition, requestOptions, OperationContext, CmdletCancellationToken); if (null == blob && container.ServiceClient.Credentials.IsSharedKey) { throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, container.Name)); } else { //Construct the blob as CloudBlockBlob no matter what's the real blob type //We can't get the blob type if Credentials only have the delete permission and don't have read permission. blob = container.GetBlockBlobReference(blobName); } await RemoveAzureBlob(taskId, localChannel, blob, true); }
/// <summary> /// remove azure blob /// </summary> /// <param name="container">CloudBlobContainer object</param> /// <param name="blobName">blob name</param> /// <returns>true if the blob is removed successfully, false if user cancel the remove operation</returns> internal async Task RemoveAzureBlob(long taskId, IStorageBlobManagement localChannel, CloudBlobContainer container, string blobName) { if (!NameUtil.IsValidBlobName(blobName)) { throw new ArgumentException(String.Format(Resources.InvalidBlobName, blobName)); } ValidatePipelineCloudBlobContainer(container); if (!UseTrack2Sdk()) { AccessCondition accessCondition = null; BlobRequestOptions requestOptions = null; CloudBlob blob = null; try { blob = await localChannel.GetBlobReferenceFromServerAsync(container, blobName, this.SnapshotTime, accessCondition, requestOptions, OperationContext, CmdletCancellationToken).ConfigureAwait(false); } catch (InvalidOperationException) { blob = null; } if (null == blob && container.ServiceClient.Credentials.IsSharedKey) { throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, container.Name)); } else { //Construct the blob as CloudBlockBlob no matter what's the real blob type //We can't get the blob type if Credentials only have the delete permission and don't have read permission. blob = container.GetBlockBlobReference(blobName, this.SnapshotTime); } await RemoveAzureBlob(taskId, localChannel, blob, true).ConfigureAwait(false); } else { if (this.VersionId != null & this.SnapshotTime != null) { throw new ArgumentException("Can't input VersionId and SnapshotTime, since a blob can't have both."); } BlobContainerClient track2container = AzureStorageContainer.GetTrack2BlobContainerClient(container, localChannel.StorageContext, ClientOptions); BlobBaseClient blobClient = Util.GetTrack2BlobClient(track2container, blobName, localChannel.StorageContext, this.VersionId, null, this.SnapshotTime is null? null : this.SnapshotTime.Value.ToString("o"), ClientOptions); // Skip check blob existance, as Server will report error is necessary await RemoveAzureBlobTrack2(taskId, localChannel, blobClient, true).ConfigureAwait(false); } }
/// <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) { CloudBlobContainer container = null; BlobRequestOptions requestOptions = RequestOptions; AccessCondition accessCondition = null; string prefix = string.Empty; if (String.IsNullOrEmpty(blobName) || WildcardPattern.ContainsWildcardCharacters(blobName)) { container = await GetCloudBlobContainerByName(localChannel, containerName); 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); } else { container = await GetCloudBlobContainerByName(localChannel, containerName, true); if (!NameUtil.IsValidBlobName(blobName)) { throw new ArgumentException(String.Format(Resources.InvalidBlobName, blobName)); } CloudBlob blob = await localChannel.GetBlobReferenceFromServerAsync(container, blobName, accessCondition, requestOptions, OperationContext, CmdletCancellationToken); 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)); } } } }