Esempio n. 1
0
 /// <summary>
 /// Check whether the container name is valid. If not throw an exception
 /// </summary>
 /// <param name="name">Container name</param>
 protected void ValidateContainerName(string name)
 {
     if (!NameUtil.IsValidContainerName(name))
     {
         throw new ArgumentException(String.Format(Resources.InvalidContainerName, name));
     }
 }
Esempio n. 2
0
        /// <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, BlobContainerPublicAccessType accessLevel)
        {
            if (!NameUtil.IsValidContainerName(name))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, name));
            }

            BlobContainerPermissions permissions = new BlobContainerPermissions();

            permissions.PublicAccess = accessLevel;

            BlobRequestOptions requestOptions  = RequestOptions;
            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);
            }
        }
Esempio n. 3
0
        /// <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 async Task SetContainerAcl(long taskId, IStorageBlobManagement localChannel, string name, BlobContainerPublicAccessType accessLevel)
        {
            if (!NameUtil.IsValidContainerName(name))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, name));
            }

            BlobContainerPermissions permissions = new BlobContainerPermissions();

            permissions.PublicAccess = accessLevel;

            BlobRequestOptions requestOptions  = RequestOptions;
            AccessCondition    accessCondition = null;

            CloudBlobContainer container = localChannel.GetContainerReference(name);

            if (!await localChannel.DoesContainerExistAsync(container, requestOptions, OperationContext, CmdletCancellationToken).ConfigureAwait(false))
            {
                throw new ResourceNotFoundException(String.Format(Resources.ContainerNotFound, name));
            }

            await localChannel.SetContainerPermissionsAsync(container, permissions, accessCondition, requestOptions, OperationContext, CmdletCancellationToken).ConfigureAwait(false);

            if (PassThru)
            {
                WriteCloudContainerObject(taskId, localChannel, container, permissions);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// get blob content
        /// </summary>
        /// <param name="containerName">source container name</param>
        /// <param name="blobName">source blob name</param>
        /// <param name="fileName">file name</param>
        /// <returns>the downloaded AzureStorageBlob object</returns>
        internal void GetBlobContent(string containerName, string blobName, string fileName)
        {
            if (!NameUtil.IsValidBlobName(blobName))
            {
                throw new ArgumentException(String.Format(Resources.InvalidBlobName, blobName));
            }

            if (!NameUtil.IsValidContainerName(containerName))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, containerName));
            }

            CloudBlobContainer container       = Channel.GetContainerReference(containerName);
            BlobRequestOptions requestOptions  = RequestOptions;
            AccessCondition    accessCondition = null;

            ICloudBlob blob = Channel.GetBlobReferenceFromServer(container, blobName, accessCondition, requestOptions, OperationContext);

            if (null == blob)
            {
                throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, containerName));
            }

            GetBlobContent(blob, fileName, true);
        }
Esempio n. 5
0
        /// <summary>
        /// get the DataLakeFileSystemClient object by name if DataLakeFileSystem exists
        /// </summary>
        /// <param name="fileSystemName">DataLakeFileSystem name</param>
        /// <returns>return DataLakeFileSystemClient object if specified DataLakeFileSystem exists, otherwise throw an exception</returns>
        internal DataLakeFileSystemClient GetFileSystemClientByName(IStorageBlobManagement localChannel, string fileSystemName, bool skipCheckExists = false)
        {
            if (!NameUtil.IsValidContainerName(fileSystemName))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, fileSystemName));
            }

            Uri fileSystemUri = localChannel.StorageContext.StorageAccount.CreateCloudBlobClient().GetContainerReference(fileSystemName).Uri;
            DataLakeFileSystemClient fileSystem;

            if (localChannel.StorageContext.StorageAccount.Credentials.IsToken) //Oauth
            {
                fileSystem = new DataLakeFileSystemClient(fileSystemUri, localChannel.StorageContext.Track2OauthToken, this.DataLakeClientOptions);
            }
            else if (localChannel.StorageContext.StorageAccount.Credentials.IsSAS) //SAS
            {
                fileSystem = new DataLakeFileSystemClient(new Uri(fileSystemUri.ToString() + "?" + Util.GetSASStringWithoutQuestionMark(localChannel.StorageContext.StorageAccount.Credentials.SASToken)), this.DataLakeClientOptions);
            }
            else if (localChannel.StorageContext.StorageAccount.Credentials.IsSharedKey) //Shared Key
            {
                fileSystem = new DataLakeFileSystemClient(fileSystemUri,
                                                          new StorageSharedKeyCredential(localChannel.StorageContext.StorageAccountName, localChannel.StorageContext.StorageAccount.Credentials.ExportBase64EncodedKey()), this.DataLakeClientOptions);
            }
            else //Anonymous
            {
                fileSystem = new DataLakeFileSystemClient(fileSystemUri, this.DataLakeClientOptions);
            }

            return(fileSystem);
        }
        /// <summary>
        /// create a new azure container
        /// </summary>
        /// <param name="name">container name</param>
        internal AzureStorageContainer CreateAzureContainer(string name, BlobContainerPublicAccessType accesslevel)
        {
            if (!NameUtil.IsValidContainerName(name))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, name));
            }

            BlobRequestOptions requestOptions  = RequestOptions;
            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();

            permissions.PublicAccess = accessLevel;

            if (accessLevel == BlobContainerPublicAccessType.Container || accessLevel == BlobContainerPublicAccessType.Blob)
            {
                Channel.SetContainerPermissions(container, permissions, accessCondition, requestOptions, OperationContext);
            }
            else
            {
                permissions = Channel.GetContainerPermissions(container, accessCondition, requestOptions, OperationContext);
            }

            AzureStorageContainer azureContainer = new AzureStorageContainer(container, permissions);

            return(azureContainer);
        }
