public override void ExecuteCmdlet()
        {
            if (String.IsNullOrEmpty(ShareName) || String.IsNullOrEmpty(Policy))
            {
                return;
            }

            NamingUtil.ValidateShareName(this.ShareName, false);

            if (!NameUtil.IsValidStoredAccessPolicyName(this.Policy))
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.InvalidAccessPolicyName, this.Policy));
            }

            //Get existing permissions
            CloudFileShare fileShare = this.Channel.GetShareReference(this.ShareName);

            FileSharePermissions fileSharePermissions = fileShare.GetPermissions();

            //Add new policy
            if (fileSharePermissions.SharedAccessPolicies.Keys.Contains(this.Policy))
            {
                throw new ResourceAlreadyExistException(String.Format(CultureInfo.CurrentCulture, Resources.PolicyAlreadyExists, this.Policy));
            }

            SharedAccessFilePolicy policy = new SharedAccessFilePolicy();

            AccessPolicyHelper.SetupAccessPolicy <SharedAccessFilePolicy>(policy, this.StartTime, this.ExpiryTime, this.Permission);
            fileSharePermissions.SharedAccessPolicies.Add(this.Policy, policy);

            //Set permissions back to container
            fileShare.SetPermissions(fileSharePermissions);
            WriteObject(Policy);
        }
Exemplo n.º 2
0
        private void StartCopyFromFile(IStorageFileManagement srcChannel, IStorageBlobManagement destChannel, string srcShareName, string srcFilePath, string destContainerName, string destBlobName)
        {
            NamingUtil.ValidateShareName(srcShareName, false);
            CloudFileShare share = srcChannel.GetShareReference(srcShareName);

            this.StartCopyFromFile(destChannel, share.GetRootDirectoryReference(), srcFilePath, destContainerName, destBlobName);
        }
        public override void ExecuteCmdlet()
        {
            this.RunTask(async taskId =>
            {
                switch (this.ParameterSetName)
                {
                case Constants.SpecificParameterSetName:
                    NamingUtil.ValidateShareName(this.Name, false);
                    var share = this.Channel.GetShareReference(this.Name, this.SnapshotTime);
                    await this.Channel.FetchShareAttributesAsync(share, null, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken).ConfigureAwait(false);
                    this.OutputStream.WriteObject(taskId, share);

                    break;

                case Constants.MatchingPrefixParameterSetName:
                    NamingUtil.ValidateShareName(this.Prefix, true);
                    await this.Channel.EnumerateSharesAsync(
                        this.Prefix,
                        ShareListingDetails.All,
                        item => this.OutputStream.WriteObject(taskId, item),
                        this.RequestOptions,
                        this.OperationContext,
                        this.CmdletCancellationToken).ConfigureAwait(false);

                    break;

                default:
                    throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName));
                }
            });
        }
Exemplo n.º 4
0
        public override void ExecuteCmdlet()
        {
            NamingUtil.ValidateShareName(this.Name, false);

            var share = this.Channel.GetShareReference(this.Name);

            this.RunTask(async taskId =>
            {
                await this.Channel.CreateShareAsync(share, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken).ConfigureAwait(false);
                this.OutputStream.WriteObject(taskId, share);
            });
        }
        public override void ExecuteCmdlet()
        {
            if (String.IsNullOrEmpty(ShareName) || String.IsNullOrEmpty(Policy))
            {
                return;
            }

            NamingUtil.ValidateShareName(this.ShareName, false);

            if (!NameUtil.IsValidStoredAccessPolicyName(this.Policy))
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.InvalidAccessPolicyName, this.Policy));
            }

            //Get existing permissions
            CloudFileShare fileShare = this.Channel.GetShareReference(this.ShareName);

            FileSharePermissions fileSharePermissions;

            try
            {
                fileSharePermissions = fileShare.GetPermissionsAsync().Result;
            }
            catch (AggregateException e) when(e.InnerException is StorageException)
            {
                throw e.InnerException;
            }

            //Add new policy
            if (fileSharePermissions.SharedAccessPolicies.Keys.Contains(this.Policy))
            {
                throw new ResourceAlreadyExistException(String.Format(CultureInfo.CurrentCulture, Resources.PolicyAlreadyExists, this.Policy));
            }

            SharedAccessFilePolicy policy = new SharedAccessFilePolicy();

            AccessPolicyHelper.SetupAccessPolicy <SharedAccessFilePolicy>(policy, this.StartTime, this.ExpiryTime, this.Permission);
            fileSharePermissions.SharedAccessPolicies.Add(this.Policy, policy);

            //Set permissions back to container
            try
            {
                Task.Run(() => fileShare.SetPermissionsAsync(fileSharePermissions, null, null, OperationContext)).Wait();
            }
            catch (AggregateException e) when(e.InnerException is StorageException)
            {
                throw e.InnerException;
            }

            WriteObject(Policy);
        }
