public override void ExecuteCmdlet() { CloudBlob blob = null; if (ParameterSetName == BlobNamePipelineParmeterSetWithPermission || ParameterSetName == BlobNamePipelineParmeterSetWithPolicy) { blob = GetCloudBlobByName(Container, Blob); } else { blob = this.CloudBlob; } // 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(blob.Name, "Generate User Delegation SAS, since input Storage Context is OAuth based.")) { generateUserDelegationSas = true; if (!string.IsNullOrEmpty(accessPolicyIdentifier)) { throw new ArgumentException("When input Storage Context is OAuth based, Saved Policy is not supported.", "Policy"); } } else { return; } } if (!(blob is InvalidCloudBlob) && !UseTrack2Sdk()) { SharedAccessBlobPolicy accessPolicy = new SharedAccessBlobPolicy(); bool shouldSetExpiryTime = SasTokenHelper.ValidateContainerAccessPolicy(Channel, blob.Container.Name, accessPolicy, accessPolicyIdentifier); SetupAccessPolicy(accessPolicy, shouldSetExpiryTime); string sasToken = GetBlobSharedAccessSignature(blob, accessPolicy, accessPolicyIdentifier, Protocol, Util.SetupIPAddressOrRangeForSAS(IPAddressOrRange), generateUserDelegationSas); if (FullUri) { string fullUri = blob.SnapshotQualifiedUri.ToString(); if (blob.IsSnapshot) { // Since snapshot URL already has '?', need remove '?' in the first char of sas fullUri = fullUri + "&" + sasToken.Substring(1); } else { fullUri = fullUri + sasToken; } WriteObject(fullUri); } else { WriteObject(sasToken); } } else // Use Track2 SDk { //Get blob instance BlobBaseClient blobClient; if (this.BlobBaseClient != null) { blobClient = this.BlobBaseClient; } else { blobClient = AzureStorageBlob.GetTrack2BlobClient(blob, Channel.StorageContext, this.ClientOptions); } // Get contaienr saved policy if any BlobSignedIdentifier identifier = null; if (ParameterSetName == BlobNamePipelineParmeterSetWithPolicy || ParameterSetName == BlobPipelineParameterSetWithPolicy) { BlobContainerClient container = AzureStorageContainer.GetTrack2BlobContainerClient(Channel.GetContainerReference(blobClient.BlobContainerName), Channel.StorageContext, ClientOptions); identifier = SasTokenHelper.GetBlobSignedIdentifier(container, this.Policy, CmdletCancellationToken); } //Create SAS builder BlobSasBuilder sasBuilder = SasTokenHelper.SetBlobSasBuilder_FromBlob(blobClient, identifier, this.Permission, this.StartTime, this.ExpiryTime, this.IPAddressOrRange, this.Protocol); //Create SAS and ourput string sasToken = SasTokenHelper.GetBlobSharedAccessSignature(Channel.StorageContext, sasBuilder, generateUserDelegationSas, ClientOptions, CmdletCancellationToken); if (sasToken[0] != '?') { sasToken = "?" + sasToken; } if (FullUri) { string fullUri = blobClient.Uri.ToString(); if (blob.IsSnapshot) { // Since snapshot URL already has '?', need remove '?' in the first char of sas fullUri = fullUri + "&" + sasToken.Substring(1); } else { fullUri = fullUri + sasToken; } WriteObject(fullUri); } else { WriteObject(sasToken); } } }