Skip to content

Commit d40f705

Browse files
committed
WORK:3295833[PSH]Filter out append blob in listing, and throw exceptions when encounters append blob in other cmdlets.
1 parent 997fb11 commit d40f705

12 files changed

+131
-53
lines changed

src/ServiceManagement/Storage/Commands.Storage/Blob/Cmdlet/GetAzureStorageBlob.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,13 @@ internal async Task ListBlobsByPrefix(long taskId, IStorageBlobManagement localC
241241
continue;
242242
}
243243

244+
if ((BlobType.BlockBlob != blob.BlobType)
245+
&& (BlobType.PageBlob != blob.BlobType))
246+
{
247+
// Block append blob here.
248+
continue;
249+
}
250+
244251
if (blobFilter == null || blobFilter(blob))
245252
{
246253
WriteCloudBlobObject(taskId, localChannel, blob, blobResult.ContinuationToken);

src/ServiceManagement/Storage/Commands.Storage/Blob/Cmdlet/GetAzureStorageBlobContent.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,7 @@ internal void GetBlobContent(string containerName, string blobName, string fileN
174174
BlobRequestOptions requestOptions = RequestOptions;
175175
AccessCondition accessCondition = null;
176176

177-
CloudBlob blob = Channel.GetBlobReferenceFromServer(container, blobName, accessCondition, requestOptions, OperationContext);
178-
179-
if (null == blob)
180-
{
181-
throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, containerName));
182-
}
177+
CloudBlob blob = GetBlobReferenceFromServerWithContainer(Channel, container, blobName, accessCondition, requestOptions, OperationContext);
183178

184179
GetBlobContent(blob, fileName, true);
185180
}
@@ -203,12 +198,8 @@ internal void GetBlobContent(CloudBlobContainer container, string blobName, stri
203198
ValidatePipelineCloudBlobContainer(container);
204199
AccessCondition accessCondition = null;
205200
BlobRequestOptions requestOptions = RequestOptions;
206-
CloudBlob blob = Channel.GetBlobReferenceFromServer(container, blobName, accessCondition, requestOptions, OperationContext);
207201

208-
if (null == blob)
209-
{
210-
throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, container.Name));
211-
}
202+
CloudBlob blob = GetBlobReferenceFromServerWithContainer(Channel, container, blobName, accessCondition, requestOptions, OperationContext);
212203

213204
GetBlobContent(blob, filePath, true);
214205
}
@@ -227,6 +218,8 @@ internal void GetBlobContent(CloudBlob blob, string fileName, bool isValidBlob =
227218
throw new ArgumentNullException(typeof(CloudBlob).Name, String.Format(Resources.ObjectCannotBeNull, typeof(CloudBlob).Name));
228219
}
229220

221+
ValidateBlobType(blob);
222+
230223
//skip download the snapshot except the CloudBlob pipeline
231224
if (IsSnapshot(blob) && ParameterSetName != BlobParameterSet)
232225
{

src/ServiceManagement/Storage/Commands.Storage/Blob/Cmdlet/GetAzureStorageBlobCopyState.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,7 @@ private CloudBlob GetCloudBlobObject(CloudBlobContainer container, string blobNa
219219
ValidateBlobName(blobName);
220220
ValidateContainerName(container.Name);
221221

222-
CloudBlob blob = Channel.GetBlobReferenceFromServer(container, blobName, accessCondition, options, OperationContext);
223-
224-
if (blob == null)
225-
{
226-
throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, container.Name));
227-
}
222+
CloudBlob blob = GetBlobReferenceFromServerWithContainer(Channel, container, blobName, accessCondition, options, OperationContext);
228223

