Ejemplo n.º 1
0
        private async Task StartCopyFromUri(long taskId, IStorageBlobManagement destChannel, Uri srcUri, CloudBlob destBlob)
        {
            if (UseTrack2Sdk())
            {
                BlobClient destBlobClient = AzureStorageBlob.GetTrack2BlobClient(destBlob, destChannel.StorageContext, this.ClientOptions);
                await StartCopyFromUri(taskId, destChannel, srcUri, destBlobClient).ConfigureAwait(false);

                return;
            }
            else // use track1 SDK
            {
                bool destExist = true;
                try
                {
                    await destBlob.FetchAttributesAsync(null, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken).ConfigureAwait(false);
                }
                catch (StorageException ex)
                {
                    if (ex.IsNotFoundException())
                    {
                        destExist = false;
                    }
                    else
                    {
                        throw;
                    }
                }

                ValidateBlobTier(destBlob.BlobType, pageBlobTier, standardBlobTier, rehydratePriority);

                if (!destExist || this.ConfirmOverwrite(srcUri.AbsoluteUri.ToString(), destBlob.Uri.ToString()))
                {
                    string copyId;

                    //Clean the Metadata of the destination Blob object, or the source metadata won't overwirte the dest blob metadata. See https://docs.microsoft.com/en-us/rest/api/storageservices/copy-blob
                    destBlob.Metadata.Clear();

                    // The Blob Type and Blob Tier must match, since already checked they are match at the begin of ExecuteCmdlet().
                    if (pageBlobTier != null)
                    {
                        copyId = await destChannel.StartCopyAsync((CloudPageBlob)destBlob, srcUri, pageBlobTier.Value, null, null, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken).ConfigureAwait(false);
                    }
                    else if (standardBlobTier != null || rehydratePriority != null)
                    {
                        copyId = await destChannel.StartCopyAsync(destBlob, srcUri, standardBlobTier, rehydratePriority, null, null, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        copyId = await destChannel.StartCopyAsync(destBlob, srcUri, null, null, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken).ConfigureAwait(false);
                    }

                    this.OutputStream.WriteVerbose(taskId, String.Format(Resources.CopyDestinationBlobPending, destBlob.Name, destBlob.Container.Name, copyId));
                    this.WriteCloudBlobObject(taskId, destChannel, destBlob);
                }
            }
        }
Ejemplo n.º 2
0
        // Convert Track1 Blob object to Track 2 append blob Client
        public static AppendBlobClient GetTrack2AppendBlobClient(CloudBlob cloubBlob, AzureStorageContext context, BlobClientOptions options = null)
        {
            AppendBlobClient blobClient = (AppendBlobClient)Util.GetTrack2BlobClientWithType(
                AzureStorageBlob.GetTrack2BlobClient(cloubBlob, context, options),
                context,
                Track2blobModel.BlobType.Append,
                options);

            return(blobClient);
        }
Ejemplo n.º 3
0
        public override void ExecuteCmdlet()
        {
            IStorageBlobManagement destChannel = GetDestinationChannel();
            IStorageBlobManagement srcChannel  = Channel;

            string target     = string.Empty;
            Action copyAction = null;

            switch (ParameterSetName)
            {
            case ContainerNameParameterSet:
                copyAction = () => StartCopyBlob(srcChannel, destChannel, SrcContainer, SrcBlob, DestContainer, DestBlob);
                target     = SrcBlob;
                break;

            case UriParameterSet:
                copyAction = () => StartCopyBlob(destChannel, AbsoluteUri, DestContainer, DestBlob, (Context != null ? GetCmdletStorageContext(Context) : null));
                target     = AbsoluteUri;
                break;

            case BlobParameterSet:
                if (CloudBlob is InvalidCloudBlob || UseTrack2Sdk())
                {
                    copyAction = () => StartCopyBlob(destChannel, BlobBaseClient, DestContainer, DestBlob);
                    target     = BlobBaseClient.Name;
                }
                else
                {
                    copyAction = () => StartCopyBlob(destChannel, CloudBlob, DestContainer, DestBlob);
                    target     = CloudBlob.Name;
                }
                break;

            case ContainerParameterSet:
                copyAction = () => StartCopyBlob(srcChannel, destChannel, CloudBlobContainer.Name, SrcBlob, DestContainer, DestBlob);
                target     = SrcBlob;
                break;

            case BlobToBlobParameterSet:
                if (CloudBlob is InvalidCloudBlob || UseTrack2Sdk())
                {
                    BlobBaseClient destBlobClient = AzureStorageBlob.GetTrack2BlobClient(DestCloudBlob, destChannel.StorageContext, ClientOptions);
                    copyAction = () => StartCopyBlob(destChannel, BlobBaseClient, destBlobClient);
                    target     = BlobBaseClient.Name;
                }
                else
                {
                    copyAction = () => StartCopyBlob(destChannel, CloudBlob, DestCloudBlob);
                    target     = CloudBlob.Name;
                }
                break;

            case ShareNameParameterSet:
                copyAction = () => StartCopyFromFile(
                    this.GetFileChannel(),
                    destChannel,
                    this.SrcShareName,
                    this.SrcFilePath,
                    this.DestContainer,
                    this.DestBlob);
                target = SrcFilePath;
                break;

            case ShareParameterSet:
                copyAction = () => StartCopyFromFile(
                    destChannel,
                    this.SrcShare.GetRootDirectoryReference(),
                    this.SrcFilePath,
                    this.DestContainer,
                    this.DestBlob);
                target = SrcFilePath;
                break;

            case DirParameterSet:
                copyAction = () => StartCopyFromFile(
                    destChannel,
                    this.SrcDir,
                    this.SrcFilePath,
                    this.DestContainer,
                    this.DestBlob);
                target = SrcFilePath;
                break;

            case FileParameterSet:
                copyAction = () => StartCopyFromFile(
                    destChannel,
                    this.SrcFile,
                    this.DestContainer,
                    this.DestBlob);
                target = SrcFile.Name;
                break;

            case FileToBlobParameterSet:
                copyAction = () => StartCopyFromFile(
                    destChannel,
                    this.SrcFile,
                    this.DestCloudBlob);
                target = SrcFile.Name;
                break;
            }

            if (copyAction != null && ShouldProcess(target, VerbsCommon.Copy))
            {
                copyAction();
            }
        }
        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);
                }
            }
        }
        /// <summary>
        /// Stop copy operation by CloudBlob object
        /// </summary>
        /// <param name="blob">CloudBlob object</param>
        /// <param name="copyId">Copy id</param>
        private async Task StopCopyBlob(long taskId, IStorageBlobManagement localChannel, CloudBlob blob, string copyId, bool fetchCopyIdFromBlob = false)
        {
            ValidateBlobType(blob);

            if (UseTrack2SDK()) // Use Track2
            {
                if (null == blob)
                {
                    throw new ArgumentException(String.Format(Resources.ObjectCannotBeNull, typeof(CloudBlob).Name));
                }

                BlobBaseClient blobBaseClient = AzureStorageBlob.GetTrack2BlobClient(blob, Channel.StorageContext, this.ClientOptions);

                string specifiedCopyId = copyId;

                if (string.IsNullOrEmpty(specifiedCopyId) && fetchCopyIdFromBlob)
                {
                    if (blob.CopyState != null)
                    {
                        specifiedCopyId = blob.CopyState.CopyId;
                    }
                }

                string abortCopyId = string.Empty;

                if (string.IsNullOrEmpty(specifiedCopyId) || Force)
                {
                    //Make sure we use the correct copy id to abort
                    //Use default retry policy for FetchBlobAttributes
                    Track2Models.BlobProperties blobProperties = blobBaseClient.GetProperties(this.BlobRequestConditions, this.CmdletCancellationToken).Value;

                    if (String.IsNullOrEmpty(blobProperties.CopyId))
                    {
                        ArgumentException e = new ArgumentException(String.Format(Resources.CopyTaskNotFound, blobBaseClient.Name, blobBaseClient.BlobContainerName));
                        OutputStream.WriteError(taskId, e);
                    }
                    else
                    {
                        abortCopyId = blobProperties.CopyId;
                    }

                    if (!Force)
                    {
                        string confirmation = String.Format(Resources.ConfirmAbortCopyOperation, blobBaseClient.Name, blobBaseClient.BlobContainerName, abortCopyId);
                        if (!await OutputStream.ConfirmAsync(confirmation).ConfigureAwait(false))
                        {
                            string cancelMessage = String.Format(Resources.StopCopyOperationCancelled, blobBaseClient.Name, blobBaseClient.BlobContainerName);
                            OutputStream.WriteVerbose(taskId, cancelMessage);
                        }
                    }
                }
                else
                {
                    abortCopyId = specifiedCopyId;
                }

                await blobBaseClient.AbortCopyFromUriAsync(abortCopyId, this.BlobRequestConditions, this.CmdletCancellationToken).ConfigureAwait(false);

                //localChannel.AbortCopyAsync(blob, abortCopyId, accessCondition, abortRequestOption, OperationContext, CmdletCancellationToken).ConfigureAwait(false);
                string message = String.Format(Resources.StopCopyBlobSuccessfully, blobBaseClient.Name, blobBaseClient.BlobContainerName);
                OutputStream.WriteObject(taskId, message);
            }
            else // use Track1
            {
                AccessCondition    accessCondition    = null;
                BlobRequestOptions abortRequestOption = RequestOptions ?? new BlobRequestOptions();

                //Set no retry to resolve the 409 conflict exception
                abortRequestOption.RetryPolicy = new NoRetry();

                if (null == blob)
                {
                    throw new ArgumentException(String.Format(Resources.ObjectCannotBeNull, typeof(CloudBlob).Name));
                }

                string specifiedCopyId = copyId;

                if (string.IsNullOrEmpty(specifiedCopyId) && fetchCopyIdFromBlob)
                {
                    if (blob.CopyState != null)
                    {
                        specifiedCopyId = blob.CopyState.CopyId;
                    }
                }

                string abortCopyId = string.Empty;

                if (string.IsNullOrEmpty(specifiedCopyId) || Force)
                {
                    //Make sure we use the correct copy id to abort
                    //Use default retry policy for FetchBlobAttributes
                    BlobRequestOptions options = RequestOptions;
                    await localChannel.FetchBlobAttributesAsync(blob, accessCondition, options, OperationContext, CmdletCancellationToken).ConfigureAwait(false);

                    if (blob.CopyState == null || String.IsNullOrEmpty(blob.CopyState.CopyId))
                    {
                        ArgumentException e = new ArgumentException(String.Format(Resources.CopyTaskNotFound, blob.Name, blob.Container.Name));
                        OutputStream.WriteError(taskId, e);
                    }
                    else
                    {
                        abortCopyId = blob.CopyState.CopyId;
                    }

                    if (!Force)
                    {
                        string confirmation = String.Format(Resources.ConfirmAbortCopyOperation, blob.Name, blob.Container.Name, abortCopyId);
                        if (!await OutputStream.ConfirmAsync(confirmation).ConfigureAwait(false))
                        {
                            string cancelMessage = String.Format(Resources.StopCopyOperationCancelled, blob.Name, blob.Container.Name);
                            OutputStream.WriteVerbose(taskId, cancelMessage);
                        }
                    }
                }
                else
                {
                    abortCopyId = specifiedCopyId;
                }

                await localChannel.AbortCopyAsync(blob, abortCopyId, accessCondition, abortRequestOption, OperationContext, CmdletCancellationToken).ConfigureAwait(false);

                string message = String.Format(Resources.StopCopyBlobSuccessfully, blob.Name, blob.Container.Name);
                OutputStream.WriteObject(taskId, message);
            }
        }
