PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/jslee1/azure-powershell
C# | 221 lines | 127 code | 24 blank | 70 comment | 19 complexity | 72f8aaedb5a613ec88f312498b999e9b MD5 | raw file
  1. // ----------------------------------------------------------------------------------
  2. //
  3. // Copyright Microsoft Corporation
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // ----------------------------------------------------------------------------------
  14. namespace Microsoft.WindowsAzure.Commands.Storage
  15. {
  16. using Commands.Common.Storage.ResourceModel;
  17. using Microsoft.WindowsAzure.Commands.Common.Storage;
  18. using Microsoft.WindowsAzure.Commands.Storage.Common;
  19. using Microsoft.WindowsAzure.Commands.Storage.Model.Contract;
  20. using Microsoft.WindowsAzure.Storage;
  21. using Microsoft.WindowsAzure.Storage.Blob;
  22. using System;
  23. using System.Globalization;
  24. /// <summary>
  25. /// Base cmdlet for storage blob/container cmdlet
  26. /// </summary>
  27. public class StorageCloudBlobCmdletBase : StorageCloudCmdletBase<IStorageBlobManagement>
  28. {
  29. /// <summary>
  30. /// Initializes a new instance of the StorageCloudBlobCmdletBase class.
  31. /// </summary>
  32. public StorageCloudBlobCmdletBase()
  33. : this(null)
  34. {
  35. }
  36. /// <summary>
  37. /// Initializes a new instance of the StorageCloudBlobCmdletBase class.
  38. /// </summary>
  39. /// <param name="channel">IStorageBlobManagement channel</param>
  40. public StorageCloudBlobCmdletBase(IStorageBlobManagement channel)
  41. {
  42. Channel = channel;
  43. }
  44. /// <summary>
  45. /// Blob request options
  46. /// </summary>
  47. public BlobRequestOptions RequestOptions
  48. {
  49. get
  50. {
  51. return (BlobRequestOptions)GetRequestOptions(StorageServiceType.Blob);
  52. }
  53. }
  54. protected static CloudBlob GetBlobReferenceFromServerWithContainer(
  55. IStorageBlobManagement localChannel,
  56. CloudBlobContainer container,
  57. string blobName,
  58. AccessCondition accessCondition = null,
  59. BlobRequestOptions requestOptions = null,
  60. OperationContext operationContext = null)
  61. {
  62. return GetBlobReferenceWrapper(() =>
  63. {
  64. try
  65. {
  66. return localChannel.GetBlobReferenceFromServer(container, blobName, accessCondition, requestOptions, operationContext);
  67. }
  68. catch (InvalidOperationException)
  69. {
  70. return null;
  71. }
  72. },
  73. blobName,
  74. container.Name);
  75. }
  76. protected static CloudBlob GetBlobReferenceWrapper(Func<CloudBlob> getBlobReference, string blobName, string containerName)
  77. {
  78. CloudBlob blob = getBlobReference();
  79. if (null == blob)
  80. {
  81. throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, containerName));
  82. }
  83. return blob;
  84. }
  85. /// <summary>
  86. /// Make sure the pipeline blob is valid and already existing
  87. /// </summary>
  88. /// <param name="blob">CloudBlob object</param>
  89. internal void ValidatePipelineCloudBlob(CloudBlob blob)
  90. {
  91. if (null == blob)
  92. {
  93. throw new ArgumentException(String.Format(Resources.ObjectCannotBeNull, typeof(CloudBlob).Name));
  94. }
  95. if (!NameUtil.IsValidBlobName(blob.Name))
  96. {
  97. throw new ArgumentException(String.Format(Resources.InvalidBlobName, blob.Name));
  98. }
  99. ValidatePipelineCloudBlobContainer(blob.Container);
  100. //BlobRequestOptions requestOptions = RequestOptions;
  101. //if (!Channel.DoesBlobExist(blob, requestOptions, OperationContext))
  102. //{
  103. // throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blob.Name, blob.Container.Name));
  104. //}
  105. }
  106. /// <summary>
  107. /// Make sure the container is valid and already existing
  108. /// </summary>
  109. /// <param name="container">A CloudBlobContainer object</param>
  110. internal void ValidatePipelineCloudBlobContainer(CloudBlobContainer container)
  111. {
  112. if (null == container)
  113. {
  114. throw new ArgumentException(String.Format(Resources.ObjectCannotBeNull, typeof(CloudBlobContainer).Name));
  115. }
  116. if (!NameUtil.IsValidContainerName(container.Name))
  117. {
  118. throw new ArgumentException(String.Format(Resources.InvalidContainerName, container.Name));
  119. }
  120. //BlobRequestOptions requestOptions = RequestOptions;
  121. //if (container.ServiceClient.Credentials.IsSharedKey
  122. // && !Channel.DoesContainerExist(container, requestOptions, OperationContext))
  123. //{
  124. // throw new ResourceNotFoundException(String.Format(Resources.ContainerNotFound, container.Name));
  125. //}
  126. }
  127. /// <summary>
  128. /// Create blob client and storage service management channel if need to.
  129. /// </summary>
  130. /// <returns>IStorageManagement object</returns>
  131. protected override IStorageBlobManagement CreateChannel()
  132. {
  133. //Init storage blob management channel
  134. if (Channel == null || !ShareChannel)
  135. {
  136. Channel = new StorageBlobManagement(GetCmdletStorageContext());
  137. }
  138. return Channel;
  139. }
  140. /// <summary>
  141. /// Get a service channel object using specified storage account
  142. /// </summary>
  143. /// <param name="account">Cloud storage account object</param>
  144. /// <returns>IStorageBlobManagement channel object</returns>
  145. protected IStorageBlobManagement CreateChannel(AzureStorageContext context)
  146. {
  147. return new StorageBlobManagement(context);
  148. }
  149. /// <summary>
  150. /// whether the specified blob is a snapshot
  151. /// </summary>
  152. /// <param name="blob">CloudBlob object</param>
  153. /// <returns>true if the specified blob is snapshot, otherwise false</returns>
  154. internal bool IsSnapshot(CloudBlob blob)
  155. {
  156. return !string.IsNullOrEmpty(blob.Name) && blob.SnapshotTime != null;
  157. }
  158. /// <summary>
  159. /// Write CloudBlob to output using specified service channel
  160. /// </summary>
  161. /// <param name="blob">The output CloudBlob object</param>
  162. /// <param name="channel">IStorageBlobManagement channel object</param>
  163. internal void WriteCloudBlobObject(long taskId, IStorageBlobManagement channel, CloudBlob blob, BlobContinuationToken continuationToken = null)
  164. {
  165. AzureStorageBlob azureBlob = new AzureStorageBlob(blob);
  166. azureBlob.Context = channel.StorageContext;
  167. azureBlob.ContinuationToken = continuationToken;
  168. OutputStream.WriteObject(taskId, azureBlob);
  169. }
  170. /// <summary>
  171. /// Write CloudBlob to output using specified service channel
  172. /// </summary>
  173. /// <param name="blob">The output CloudBlob object</param>
  174. /// <param name="channel">IStorageBlobManagement channel object</param>
  175. internal void WriteCloudContainerObject(long taskId, IStorageBlobManagement channel,
  176. CloudBlobContainer container, BlobContainerPermissions permissions, BlobContinuationToken continuationToken = null)
  177. {
  178. AzureStorageContainer azureContainer = new AzureStorageContainer(container, permissions);
  179. azureContainer.Context = channel.StorageContext;
  180. azureContainer.ContinuationToken = continuationToken;
  181. OutputStream.WriteObject(taskId, azureContainer);
  182. }
  183. protected void ValidateBlobType(CloudBlob blob)
  184. {
  185. if ((BlobType.BlockBlob != blob.BlobType)
  186. && (BlobType.PageBlob != blob.BlobType)
  187. && (BlobType.AppendBlob != blob.BlobType))
  188. {
  189. throw new InvalidOperationException(string.Format(
  190. CultureInfo.CurrentCulture,
  191. Resources.InvalidBlobType,
  192. blob.BlobType,
  193. blob.Name));
  194. }
  195. }
  196. }
  197. }