229224
return GetCloudBlobObject(blob);
230225
}
@@ -237,6 +232,9 @@ private CloudBlob GetCloudBlobObject(CloudBlobContainer container, string blobNa
237232
private CloudBlob GetCloudBlobObject(CloudBlob blob)
238233
{
239234
ValidateBlobName(blob.Name);
235+
236+
ValidateBlobType(blob);
237+
240238
return blob;
241239
}
242240

src/ServiceManagement/Storage/Commands.Storage/Blob/Cmdlet/NewAzureStorageBlobSasToken.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ public class NewAzureStorageBlobSasTokenCommand : StorageCloudBlobCmdletBase
4444
/// </summary>
4545
private const string BlobPipelineParameterSetWithPolicy = "BlobPipelineWithPolicy";
4646

47-
[Parameter(HelpMessage = "ICloudBlob Object", Mandatory = true,
47+
[Alias("ICloudBlob")]
48+
[Parameter(HelpMessage = "CloudBlob Object", Mandatory = true,
4849
ValueFromPipelineByPropertyName = true, ParameterSetName = BlobPipelineParameterSetWithPolicy)]
49-
[Parameter(HelpMessage = "ICloudBlob Object", Mandatory = true,
50+
[Parameter(HelpMessage = "CloudBlob Object", Mandatory = true,
5051
ValueFromPipelineByPropertyName = true, ParameterSetName = BlobPipelineParameterSetWithPermision)]
51-
public ICloudBlob ICloudBlob { get; set; }
52+
public CloudBlob CloudBlob { get; set; }
5253

5354
[Parameter(Position = 0, Mandatory = true, HelpMessage = "Container Name",
5455
ParameterSetName = BlobNamePipelineParmeterSetWithPermission)]
@@ -117,16 +118,16 @@ public NewAzureStorageBlobSasTokenCommand(IStorageBlobManagement channel)
117118
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
118119
public override void ExecuteCmdlet()
119120
{
120-
ICloudBlob blob = default(ICloudBlob);
121+
CloudBlob blob = null;
121122

122123
if (ParameterSetName == BlobNamePipelineParmeterSetWithPermission ||
123124
ParameterSetName == BlobNamePipelineParmeterSetWithPolicy)
124125
{
125-
blob = GetICloudBlobByName(Container, Blob);
126+
blob = GetCloudBlobByName(Container, Blob);
126127
}
127128
else
128129
{
129-
blob = ICloudBlob;
130+
blob = this.CloudBlob;
130131
}
131132

132133
SharedAccessBlobPolicy accessPolicy = new SharedAccessBlobPolicy();
@@ -149,11 +150,11 @@ public override void ExecuteCmdlet()
149150
/// <summary>
150151
/// Get blob shared access signature
151152
/// </summary>
152-
/// <param name="blob">ICloudBlob object</param>
153+
/// <param name="blob">CloudBlob object</param>
153154
/// <param name="accessPolicy">SharedAccessBlobPolicy object</param>
154155
/// <param name="policyIdentifier">The existing policy identifier.</param>
155156
/// <returns></returns>
156-
private string GetBlobSharedAccessSignature(ICloudBlob blob, SharedAccessBlobPolicy accessPolicy, string policyIdentifier)
157+
private string GetBlobSharedAccessSignature(CloudBlob blob, SharedAccessBlobPolicy accessPolicy, string policyIdentifier)
157158
{
158159
CloudBlobContainer container = blob.Container;
159160
string signature = String.Empty;
@@ -221,12 +222,12 @@ internal void SetupAccessPolicyPermission(SharedAccessBlobPolicy policy, string
221222
}
222223

223224
/// <summary>
224-
/// Get ICloudBlob object by name
225+
/// Get CloudBlob object by name
225226
/// </summary>
226227
/// <param name="ContainerName">Container name</param>
227228
/// <param name="BlobName">Blob name.</param>
228-
/// <returns>ICloudBlob object</returns>
229-
private ICloudBlob GetICloudBlobByName(string ContainerName, string BlobName)
229+
/// <returns>CloudBlob object</returns>
230+
private CloudBlob GetCloudBlobByName(string ContainerName, string BlobName)
230231
{
231232
CloudBlobContainer container = Channel.GetContainerReference(ContainerName);
232233
//Create a block blob object in local no mattter what's the real blob type. If so, we can save the unnecessary request calls.

src/ServiceManagement/Storage/Commands.Storage/Blob/Cmdlet/RemoveAzureStorageBlob.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ public RemoveStorageAzureBlobCommand(IStorageBlobManagement channel)
114114
/// <returns>true if the blob is removed successfully, false if user cancel the remove operation</returns>
115115
internal async Task RemoveAzureBlob(long taskId, IStorageBlobManagement localChannel, CloudBlob blob, bool isValidBlob)
116116
{
117+
ValidateBlobType(blob);
118+
117119
if (!isValidBlob)
118120
{
119121
ValidatePipelineCloudBlob(blob);
@@ -147,7 +149,7 @@ internal async Task RemoveAzureBlob(long taskId, IStorageBlobManagement localCha
147149

148150
try
149151
{
150-
await DeleteICloudAsync(taskId, localChannel, blob, deleteSnapshotsOption);
152+
await DeleteCloudAsync(taskId, localChannel, blob, deleteSnapshotsOption);
151153
retryDeleteSnapshot = false;
152154
}
153155
catch (StorageException e)
@@ -170,7 +172,7 @@ internal async Task RemoveAzureBlob(long taskId, IStorageBlobManagement localCha
170172
if (await OutputStream.ConfirmAsync(message))
171173
{
172174
deleteSnapshotsOption = DeleteSnapshotsOption.IncludeSnapshots;
173-
await DeleteICloudAsync(taskId, localChannel, blob, deleteSnapshotsOption);
175+
await DeleteCloudAsync(taskId, localChannel, blob, deleteSnapshotsOption);
174176
}
175177
else
176178
{
@@ -180,7 +182,7 @@ internal async Task RemoveAzureBlob(long taskId, IStorageBlobManagement localCha
180182
}
181183
}
182184

183-
internal async Task DeleteICloudAsync(long taskId, IStorageBlobManagement localChannel, CloudBlob blob, DeleteSnapshotsOption deleteSnapshotsOption)
185+
internal async Task DeleteCloudAsync(long taskId, IStorageBlobManagement localChannel, CloudBlob blob, DeleteSnapshotsOption deleteSnapshotsOption)
184186
{
185187
AccessCondition accessCondition = null;
186188
BlobRequestOptions requestOptions = null;
@@ -215,8 +217,17 @@ internal async Task RemoveAzureBlob(long taskId, IStorageBlobManagement localCha
215217
AccessCondition accessCondition = null;
216218
BlobRequestOptions requestOptions = null;
217219

218-
CloudBlob blob = await localChannel.GetBlobReferenceFromServerAsync(container, blobName, accessCondition,
219-
requestOptions, OperationContext, CmdletCancellationToken);
220+
CloudBlob blob = null;
221+
222+
try
223+
{
224+
blob = await localChannel.GetBlobReferenceFromServerAsync(container, blobName, accessCondition,
225+
requestOptions, OperationContext, CmdletCancellationToken);
226+
}
227+
catch (InvalidOperationException)
228+
{
229+
blob = null;
230+
}
220231

221232
if (null == blob && container.ServiceClient.Credentials.IsSharedKey)
222233
{

src/ServiceManagement/Storage/Commands.Storage/Blob/Cmdlet/StartAzureStorageBlobCopy.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ public override void ExecuteCmdlet()
257257
/// <returns>Destination CloudBlob object</returns>
258258
private void StartCopyBlob(IStorageBlobManagement destChannel, CloudBlob srcCloudBlob, CloudBlob destCloudBlob)
259259
{
260+
ValidateBlobType(srcCloudBlob);
261+
260262
Func<long, Task> taskGenerator = (taskId) => StartCopyInTransferManager(taskId, destChannel, srcCloudBlob, destCloudBlob);
261263
RunTask(taskGenerator);
262264
}
@@ -312,7 +314,22 @@ private void StartCopyBlob(IStorageBlobManagement destChannel, string srcUri, st
312314
if (sourceUri.Host.ToLower() == contextUri.Host.ToLower())
313315
{
314316
CloudBlobClient blobClient = context.StorageAccount.CreateCloudBlobClient();
315-
CloudBlob blobReference = Util.GetBlobReferenceFromServer(blobClient, sourceUri);
317+
CloudBlob blobReference = null;
318+
319+
try
320+
{
321+
blobReference = Util.GetBlobReferenceFromServer(blobClient, sourceUri);
322+
}
323+
catch (InvalidOperationException)
324+
{
325+
blobReference = null;
326+
}
327+
328+
if (null == blobReference)
329+
{
330+
throw new ResourceNotFoundException(String.Format(Resources.BlobUriNotFound, sourceUri.ToString()));
331+
}
332+
316333
StartCopyBlob(destChannel, blobReference, destContainer, destBlobName);
317334
}
318335
else
@@ -352,12 +369,7 @@ private void StartCopyBlob(IStorageBlobManagement SrcChannel, IStorageBlobManage
352369
AccessCondition accessCondition = null;
353370
BlobRequestOptions options = RequestOptions;
354371
CloudBlobContainer container = SrcChannel.GetContainerReference(srcContainerName);
355-
CloudBlob blob = SrcChannel.GetBlobReferenceFromServer(container, srcBlobName, accessCondition, options, OperationContext);
356-
357-
if (blob == null)
358-
{
359-
throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, srcBlobName, srcContainerName));
360-
}
372+
CloudBlob blob = GetBlobReferenceFromServerWithContainer(SrcChannel, container, srcBlobName, accessCondition, options, OperationContext);
361373

362374
this.StartCopyBlob(destChannel, blob, destContainerName, destBlobName);
363375
}

src/ServiceManagement/Storage/Commands.Storage/Blob/Cmdlet/StopAzureStorageBlobCopy.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,7 @@ private async Task StopCopyBlob(long taskId, IStorageBlobManagement localChannel
145145

146146
AccessCondition accessCondition = null;
147147
BlobRequestOptions options = RequestOptions;
148-
CloudBlob blob = localChannel.GetBlobReferenceFromServer(container, blobName, accessCondition, options, OperationContext);
149-
150-
if (blob == null)
151-
{
152-
throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, container.Name));
153-
}
148+
CloudBlob blob = GetBlobReferenceFromServerWithContainer(localChannel, container, blobName, accessCondition, options, OperationContext);
154149

155150
await StopCopyBlob(taskId, localChannel, blob, copyId);
156151
}
@@ -162,6 +157,8 @@ private async Task StopCopyBlob(long taskId, IStorageBlobManagement localChannel
162157
/// <param name="copyId">Copy id</param>
163158
private async Task StopCopyBlob(long taskId, IStorageBlobManagement localChannel, CloudBlob blob, string copyId, bool fetchCopyIdFromBlob = false)
164159
{
160+
ValidateBlobType(blob);
161+
165162
AccessCondition accessCondition = null;
166163
BlobRequestOptions abortRequestOption = RequestOptions ?? new BlobRequestOptions();
167164

src/ServiceManagement/Storage/Commands.Storage/Blob/StorageCloudBlobCmdletBase.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Microsoft.WindowsAzure.Commands.Storage
1919
using Microsoft.WindowsAzure.Commands.Storage.Common;
2020
using Microsoft.WindowsAzure.Commands.Storage.Model.Contract;
2121
using Microsoft.WindowsAzure.Commands.Storage.Model.ResourceModel;
22+
using Microsoft.WindowsAzure.Storage;
2223
using Microsoft.WindowsAzure.Storage.Blob;
2324

2425
/// <summary>
@@ -54,6 +55,41 @@ public BlobRequestOptions RequestOptions
5455
}
5556
}
5657

58+
protected static CloudBlob GetBlobReferenceFromServerWithContainer(
59+
IStorageBlobManagement localChannel,
60+
CloudBlobContainer container,
61+
string blobName,
62+
AccessCondition accessCondition = null,
63+
BlobRequestOptions requestOptions = null,
64+
OperationContext operationContext = null)
65+
{
66+
return GetBlobReferenceWrapper(() =>
67+
{
68+
try
69+
{
70+
return localChannel.GetBlobReferenceFromServer(container, blobName, accessCondition, requestOptions, operationContext);
71+
}
72+
catch (InvalidOperationException)
73+
{
74+
return null;
75+
}
76+
},
77+
blobName,
78+
container.Name);
79+
}
80+
81+
protected static CloudBlob GetBlobReferenceWrapper(Func<CloudBlob> getBlobReference, string blobName, string containerName)
82+
{
83+
CloudBlob blob = getBlobReference();
84+
85+
if (null == blob)
86+
{
87+
throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, containerName));
88+
}
89+
90+
return blob;
91+
}
92+
5793
/// <summary>
5894
/// Make sure the pipeline blob is valid and already existing
5995
/// </summary>
@@ -178,6 +214,15 @@ protected void ValidateBlobName(string name)
178214
}
179215
}
180216

217+
protected void ValidateBlobType(CloudBlob blob)
218+
{
219+
if ((BlobType.BlockBlob != blob.BlobType)
220+
&& (BlobType.PageBlob != blob.BlobType))
221+
{
222+
throw new InvalidOperationException(string.Format(Resources.InvalidBlobType, blob.Name));
223+
}
224+
}
225+
181226
/// <summary>
182227
/// Check whether the container name is valid. If not throw an exception
183228
/// </summary>

src/ServiceManagement/Storage/Commands.Storage/Common/Util.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Microsoft.WindowsAzure.Commands.Storage.Common
1616
{
1717
using System;
18+
using System.Globalization;
1819
using System.Net;
1920
using Microsoft.WindowsAzure.Storage;
2021
using Microsoft.WindowsAzure.Storage.Blob;
@@ -91,12 +92,11 @@ private static CloudBlob GetBlobReferenceFromServer(
9192
return null;
9293
}
9394

94-
return GetCorespondingTypeBlobReference(blob);
95+
return GetCorrespondingTypeBlobReference(blob);
9596
}
9697

97-
public static CloudBlob GetCorespondingTypeBlobReference(CloudBlob blob)
98+
public static CloudBlob GetCorrespondingTypeBlobReference(CloudBlob blob)
9899
{
99-
100100
if (BlobType.BlockBlob == blob.Properties.BlobType)
101101
{
102102
return new CloudBlockBlob(blob.SnapshotQualifiedUri, blob.ServiceClient.Credentials);
@@ -107,7 +107,7 @@ public static CloudBlob GetCorespondingTypeBlobReference(CloudBlob blob)
107107
return new CloudPageBlob(blob.SnapshotQualifiedUri, blob.ServiceClient.Credentials);
108108
}
109109

110-
throw new InvalidOperationException(string.Format("Not supported blob type: {0}", blob.Properties.BlobType));
110+
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidBlobType, blob.Name));
111111
}
112112
}
113113
}

src/ServiceManagement/Storage/Commands.Storage/Model/Contract/StorageBlobManagement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public async Task<CloudBlob> GetBlobReferenceFromServerAsync(CloudBlobContainer
401401
CloudBlob blob = container.GetBlobReference(blobName);
402402
await blob.FetchAttributesAsync(accessCondition, options, operationContext, cancellationToken);
403403

404-
return Util.GetCorespondingTypeBlobReference(blob);
404+
return Util.GetCorrespondingTypeBlobReference(blob);
405405
}
406406
catch (StorageException e)
407407
{

src/ServiceManagement/Storage/Commands.Storage/Resources.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)