Пример #1
0
        public override void ExecuteCmdlet()
        {
            IStorageBlobManagement localChannel = Channel;

            // When the input context is Oauth bases, can't generate normal SAS, but UserDelegationSas
            bool generateUserDelegationSas = false;

            if (Channel != null && Channel.StorageContext != null && Channel.StorageContext.StorageAccount.Credentials.IsToken)
            {
                if (ShouldProcess(this.Path, "Generate User Delegation SAS, since input Storage Context is OAuth based."))
                {
                    generateUserDelegationSas = true;
                }
                else
                {
                    return;
                }
            }

            if (this.ParameterSetName == ItemParameterSet)
            {
                if (this.InputObject.IsDirectory)
                {
                    this.FileSystem = this.InputObject.Directory.FileSystemName;
                    this.Path       = this.InputObject.Directory.Path;
                }
                else
                {
                    this.FileSystem = this.InputObject.File.FileSystemName;
                    this.Path       = this.InputObject.File.Path;
                }
            }

            DataLakeSasBuilder sasBuilder = new DataLakeSasBuilder();

            sasBuilder.FileSystemName = this.FileSystem;
            sasBuilder.Path           = this.Path;
            sasBuilder.SetPermissions(this.Permission, true);
            if (StartTime != null)
            {
                sasBuilder.StartsOn = StartTime.Value.ToUniversalTime();
            }
            if (ExpiryTime != null)
            {
                sasBuilder.ExpiresOn = ExpiryTime.Value.ToUniversalTime();
            }
            else
            {
                if (sasBuilder.StartsOn != DateTimeOffset.MinValue)
                {
                    sasBuilder.ExpiresOn = sasBuilder.StartsOn.AddHours(1).ToUniversalTime();
                }
                else
                {
                    sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddHours(1);
                }
            }
            if (this.IPAddressOrRange != null)
            {
                sasBuilder.IPRange = Util.SetupIPAddressOrRangeForSASTrack2(this.IPAddressOrRange);
            }
            if (this.Protocol != null)
            {
                sasBuilder.Protocol = this.Protocol.Value;
            }

            DataLakeFileSystemClient fileSystem = GetFileSystemClientByName(localChannel, this.FileSystem);

            DataLakePathClient      pathClient;
            DataLakeFileClient      fileClient;
            DataLakeDirectoryClient dirClient;

            if (GetExistDataLakeGen2Item(fileSystem, this.Path, out fileClient, out dirClient))
            {
                // Directory
                sasBuilder.IsDirectory = true;
                pathClient             = dirClient;
                //WriteDataLakeGen2Item(localChannel, dirClient);
                // sasBuilder.ToSasQueryParameters()
            }
            else
            {
                //File
                sasBuilder.IsDirectory = false;
                pathClient             = fileClient;
                //WriteDataLakeGen2Item(Channel, fileClient);
            }
            string sasToken = SasTokenHelper.GetDatalakeGen2SharedAccessSignature(Channel.StorageContext, sasBuilder, generateUserDelegationSas, DataLakeClientOptions, CmdletCancellationToken);


            if (FullUri)
            {
                string fullUri = pathClient.Uri.ToString();
                fullUri = fullUri + "?" + sasToken;
                WriteObject(fullUri);
            }
            else
            {
                WriteObject(sasToken);
            }
        }