Esempio n. 7
0
        /// <summary>
        /// get blob content
        /// </summary>
        /// <param name="containerName">source container name</param>
        /// <param name="blobName">source blob name</param>
        /// <param name="fileName">file name</param>
        /// <returns>the downloaded AzureStorageBlob object</returns>
        internal void GetBlobContent(string containerName, string blobName, string fileName)
        {
            if (!NameUtil.IsValidContainerName(containerName))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, containerName));
            }

            CloudBlobContainer container = Channel.GetContainerReference(containerName);

            GetBlobContent(container, blobName, fileName);
        }
Esempio n. 8
0
        /// <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);
        }
Esempio n. 9
0
        /// <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 async Task SetContainerAcl(long taskId, IStorageBlobManagement localChannel, string name, BlobContainerPublicAccessType accessLevel)
        {
            if (!NameUtil.IsValidContainerName(name))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, name));
            }

            BlobRequestOptions requestOptions = RequestOptions;

            CloudBlobContainer  container       = localChannel.GetContainerReference(name);
            BlobContainerClient containerClient = AzureStorageContainer.GetTrack2BlobContainerClient(container, this.Channel.StorageContext, ClientOptions);

            // Get container permission and set the public access as input
            BlobContainerAccessPolicy accessPolicy;

            try
            {
                accessPolicy = containerClient.GetAccessPolicy(null, this.CmdletCancellationToken);
            }
            catch (global::Azure.RequestFailedException e) when(e.Status == 404)
            {
                throw new ResourceNotFoundException(String.Format(Resources.ContainerNotFound, name));
            }
            PublicAccessType publicAccessType = PublicAccessType.None;

            switch (accessLevel)
            {
            case BlobContainerPublicAccessType.Blob:
                publicAccessType = PublicAccessType.Blob;
                break;

            case BlobContainerPublicAccessType.Container:
                publicAccessType = PublicAccessType.BlobContainer;
                break;

            case BlobContainerPublicAccessType.Off:
                publicAccessType = PublicAccessType.None;
                break;

            default:
            case BlobContainerPublicAccessType.Unknown:
                throw new ArgumentOutOfRangeException("Permission");
            }
            await containerClient.SetAccessPolicyAsync(publicAccessType, accessPolicy.SignedIdentifiers, null, this.CmdletCancellationToken).ConfigureAwait(false);

            if (PassThru)
            {
                AzureStorageContainer storageContainer = new AzureStorageContainer(containerClient, Channel.StorageContext);
                storageContainer.SetTrack2Permission();
                OutputStream.WriteObject(taskId, storageContainer);
            }
        }
        /// <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>
        /// create a new azure container
        /// </summary>
        /// <param name="name">container name</param>
        internal async Task CreateAzureContainer(long taskId, IStorageBlobManagement localChannel, string name, BlobContainerPublicAccessType accesslevel)
        {
            if (!NameUtil.IsValidContainerName(name))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, name));
            }

            BlobRequestOptions  requestOptions  = RequestOptions;
            CloudBlobContainer  container       = localChannel.GetContainerReference(name);
            BlobContainerClient containerClient = AzureStorageContainer.GetTrack2BlobContainerClient(container, localChannel.StorageContext);

            PublicAccessType containerPublicAccess = PublicAccessType.None;

            if (accesslevel == BlobContainerPublicAccessType.Blob)
            {
                containerPublicAccess = PublicAccessType.Blob;
            }
            else if (accesslevel == BlobContainerPublicAccessType.Container)
            {
                containerPublicAccess = PublicAccessType.BlobContainer;
            }

            global::Azure.Storage.Blobs.Models.BlobContainerEncryptionScopeOptions encryptionScopeOption = null;
            if (this.DefaultEncryptionScope != null)
            {
                encryptionScopeOption = new global::Azure.Storage.Blobs.Models.BlobContainerEncryptionScopeOptions()
                {
                    // parameterset can ensure the 2 parameters must be set together.
                    DefaultEncryptionScope         = this.DefaultEncryptionScope,
                    PreventEncryptionScopeOverride = this.preventEncryptionScopeOverride.Value
                };
            }

            Response <BlobContainerInfo> responds = await containerClient.CreateIfNotExistsAsync(containerPublicAccess, null, encryptionScopeOption, CmdletCancellationToken).ConfigureAwait(false);

            if (responds == null || responds.Value == null) // Container already exist so not created again
            {
                throw new ResourceAlreadyExistException(String.Format(Resources.ContainerAlreadyExists, name));
            }

            BlobContainerPermissions permissions = new BlobContainerPermissions()
            {
                PublicAccess = accesslevel
            };

            container.FetchAttributes();
            WriteCloudContainerObject(taskId, localChannel, container, permissions);
        }