Ejemplo n.º 6
0
        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
            {
                BlobBaseClient blobClient;
                if (this.BlobBaseClient != null)
                {
                    blobClient = this.BlobBaseClient;
                }
                else
                {
                    blobClient = AzureStorageBlob.GetTrack2BlobClient(blob, Channel.StorageContext, this.ClientOptions);
                }

                BlobSasBuilder sasBuilder;
                if (ParameterSetName == BlobNamePipelineParmeterSetWithPolicy || ParameterSetName == BlobPipelineParameterSetWithPolicy)
                {
                    BlobContainerClient container = AzureStorageContainer.GetTrack2BlobContainerClient(Channel.GetContainerReference(blobClient.BlobContainerName), Channel.StorageContext, ClientOptions);
                    IEnumerable <BlobSignedIdentifier> signedIdentifiers = container.GetAccessPolicy(cancellationToken: CmdletCancellationToken).Value.SignedIdentifiers;
                    BlobSignedIdentifier signedIdentifier = null;
                    foreach (BlobSignedIdentifier identifier in signedIdentifiers)
                    {
                        if (identifier.Id == this.Policy)
                        {
                            signedIdentifier = identifier;
                            break;
                        }
                    }
                    if (signedIdentifier is null)
                    {
                        throw new ArgumentException(string.Format(Resources.InvalidAccessPolicy, this.Policy));
                    }
                    sasBuilder = new BlobSasBuilder
                    {
                        BlobContainerName = blobClient.BlobContainerName,
                        BlobName          = blobClient.Name,
                        Identifier        = this.Policy
                    };

                    if (this.StartTime != null)
                    {
                        if (signedIdentifier.AccessPolicy.StartsOn != DateTimeOffset.MinValue)
                        {
                            throw new InvalidOperationException(Resources.SignedStartTimeMustBeOmitted);
                        }
                        else
                        {
                            sasBuilder.StartsOn = this.StartTime.Value.ToUniversalTime();
                        }
                    }

                    if (this.ExpiryTime != null)
                    {
                        if (signedIdentifier.AccessPolicy.ExpiresOn != DateTimeOffset.MinValue)
                        {
                            throw new ArgumentException(Resources.SignedExpiryTimeMustBeOmitted);
                        }
                        else
                        {
                            sasBuilder.ExpiresOn = this.ExpiryTime.Value.ToUniversalTime();
                        }
                    }
                    else if (signedIdentifier.AccessPolicy.ExpiresOn == DateTimeOffset.MinValue)
                    {
                        if (sasBuilder.StartsOn != DateTimeOffset.MinValue)
                        {
                            sasBuilder.ExpiresOn = sasBuilder.StartsOn.ToUniversalTime().AddHours(1);
                        }
                        else
                        {
                            sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddHours(1);
                        }
                    }

                    if (this.Permission != null)
                    {
                        if (signedIdentifier.AccessPolicy.Permissions != null)
                        {
                            throw new ArgumentException(Resources.SignedPermissionsMustBeOmitted);
                        }
                        else
                        {
                            sasBuilder.SetPermissions(this.Permission);
                        }
                    }
                }
                else
                {
                    sasBuilder = new BlobSasBuilder
                    {
                        BlobContainerName = blobClient.BlobContainerName,
                        BlobName          = blobClient.Name,
                    };
                    sasBuilder.SetPermissions(this.Permission);
                    if (this.StartTime != null)
                    {
                        sasBuilder.StartsOn = this.StartTime.Value.ToUniversalTime();
                    }
                    if (this.ExpiryTime != null)
                    {
                        sasBuilder.ExpiresOn = this.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)
                {
                    if (this.Protocol.Value == SharedAccessProtocol.HttpsOrHttp)
                    {
                        sasBuilder.Protocol = SasProtocol.HttpsAndHttp;
                    }
                    else //HttpsOnly
                    {
                        sasBuilder.Protocol = SasProtocol.Https;
                    }
                }
                if (Util.GetVersionIdFromBlobUri(blobClient.Uri) != null)
                {
                    sasBuilder.BlobVersionId = Util.GetVersionIdFromBlobUri(blobClient.Uri);
                }
                if (Util.GetSnapshotTimeFromBlobUri(blobClient.Uri) != null)
                {
                    sasBuilder.Snapshot = Util.GetSnapshotTimeFromBlobUri(blobClient.Uri).Value.ToString("o");
                }

                string sasToken = GetBlobSharedAccessSignature(blobClient, sasBuilder, generateUserDelegationSas);
                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);
                }
            }
        }