/// <summary> /// Pack CloudBlobContainer and it's permission to AzureStorageContainer object /// </summary> /// <param name="containerList">An enumerable collection of CloudBlobContainer</param> /// <returns>An enumerable collection of AzureStorageContainer</returns> internal IEnumerable<AzureStorageContainer> PackCloudBlobContainerWithAcl(IEnumerable<CloudBlobContainer> containerList) { if (null == containerList) { yield break; } BlobRequestOptions requestOptions = null; AccessCondition accessCondition = null; foreach (CloudBlobContainer container in containerList) { BlobContainerPermissions permissions = null; try { permissions = Channel.GetContainerPermissions(container, accessCondition, requestOptions, OperationContext); } catch (Exception e) { //Log the error message and continue the process WriteVerboseWithTimestamp(String.Format(Resources.GetContainerPermissionException, container.Name, e.Message)); } AzureStorageContainer azureContainer = new AzureStorageContainer(container, permissions); yield return azureContainer; } }
/// <summary> /// create a new azure container /// </summary> /// <param name="name">container name</param> internal AzureStorageContainer CreateAzureContainer(string name, string accesslevel) { if (!NameUtil.IsValidContainerName(name)) { throw new ArgumentException(String.Format(Resources.InvalidContainerName, name)); } BlobRequestOptions requestOptions = null; AccessCondition accessCondition = null; CloudBlobContainer container = Channel.GetContainerReference(name); bool created = Channel.CreateContainerIfNotExists(container, requestOptions, OperationContext); if (!created) { throw new ResourceAlreadyExistException(String.Format(Resources.ContainerAlreadyExists, name)); } BlobContainerPermissions permissions = new BlobContainerPermissions(); accessLevel = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(accessLevel); switch (CultureInfo.CurrentCulture.TextInfo.ToTitleCase(accessLevel)) { case StorageNouns.ContainerAclOff: permissions.PublicAccess = BlobContainerPublicAccessType.Off; break; case StorageNouns.ContainerAclBlob: permissions.PublicAccess = BlobContainerPublicAccessType.Blob; break; case StorageNouns.ContainerAclContainer: permissions.PublicAccess = BlobContainerPublicAccessType.Container; break; default: throw new ArgumentException(Resources.OnlyOnePermissionForContainer); } if(accessLevel == StorageNouns.ContainerAclContainer || accessLevel == StorageNouns.ContainerAclBlob) { Channel.SetContainerPermissions(container, permissions, accessCondition, requestOptions, OperationContext); } else { permissions = Channel.GetContainerPermissions(container, accessCondition, requestOptions, OperationContext); } AzureStorageContainer azureContainer = new AzureStorageContainer(container, permissions); return azureContainer; }
/// <summary> /// set the access level of specified container /// </summary> /// <param name="name">container name</param> /// <param name="accessLevel">access level in ("off", "blob", "container")</param> internal void SetContainerAcl(string name, string accessLevel) { if (!NameUtil.IsValidContainerName(name)) { throw new ArgumentException(String.Format(Resources.InvalidContainerName, name)); } BlobContainerPermissions permissions = new BlobContainerPermissions(); switch (CultureInfo.CurrentCulture.TextInfo.ToTitleCase(accessLevel)) { case StorageNouns.ContainerAclOff: permissions.PublicAccess = BlobContainerPublicAccessType.Off; break; case StorageNouns.ContainerAclBlob: permissions.PublicAccess = BlobContainerPublicAccessType.Blob; break; case StorageNouns.ContainerAclContainer: permissions.PublicAccess = BlobContainerPublicAccessType.Container; break; default: throw new ArgumentException(Resources.OnlyOnePermissionForContainer); } BlobRequestOptions requestOptions = null; AccessCondition accessCondition = null; CloudBlobContainer container = Channel.GetContainerReference(name); if (!Channel.DoesContainerExist(container, requestOptions, OperationContext)) { throw new ResourceNotFoundException(String.Format(Resources.ContainerNotFound, name)); } Channel.SetContainerPermissions(container, permissions, accessCondition, requestOptions, OperationContext); AzureStorageContainer azureContainer = new AzureStorageContainer(container, permissions); if (PassThru) { WriteObjectWithStorageContext(azureContainer); } }