Esempio n. 12
0
        /// <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 <CloudBlobContainer> ListContainersByName(string name)
        {
            ContainerListingDetails details    = ContainerListingDetails.Metadata;
            string             prefix          = string.Empty;
            BlobRequestOptions requestOptions  = null;
            AccessCondition    accessCondition = null;

            if (String.IsNullOrEmpty(name) || WildcardPattern.ContainsWildcardCharacters(name))
            {
                IEnumerable <CloudBlobContainer> containers = Channel.ListContainers(prefix, details, requestOptions, OperationContext);
                WildcardOptions options  = WildcardOptions.IgnoreCase | WildcardOptions.Compiled;
                WildcardPattern wildcard = null;

                if (!string.IsNullOrEmpty(name))
                {
                    wildcard = new WildcardPattern(name, options);
                }

                foreach (CloudBlobContainer container in containers)
                {
                    if (null == wildcard || wildcard.IsMatch(container.Name))
                    {
                        yield return(container);
                    }
                }
            }
            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(container);
                }
                else
                {
                    throw new ResourceNotFoundException(String.Format(Resources.ContainerNotFound, name));
                }
            }
        }
        /// <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));
                }
            }
        }
Esempio n. 14
0
        /// <summary>
        /// Make sure the pipeline blob is valid and already existing
        /// </summary>
        /// <param name="blob">CloudBlob object</param>
        internal void ValidatePipelineCloudBlobTrack2(BlobBaseClient blob)
        {
            if (null == blob)
            {
                throw new ArgumentException(String.Format(Resources.ObjectCannotBeNull, typeof(CloudBlob).Name));
            }

            if (!NameUtil.IsValidBlobName(blob.Name))
            {
                throw new ArgumentException(String.Format(Resources.InvalidBlobName, blob.Name));
            }

            if (!NameUtil.IsValidContainerName(blob.BlobContainerName))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, blob.BlobContainerName));
            }
        }
Esempio n. 15
0
        /// <summary>
        /// get the CloudBlobContianer object by name if container exists
        /// </summary>
        /// <param name="containerName">container name</param>
        /// <returns>return CloudBlobContianer object if specified container exists, otherwise throw an exception</returns>
        internal CloudBlobContainer GetCloudBlobContainerByName(string containerName, bool skipCheckExists = false)
        {
            if (!NameUtil.IsValidContainerName(containerName))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, containerName));
            }

            BlobRequestOptions requestOptions = null;
            CloudBlobContainer container      = Channel.GetContainerReference(containerName);

            if (!skipCheckExists && !Channel.DoesContainerExist(container, requestOptions, OperationContext))
            {
                throw new ArgumentException(String.Format(Resources.ContainerNotFound, containerName));
            }

            return(container);
        }
Esempio n. 16
0
        /// <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);
            }
        }
        /// <summary>
        /// get the CloudBlobContainer object by name if container exists
        /// </summary>
        /// <param name="containerName">container name</param>
        /// <returns>return CloudBlobContianer object if specified container exists, otherwise throw an exception</returns>
        internal async Task <CloudBlobContainer> GetCloudBlobContainerByName(IStorageBlobManagement localChannel, string containerName, bool skipCheckExists = false)
        {
            if (!NameUtil.IsValidContainerName(containerName))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, containerName));
            }

            BlobRequestOptions requestOptions = RequestOptions;
            CloudBlobContainer container      = localChannel.GetContainerReference(containerName);

            if (!skipCheckExists && container.ServiceClient.Credentials.IsSharedKey &&
                !await localChannel.DoesContainerExistAsync(container, requestOptions, OperationContext, CmdletCancellationToken).ConfigureAwait(false))
            {
                throw new ArgumentException(String.Format(Resources.ContainerNotFound, containerName));
            }

            return(container);
        }