Exemplo n.º 6
0
        private CloudFile GetDestFile()
        {
            var destChannal = this.GetDestinationChannel();

            if (null != this.DestFile)
            {
                return(this.DestFile);
            }
            else
            {
                string destPath = this.DestFilePath;

                NamingUtil.ValidateShareName(this.DestShareName, false);
                CloudFileShare share = destChannal.GetShareReference(this.DestShareName);

                string[] path = NamingUtil.ValidatePath(destPath, true);
                return(share.GetRootDirectoryReference().GetFileReferenceByPath(path));
            }
        }
Exemplo n.º 7
0
        private void StartCopyFromFile()
        {
            CloudFile sourceFile = null;
            string    filePath   = null;

            if (null != this.SrcFile)
            {
                sourceFile = this.SrcFile;
                filePath   = this.SrcFile.GetFullPath();
            }
            else
            {
                CloudFileDirectory dir = null;

                if (null != this.SrcShare)
                {
                    dir = this.SrcShare.GetRootDirectoryReference();
                }
                else
                {
                    NamingUtil.ValidateShareName(this.SrcShareName, false);
                    dir = this.BuildFileShareObjectFromName(this.SrcShareName).GetRootDirectoryReference();
                }

                string[] path = NamingUtil.ValidatePath(this.SrcFilePath, true);
                sourceFile = dir.GetFileReferenceByPath(path);
                filePath   = this.SrcFilePath;
            }

            CloudFile destFile = this.GetDestFile();

            Func <long, Task> taskGenerator = (taskId) => StartAsyncCopy(
                taskId,
                destFile,
                () => this.ConfirmOverwrite(sourceFile.SnapshotQualifiedUri.ToString(), destFile.SnapshotQualifiedUri.ToString()),
                () => destFile.StartCopyAsync(sourceFile.GenerateCopySourceFile()));

            this.RunTask(taskGenerator);
        }
Exemplo n.º 8
0
        private async Task <CloudFile> BuildCloudFileInstanceFromPathAsync(string defaultFileName, string[] path, bool pathIsDirectory)
        {
            CloudFileDirectory baseDirectory = null;
            bool isPathEmpty = path.Length == 0;

            switch (this.ParameterSetName)
            {
            case Constants.DirectoryParameterSetName:
                baseDirectory = this.Directory;
                break;

            case Constants.ShareNameParameterSetName:
                NamingUtil.ValidateShareName(this.ShareName, false);
                baseDirectory = this.BuildFileShareObjectFromName(this.ShareName).GetRootDirectoryReference();
                break;

            case Constants.ShareParameterSetName:
                baseDirectory = this.Share.GetRootDirectoryReference();
                break;

            default:
                throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName));
            }

            if (isPathEmpty)
            {
                return(baseDirectory.GetFileReference(defaultFileName));
            }

            var directory = baseDirectory.GetDirectoryReferenceByPath(path);

            if (pathIsDirectory)
            {
                return(directory.GetFileReference(defaultFileName));
            }

            bool directoryExists;

            try
            {
                directoryExists = await this.Channel.DirectoryExistsAsync(directory, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken);
            }
            catch (StorageException e)
            {
                if (e.RequestInformation != null &&
                    e.RequestInformation.HttpStatusCode == (int)HttpStatusCode.BadRequest &&
                    e.RequestInformation.ExtendedErrorInformation == null)
                {
                    throw new AzureStorageFileException(ErrorCategory.InvalidArgument, ErrorIdConstants.InvalidResource, Resources.InvalidResource, this);
                }

                throw;
            }

            if (directoryExists)
            {
                // If the directory exist on the cloud, we treat the path as
                // to a directory. So we append the default file name after
                // it and build an instance of CloudFile class.
                return(directory.GetFileReference(defaultFileName));
            }
            else
            {
                // If the directory does not exist, we treat the path as to a
                // file. So we use the path of the directory to build out a
                // new instance of CloudFile class.
                return(baseDirectory.GetFileReferenceByPath(path));
            }
        }