Esempio n. 18
0
        /// <summary>
        /// Make sure the container is valid and already existing
        /// </summary>
        /// <param name="container">A CloudBlobContainer object</param>
        internal void ValidatePipelineCloudBlobContainer(CloudBlobContainer container)
        {
            if (null == container)
            {
                throw new ArgumentException(String.Format(Resources.ObjectCannotBeNull, typeof(CloudBlobContainer).Name));
            }

            if (!NameUtil.IsValidContainerName(container.Name))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, container.Name));
            }

            BlobRequestOptions requestOptions = null;

            if (!Channel.DoesContainerExist(container, requestOptions, OperationContext))
            {
                throw new ResourceNotFoundException(String.Format(Resources.ContainerNotFound, container.Name));
            }
        }
Esempio n. 19
0
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(this.Name, "Restore deleted container"))
            {
                if (!NameUtil.IsValidContainerName(this.Name))
                {
                    throw new ArgumentException(String.Format(Resources.InvalidContainerName, this.Name));
                }

                BlobServiceClient blobServiceClient = Util.GetTrack2BlobServiceClient(this.Channel.StorageContext, ClientOptions);

                BlobContainerClient destContainerClient = blobServiceClient.UndeleteBlobContainer(this.Name, this.VersionId, this.Name, this.CmdletCancellationToken).Value;

                AzureStorageContainer destAzureStorageContainer = new AzureStorageContainer(destContainerClient, Channel.StorageContext);
                destAzureStorageContainer.SetTrack2Permission();

                WriteObject(destAzureStorageContainer);
            }
        }
Esempio n. 20
0
        /// <summary>
        /// Make sure the container is valid and already existing
        /// </summary>
        /// <param name="container">A CloudBlobContainer object</param>
        internal void ValidatePipelineCloudBlobContainer(CloudBlobContainer container)
        {
            if (null == container)
            {
                throw new ArgumentException(String.Format(Resources.ObjectCannotBeNull, typeof(CloudBlobContainer).Name));
            }

            if (!NameUtil.IsValidContainerName(container.Name))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, container.Name));
            }

            //BlobRequestOptions requestOptions = RequestOptions;

            //if (container.ServiceClient.Credentials.IsSharedKey
            //    && !Channel.DoesContainerExist(container, requestOptions, OperationContext))
            //{
            //    throw new ResourceNotFoundException(String.Format(Resources.ContainerNotFound, container.Name));
            //}
        }
Esempio n. 21
0
        /// <summary>
        /// remove azure container by container name
        /// </summary>
        /// <param name="name">container name</param>
        internal async Task RemoveAzureContainer(long taskId, IStorageBlobManagement localChannel, string name)
        {
            if (!NameUtil.IsValidContainerName(name))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, name));
            }

            BlobRequestOptions requestOptions  = RequestOptions;
            AccessCondition    accessCondition = null;

            CloudBlobContainer container = localChannel.GetContainerReference(name);

            if (!await localChannel.DoesContainerExistAsync(container, requestOptions, OperationContext, CmdletCancellationToken).ConfigureAwait(false))
            {
                throw new ResourceNotFoundException(String.Format(Resources.ContainerNotFound, name));
            }

            string result  = string.Empty;
            bool   removed = false;

            if (force || ContainerIsEmpty(container) || OutputStream.ConfirmAsync(String.Format("Remove container and all content in it: {0}", name)).Result)
            {
                await localChannel.DeleteContainerAsync(container, accessCondition, requestOptions, OperationContext, CmdletCancellationToken).ConfigureAwait(false);

                result  = String.Format(Resources.RemoveContainerSuccessfully, name);
                removed = true;
            }
            else
            {
                result = String.Format(Resources.RemoveContainerCancelled, name);
            }

            OutputStream.WriteVerbose(taskId, result);

            if (PassThru)
            {
                OutputStream.WriteObject(taskId, removed);
            }
        }
        /// <summary>
        /// remove azure container by container name
        /// </summary>
        /// <param name="name">container name</param>
        internal void RemoveAzureContainer(string name)
        {
            if (!NameUtil.IsValidContainerName(name))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, name));
            }

            BlobRequestOptions requestOptions  = RequestOptions;
            AccessCondition    accessCondition = null;

            CloudBlobContainer container = Channel.GetContainerReference(name);

            if (!Channel.DoesContainerExist(container, requestOptions, OperationContext))
            {
                throw new ResourceNotFoundException(String.Format(Resources.ContainerNotFound, name));
            }

            string result  = string.Empty;
            bool   removed = false;

            if (force || ConfirmRemove(name))
            {
                Channel.DeleteContainer(container, accessCondition, requestOptions, OperationContext);
                result  = String.Format(Resources.RemoveContainerSuccessfully, name);
                removed = true;
            }
            else
            {
                result = String.Format(Resources.RemoveContainerCancelled, name);
            }

            WriteVerbose(result);

            if (PassThru)
            {
                WriteObject(removed);
            }
        }
Esempio n. 23
0
        /// <summary>
        /// create a new azure container
        /// </summary>
        /// <param name="name">container name</param>
        internal async Task CreateAzureContainer(long taskId, IStorageBlobManagement localChannel, string name, BlobContainerPublicAccessType accesslevel)
        {
            if (!NameUtil.IsValidContainerName(name))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, name));
            }

            BlobRequestOptions requestOptions = RequestOptions;
            CloudBlobContainer container      = localChannel.GetContainerReference(name);

            BlobContainerPermissions permissions = new BlobContainerPermissions();

            permissions.PublicAccess = accesslevel;

            bool created = await localChannel.CreateContainerIfNotExistsAsync(container, permissions.PublicAccess, requestOptions, OperationContext, CmdletCancellationToken);

            if (!created)
            {
                throw new ResourceAlreadyExistException(String.Format(Resources.ContainerAlreadyExists, name));
            }

            WriteCloudContainerObject(taskId, localChannel, container, permissions);
        }
        /// <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 async Task SetContainerAcl(long taskId, IStorageBlobManagement localChannel, string name, BlobContainerPublicAccessType accessLevel)
        {
            if (!NameUtil.IsValidContainerName(name))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, name));
            }

            BlobRequestOptions requestOptions  = RequestOptions;
            AccessCondition    accessCondition = null;
            bool needUseTrack2 = false;

            CloudBlobContainer container = localChannel.GetContainerReference(name);

            // Get container permission and set the public access as input
            BlobContainerPermissions permissions = null;

            try
            {
                permissions = localChannel.GetContainerPermissions(container, null, requestOptions, OperationContext);
            }
            catch (StorageException e) when(e.IsNotFoundException())
            {
                throw new ResourceNotFoundException(String.Format(Resources.ContainerNotFound, name));
            }
            catch (StorageException e) when(e.IsConflictException())
            {
                // 409 Conflict, might caused by the container has an Stored access policy contains a permission that is not supported by Track1 SDK API veresion, so switch to Track2 SDK
                needUseTrack2 = true;
            }

            if (!needUseTrack2) // Track1
            {
                permissions.PublicAccess = accessLevel;

                await localChannel.SetContainerPermissionsAsync(container, permissions, accessCondition, requestOptions, OperationContext, CmdletCancellationToken).ConfigureAwait(false);

                if (PassThru)
                {
                    WriteCloudContainerObject(taskId, localChannel, container, permissions);
                }
            }
            else // Track2
            {
                BlobContainerClient containerClient = AzureStorageContainer.GetTrack2BlobContainerClient(container, this.Channel.StorageContext, ClientOptions);

                // Get container permission and set the public access as input
                BlobContainerAccessPolicy accessPolicy;

                accessPolicy = containerClient.GetAccessPolicy(null, this.CmdletCancellationToken);

                PublicAccessType publicAccessType = PublicAccessType.None;
                switch (accessLevel)
                {
                case BlobContainerPublicAccessType.Blob:
                    publicAccessType = PublicAccessType.Blob;
                    break;

                case BlobContainerPublicAccessType.Container:
                    publicAccessType = PublicAccessType.BlobContainer;
                    break;

                case BlobContainerPublicAccessType.Off:
                    publicAccessType = PublicAccessType.None;
                    break;

                default:
                case BlobContainerPublicAccessType.Unknown:
                    throw new ArgumentOutOfRangeException("Permission");
                }
                await containerClient.SetAccessPolicyAsync(publicAccessType, accessPolicy.SignedIdentifiers, null, this.CmdletCancellationToken).ConfigureAwait(false);

                if (PassThru)
                {
                    AzureStorageContainer storageContainer = new AzureStorageContainer(container, null);
                    storageContainer.Context = localChannel.StorageContext;
                    storageContainer.SetTrack2Permission();
                    OutputStream.WriteObject(taskId, storageContainer);
                }
            }
        }