示例#1
0
 private static void DoLeaseOperation(CloudBlob blob, string leaseId, Microsoft.WindowsAzure.StorageClient.Protocol.LeaseAction action)
 {
     try
     {
         if (blob == null || leaseId == null)
             return;
         var creds = blob.ServiceClient.Credentials;
         var transformedUri = new Uri(creds.TransformUri(blob.Uri.ToString()));
         var req = BlobRequest.Lease(transformedUri, AzureBlobLeaseTimeout, action, leaseId);
         creds.SignRequest(req);
         req.GetResponse().Close();
     }
     catch (WebException e)
     {
         Console.WriteLine("WebException", e.Message + ". DoLeaseOperation, blob: " + blob.Name + ", leaseId: " + leaseId + ", action " + action);
     }
 }
 public virtual Task DownloadAsync(CloudBlob sourceBlob, string destPath, DownloadOptions options, SingleTransferContext context, CancellationToken cancellationToken)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// Deletes the file.
        /// </summary>
        /// <param name="bucketName">Name of the bucket.</param>
        /// <param name="keyName">Name of the key.</param>
        public void DeleteFile(string bucketName, string keyName)
        {
            CloudBlob blob = GetBlob(bucketName, keyName);

            blob.DeleteIfExists();
        }
        /// <summary>
        /// Start copy operation by source and destination CloudBlob object
        /// </summary>
        /// <param name="srcCloudBlob">Source CloudBlob object</param>
        /// <param name="destCloudBlob">Destination CloudBlob object</param>
        /// <returns>Destination CloudBlob object</returns>
        private void StartCopyBlob(IStorageBlobManagement destChannel, CloudBlob srcCloudBlob, CloudBlob destCloudBlob)
        {
            ValidateBlobType(srcCloudBlob);

            Func <long, Task> taskGenerator = (taskId) => StartCopyInTransferManager(taskId, destChannel, srcCloudBlob, destCloudBlob);

            RunTask(taskGenerator);
        }
        /// <summary>
        /// Start copy using transfer mangager by source CloudBlob object
        /// </summary>
        /// <param name="blob">Source CloudBlob object</param>
        /// <param name="destContainer">Destination CloudBlobContainer object</param>
        /// <param name="destBlobName">Destination blob name</param>
        /// <returns>Destination CloudBlob object</returns>
        private async Task StartCopyInTransferManager(long taskId, IStorageBlobManagement DestChannel, CloudBlob sourceBlob, CloudBlob destBlob)
        {
            NameUtil.ValidateBlobName(sourceBlob.Name);
            NameUtil.ValidateContainerName(destBlob.Container.Name);
            NameUtil.ValidateBlobName(destBlob.Name);

            Dictionary <string, string> BlobPath = new Dictionary <string, string>()
            {
                { "Container", destBlob.Container.Name },
                { "Blob", destBlob.Name }
            };

            DataMovementUserData data = new DataMovementUserData()
            {
                Data    = BlobPath,
                TaskId  = taskId,
                Channel = DestChannel,
                Record  = null
            };

            TransferJob startCopyJob = new TransferJob(new TransferLocation(sourceBlob), new TransferLocation(destBlob), TransferMethod.AsyncCopyInAzureStorageWithoutMonitor);

            await this.EnqueueStartCopyJob(startCopyJob, data);
        }
        /// <summary>
        /// Write CloudBlob to output using specified service channel
        /// </summary>
        /// <param name="blob">The output CloudBlob object</param>
        /// <param name="channel">IStorageBlobManagement channel object</param>
        internal void WriteCloudBlobObject(long taskId, IStorageBlobManagement channel, CloudBlob blob, BlobContinuationToken continuationToken = null)
        {
            AzureStorageBlob azureBlob = new AzureStorageBlob(blob);

            azureBlob.Context           = channel.StorageContext;
            azureBlob.ContinuationToken = continuationToken;
            OutputStream.WriteObject(taskId, azureBlob);
        }
示例#7
0
        internal async Task DeleteCloudAsync(long taskId, IStorageBlobManagement localChannel, CloudBlob blob, DeleteSnapshotsOption deleteSnapshotsOption)
        {
            AccessCondition    accessCondition = null;
            BlobRequestOptions requestOptions  = null;

            await localChannel.DeleteCloudBlobAsync(blob, deleteSnapshotsOption, accessCondition,
                                                    requestOptions, OperationContext, CmdletCancellationToken).ConfigureAwait(false);

            string result = String.Format(Resources.RemoveBlobSuccessfully, blob.Name, blob.Container.Name);

            OutputStream.WriteVerbose(taskId, result);

            if (PassThru)
            {
                OutputStream.WriteObject(taskId, true);
            }
        }
示例#8
0
        /// <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);

            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);

                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))
                    {
                        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);

            string message = String.Format(Resources.StopCopyBlobSuccessfully, blob.Name, blob.Container.Name);

            OutputStream.WriteObject(taskId, message);
        }
 /// <summary>
 /// fetch blob attributes
 /// </summary>
 /// <param name="accessCondition">Access condition</param>
 /// <param name="options">blob request options</param>
 /// <param name="operationContext">An object that represents the context for the current operation.</param>
 public void FetchBlobAttributes(CloudBlob blob, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
 {
     return;
 }
示例#10
0
 public SingleFileSignature(FileInfo file, CloudBlob blob = null) : base(file.Directory, file.Name, new string[] { file.Name }, blob)
 {
     File = file;
     Size = File.Length;
 }
示例#11
0
        public async Task <bool> ExecuteAsync()
        {
            try
            {
                Log.LogMessage("Performing blob merge...");

                if (string.IsNullOrEmpty(SourceBlobDirectory) || string.IsNullOrEmpty(TargetBlobDirectory))
                {
                    Log.LogError($"Please specify a source blob directory and a target blob directory");
                }
                else
                {
                    // Canonicalize the target uri
                    string targetUri = GetCanonicalStorageUri(TargetBlobDirectory);
                    // Invoke the blob URI parser on the target URI and deal with any container creation that needs to happen
                    BlobUrlInfo         targetUrlInfo   = new BlobUrlInfo(targetUri);
                    CloudStorageAccount storageAccount  = new CloudStorageAccount(new WindowsAzure.Storage.Auth.StorageCredentials(targetUrlInfo.AccountName, AccountKey), true);
                    CloudBlobClient     client          = storageAccount.CreateCloudBlobClient();
                    CloudBlobContainer  targetContainer = client.GetContainerReference(targetUrlInfo.ContainerName);

                    if (!SkipCreateContainer)
                    {
                        Log.LogMessage($"Creating container {targetUrlInfo.ContainerName} if it doesn't exist.");
                        await targetContainer.CreateIfNotExistsAsync();
                    }

                    string sourceUri = GetCanonicalStorageUri(SourceBlobDirectory);
                    // Grab the source blob path from the source info and combine with the target blob path.
                    BlobUrlInfo sourceBlobInfo = new BlobUrlInfo(sourceUri);

                    // For now the source and target storage accounts should be the same, so the same account key is used for each.
                    if (sourceBlobInfo.AccountName != targetUrlInfo.AccountName)
                    {
                        Log.LogError($"Source and target storage accounts should be identical");
                    }
                    else
                    {
                        CloudBlobContainer sourceContainer = client.GetContainerReference(sourceBlobInfo.ContainerName);

                        Log.LogMessage($"Listing blobs in {sourceUri}");

                        // Get all source URI's with the blob prefix
                        BlobContinuationToken token       = null;
                        List <IListBlobItem>  sourceBlobs = new List <IListBlobItem>();
                        do
                        {
                            BlobResultSegment segment = await sourceContainer.ListBlobsSegmentedAsync(sourceBlobInfo.BlobPath, true,
                                                                                                      BlobListingDetails.None, null, token, new BlobRequestOptions(), null);

                            token = segment.ContinuationToken;
                            sourceBlobs.AddRange(segment.Results);
                        }while (token != null);

                        // Ensure the source exists
                        if (!SkipIfMissing && sourceBlobs.Count == 0)
                        {
                            Log.LogError($"No blobs found in {sourceUri}");
                        }

                        await Task.WhenAll(sourceBlobs.Select(async blob =>
                        {
                            // Determine the relative URI for the target.  This works properly when the
                            // trailing slash is left off of the source and target URIs.
                            string relativeBlobPath  = blob.Uri.ToString().Substring(sourceUri.Length);
                            string specificTargetUri = GetCanonicalStorageUri(targetUri, relativeBlobPath);
                            BlobUrlInfo specificTargetBlobUrlInfo = new BlobUrlInfo(specificTargetUri);
                            CloudBlob targetBlob = targetContainer.GetBlobReference(specificTargetBlobUrlInfo.BlobPath);

                            Log.LogMessage($"Merging {blob.Uri.ToString()} into {targetBlob.Uri.ToString()}");

                            if (!Overwrite && await targetBlob.ExistsAsync())
                            {
                                Log.LogError($"Target blob {targetBlob.Uri.ToString()} already exists.");
                            }

                            await targetBlob.StartCopyAsync(blob.Uri);
                        }));
                    }
                }
            }
            catch (Exception e)
            {
                Log.LogErrorFromException(e, true);
            }

            return(!Log.HasLoggedErrors);
        }
        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, this.EncryptionScope);

                //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);
                }
            }
        }
示例#13
0
        public static CloudBlob GetBlobReferenceFromServer(CloudBlobClient client, Uri blobUri)
        {
            CloudBlob blob = new CloudBlob(blobUri, client.Credentials);

            return(GetBlobReferenceFromServer(blob));
        }
        /// <summary>
        /// Start copy using transfer mangager by source CloudBlob object
        /// </summary>
        /// <param name="blob">Source CloudBlob object</param>
        /// <param name="destContainer">Destination CloudBlobContainer object</param>
        /// <param name="destBlobName">Destination blob name</param>
        /// <returns>Destination CloudBlob object</returns>
        private async Task StartCopyAsync(long taskId, IStorageBlobManagement destChannel, CloudBlob sourceBlob, CloudBlob destBlob)
        {
            NameUtil.ValidateBlobName(sourceBlob.Name);
            NameUtil.ValidateContainerName(destBlob.Container.Name);
            NameUtil.ValidateBlobName(destBlob.Name);

            await this.StartCopyFromBlob(taskId, destChannel, sourceBlob, destBlob);
        }
        private async Task StartCopyFromUri(long taskId, IStorageBlobManagement destChannel, Uri srcUri, CloudBlob destBlob)
        {
            bool destExist = true;

            try
            {
                await destBlob.FetchAttributesAsync(null, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken);
            }
            catch (StorageException ex)
            {
                if (ex.IsNotFoundException())
                {
                    destExist = false;
                }
                else
                {
                    throw;
                }
            }

            if (!destExist || this.ConfirmOverwrite(srcUri.AbsoluteUri.ToString(), destBlob.Uri.ToString()))
            {
                string copyId = await destChannel.StartCopyAsync(destBlob, srcUri, null, null, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken);

                this.OutputStream.WriteVerbose(taskId, String.Format(Resources.CopyDestinationBlobPending, destBlob.Name, destBlob.Container.Name, copyId));
                this.WriteCloudBlobObject(taskId, destChannel, destBlob);
            }
        }
 /// <summary>
 /// set blob meta data
 /// </summary>
 /// <param name="blob">ICloud blob object</param>
 /// <param name="accessCondition">Access condition</param>
 /// <param name="options">blob request options</param>
 /// <param name="operationContext">An object that represents the context for the current operation.</param>
 public void SetBlobMetadata(CloudBlob blob, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
 {
     return;
 }
 /// <summary>
 /// whether the specified blob is a snapshot
 /// </summary>
 /// <param name="blob">CloudBlob object</param>
 /// <returns>true if the specified blob is snapshot, otherwise false</returns>
 internal bool IsSnapshot(CloudBlob blob)
 {
     return(!string.IsNullOrEmpty(blob.Name) && blob.SnapshotTime != null);
 }
 /// <summary>
 /// Abort copy operation on specified blob
 /// </summary>
 /// <param name="blob">CloudBlob object</param>
 /// <param name="copyId">Copy id</param>
 /// <param name="accessCondition">Access condition</param>
 /// <param name="options">Blob request options</param>
 /// <param name="operationContext">Operation context</param>
 public void AbortCopy(CloudBlob blob, string copyId, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
 {
     return;
 }
示例#19
0
        /// <summary>
        /// remove the azure blob
        /// </summary>
        /// <param name="blob">Cloudblob object</param>
        /// <param name="isValidBlob">whether the Cloudblob parameter is validated</param>
        /// <returns>true if the blob is removed successfully, false if user cancel the remove operation</returns>
        internal async Task RemoveAzureBlob(long taskId, IStorageBlobManagement localChannel, CloudBlob blob, bool isValidBlob)
        {
            if (!isValidBlob)
            {
                ValidatePipelineCloudBlob(blob);
            }

            ValidateBlobType(blob);

            DeleteSnapshotsOption deleteSnapshotsOption = DeleteSnapshotsOption.None;
            bool retryDeleteSnapshot = false;

            if (IsSnapshot(blob))
            {
                if (deleteSnapshot)
                {
                    throw new ArgumentException(String.Format(Resources.CannotDeleteSnapshotForSnapshot, blob.Name, blob.SnapshotTime));
                }
            }
            else
            {
                if (deleteSnapshot)
                {
                    deleteSnapshotsOption = DeleteSnapshotsOption.DeleteSnapshotsOnly;
                }
                else if (force)
                {
                    deleteSnapshotsOption = DeleteSnapshotsOption.IncludeSnapshots;
                }
                else
                {
                    retryDeleteSnapshot = true;
                }
            }

            try
            {
                await DeleteCloudAsync(taskId, localChannel, blob, deleteSnapshotsOption).ConfigureAwait(false);

                retryDeleteSnapshot = false;
            }
            catch (StorageException e)
            {
                if (e.IsConflictException() && retryDeleteSnapshot)
                {
                    //If x-ms-delete-snapshots is not specified on the request and the blob has associated snapshots, the Blob service returns status code 409 (Conflict).
                    retryDeleteSnapshot = true;
                }
                else
                {
                    throw;
                }
            }

            if (retryDeleteSnapshot)
            {
                string message = string.Format(Resources.ConfirmRemoveBlobWithSnapshot, blob.Name, blob.Container.Name);

                if (await OutputStream.ConfirmAsync(message).ConfigureAwait(false))
                {
                    deleteSnapshotsOption = DeleteSnapshotsOption.IncludeSnapshots;
                    await DeleteCloudAsync(taskId, localChannel, blob, deleteSnapshotsOption).ConfigureAwait(false);
                }
                else
                {
                    string result = String.Format(Resources.RemoveBlobCancelled, blob.Name, blob.Container.Name);
                    OutputStream.WriteVerbose(taskId, result);
                }
            }
        }
 /// <summary>
 /// Return a task that asynchronously check whether the specified blob exists.
 /// </summary>
 /// <param name="blob">CloudBlob object</param>
 /// <param name="options">Blob request options</param>
 /// <param name="operationContext">Operation context</param>
 /// <param name="cmdletCancellationToken">Cancellation token</param>
 /// <returns>A task object that asynchronously check whether the specified blob exists.</returns>
 public Task <bool> DoesBlobExistAsync(CloudBlob blob, BlobRequestOptions options, OperationContext operationContext, CancellationToken cmdletCancellationToken)
 {
     return(Task.Factory.StartNew(() => this.DoesBlobExist(blob, options, operationContext)));
 }
示例#21
0
        /// <summary>
        /// Get blob shared access signature
        /// </summary>
        /// <param name="blob">CloudBlob object</param>
        /// <param name="accessPolicy">SharedAccessBlobPolicy object</param>
        /// <param name="policyIdentifier">The existing policy identifier.</param>
        /// <returns></returns>
        private string GetBlobSharedAccessSignature(CloudBlob blob, SharedAccessBlobPolicy accessPolicy, string policyIdentifier, SharedAccessProtocol?protocol, IPAddressOrRange iPAddressOrRange)
        {
            CloudBlobContainer container = blob.Container;

            return(blob.GetSharedAccessSignature(accessPolicy, null, policyIdentifier, protocol, iPAddressOrRange));
        }
 /// <summary>
 /// Return a task that asynchronously fetch blob attributes
 /// </summary>
 /// <param name="blob">ICloud blob object</param>
 /// <param name="accessCondition">Access condition</param>
 /// <param name="options">Blob request options</param>
 /// <param name="operationContext">Operation context</param>
 /// <param name="cmdletCancellationToken">Cancellation token</param>
 /// <returns>Return a task that asynchronously fetch blob attributes</returns>
 public Task FetchBlobAttributesAsync(CloudBlob blob, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext, CancellationToken cmdletCancellationToken)
 {
     return(Task.Factory.StartNew(() => this.FetchBlobAttributes(blob, accessCondition, options, operationContext)));
 }
        private void StartCopyFromFile(IStorageBlobManagement destChannel, CloudFile srcFile, CloudBlob destBlob)
        {
            CloudBlockBlob destBlockBlob = destBlob as CloudBlockBlob;

            if (null == destBlockBlob)
            {
                throw new InvalidOperationException(Resources.OnlyCopyFromBlockBlobToAzureFile);
            }

            Func <long, Task> taskGenerator = (taskId) => this.StartCopyFromFile(taskId, destChannel, srcFile, destBlockBlob);;

            RunTask(taskGenerator);
        }
 /// <summary>
 /// Return a task that asynchronously abort the blob copy operation
 /// </summary>
 /// <param name="blob">CloudBlob object</param>
 /// <param name="abortCopyId">Copy id</param>
 /// <param name="accessCondition">Access condition</param>
 /// <param name="abortRequestOption">Blob request options</param>
 /// <param name="operationContext">Operation context</param>
 /// <param name="cmdletCancellationToken">Cancellation token</param>
 /// <returns>Return a task that asynchronously abort the blob copy operation</returns>
 public Task AbortCopyAsync(CloudBlob blob, string copyId, AccessCondition accessCondition, BlobRequestOptions requestOption, OperationContext operationContext, CancellationToken cmdletCancellationToken)
 {
     return(Task.Factory.StartNew(() => this.AbortCopy(blob, copyId, accessCondition, requestOption, operationContext)));
 }
示例#25
0
        public async Task <bool> ExecuteAsync()
        {
            try
            {
                Log.LogMessage("Performing blob merge...");

                string sourceKey = SourceAccountKey ?? AccountKey;
                string targetKey = TargetAccountKey ?? AccountKey;

                if (string.IsNullOrEmpty(SourceBlobDirectory) ||
                    string.IsNullOrEmpty(TargetBlobDirectory) ||
                    string.IsNullOrEmpty(sourceKey) || string.IsNullOrEmpty(targetKey))
                {
                    Log.LogError($"Please specify a source blob directory, a target blob directory and account keys");
                }
                else
                {
                    // Canonicalize the target uri
                    string targetUri = GetCanonicalStorageUri(TargetBlobDirectory);
                    // Invoke the blob URI parser on the target URI and deal with any container creation that needs to happen
                    BlobUrlInfo         targetUrlInfo        = new BlobUrlInfo(targetUri);
                    CloudStorageAccount targetStorageAccount = new CloudStorageAccount(new WindowsAzure.Storage.Auth.StorageCredentials(targetUrlInfo.AccountName, targetKey), true);
                    CloudBlobClient     targetClient         = targetStorageAccount.CreateCloudBlobClient();
                    CloudBlobContainer  targetContainer      = targetClient.GetContainerReference(targetUrlInfo.ContainerName);

                    if (!SkipCreateContainer)
                    {
                        Log.LogMessage($"Creating container {targetUrlInfo.ContainerName} if it doesn't exist.");
                        await targetContainer.CreateIfNotExistsAsync();
                    }

                    string sourceUri = GetCanonicalStorageUri(SourceBlobDirectory);
                    // Grab the source blob path from the source info and combine with the target blob path.
                    BlobUrlInfo         sourceBlobInfo       = new BlobUrlInfo(sourceUri);
                    CloudStorageAccount sourceStorageAccount = new CloudStorageAccount(new WindowsAzure.Storage.Auth.StorageCredentials(sourceBlobInfo.AccountName, sourceKey), true);
                    CloudBlobClient     sourceClient         = sourceStorageAccount.CreateCloudBlobClient();

                    CloudBlobContainer sourceContainer = sourceClient.GetContainerReference(sourceBlobInfo.ContainerName);

                    Log.LogMessage($"Listing blobs in {sourceUri}");

                    // Get all source URI's with the blob prefix
                    BlobContinuationToken token       = null;
                    List <IListBlobItem>  sourceBlobs = new List <IListBlobItem>();
                    do
                    {
                        BlobResultSegment segment = await sourceContainer.ListBlobsSegmentedAsync(sourceBlobInfo.BlobPath, true,
                                                                                                  BlobListingDetails.None, null, token, new BlobRequestOptions(), null);

                        token = segment.ContinuationToken;
                        sourceBlobs.AddRange(segment.Results);
                    }while (token != null);

                    // Ensure the source exists
                    if (!SkipIfMissing && sourceBlobs.Count == 0)
                    {
                        Log.LogError($"No blobs found in {sourceUri}");
                    }

                    await Task.WhenAll(sourceBlobs.Select(async blob =>
                    {
                        // Determine the relative URI for the target.  This works properly when the
                        // trailing slash is left off of the source and target URIs.
                        string relativeBlobPath  = blob.Uri.ToString().Substring(sourceUri.Length);
                        string specificTargetUri = GetCanonicalStorageUri(targetUri, relativeBlobPath);
                        BlobUrlInfo specificTargetBlobUrlInfo = new BlobUrlInfo(specificTargetUri);
                        CloudBlob targetBlob = targetContainer.GetBlobReference(specificTargetBlobUrlInfo.BlobPath);

                        Log.LogMessage($"Merging {blob.Uri.ToString()} into {targetBlob.Uri.ToString()}");

                        if (!Overwrite && await targetBlob.ExistsAsync())
                        {
                            Log.LogError($"Target blob {targetBlob.Uri.ToString()} already exists.");
                        }

                        BlobUrlInfo specificSourceBlobUrlInfo = new BlobUrlInfo(blob.Uri);
                        CloudBlob sourceBlob          = sourceContainer.GetBlobReference(specificSourceBlobUrlInfo.BlobPath);
                        SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy
                        {
                            Permissions            = SharedAccessBlobPermissions.Read,
                            SharedAccessStartTime  = null,
                            SharedAccessExpiryTime = DateTimeOffset.Now.AddMinutes(30)
                        };
                        string sas = sourceBlob.GetSharedAccessSignature(policy);
                        await targetBlob.StartCopyAsync(new Uri(blob.Uri + sas));

                        DateTime endWaitTime  = DateTime.Now.AddMinutes(CopyWaitTimeoutInMinutes);
                        TimeSpan waitInterval = TimeSpan.FromSeconds(30);
                        ICloudBlob copyInProgessBlob;
                        do
                        {
                            await Task.Delay(waitInterval);
                            copyInProgessBlob = await targetContainer.GetBlobReferenceFromServerAsync(specificTargetBlobUrlInfo.BlobPath);
                        }while (DateTime.Now.CompareTo(endWaitTime) < 0 && copyInProgessBlob.CopyState.Status == CopyStatus.Pending);

                        if (copyInProgessBlob?.CopyState?.Status != CopyStatus.Success)
                        {
                            Log.LogError($"{copyInProgessBlob.Uri.ToString()} timed out or failed.");
                        }
                        else
                        {
                            Log.LogMessage($"{copyInProgessBlob.Uri.ToString()} completed.");
                        }
                    }));
                }
            }
            catch (Exception e)
            {
                Log.LogErrorFromException(e, true);
            }

            return(!Log.HasLoggedErrors);
        }
 /// <summary>
 /// Return a task that asynchronously delete the specified blob
 /// </summary>
 /// <param name="blob">CloudBlob object</param>
 /// <param name="deleteSnapshotsOption">Snapshot delete option</param>
 /// <param name="accessCondition">Access condition</param>
 /// <param name="requestOptions">Blob request option</param>
 /// <param name="operationContext">Operation context</param>
 /// <param name="cmdletCancellationToken">Cancellation token</param>
 /// <returns>Return a task that asynchronously delete the specified blob</returns>
 public Task DeleteCloudBlobAsync(CloudBlob blob, DeleteSnapshotsOption deleteSnapshotsOption, AccessCondition accessCondition, BlobRequestOptions requestOptions, OperationContext operationContext, CancellationToken cmdletCancellationToken)
 {
     return(Task.Factory.StartNew(() => this.DeleteCloudBlob(blob, deleteSnapshotsOption, accessCondition, requestOptions, operationContext)));
 }
 public virtual Task UploadAsync(string sourcePath, CloudBlob destBlob, UploadOptions options, SingleTransferContext context, CancellationToken cancellationToken)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Return a task that asynchronously set blob meta data
 /// </summary>
 /// <param name="blob">ICloud blob object</param>
 /// <param name="accessCondition">Access condition</param>
 /// <param name="options">Blob request options</param>
 /// <param name="operationContext">An object that represents the context for the current operation.</param>
 public Task SetBlobMetadataAsync(CloudBlob blob, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext, CancellationToken cmdletCancellationToken)
 {
     return(Task.Factory.StartNew(() => this.SetBlobMetadata(blob, accessCondition, options, operationContext)));
 }
示例#29
0
        /// <summary>
        /// Gets the file.
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="keyName"></param>
        /// <returns></returns>
        public byte[] GetFile(string bucketName, string keyName)
        {
            CloudBlob blob = GetBlob(bucketName, keyName);

            return(blob.DownloadByteArray());
        }
 public Task <string> StartCopyAsync(CloudBlob blob, Uri source, StandardBlobTier?standardBlobTier, RehydratePriority?rehydratePriority, AccessCondition sourceAccessCondition, AccessCondition destAccessCondition, BlobRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     throw new NotImplementedException();
 }
示例#31
0
 public static void ReleaseLease(CloudBlob blob, string leaseId)
 {
     DoLeaseOperation(blob, leaseId, Microsoft.WindowsAzure.StorageClient.Protocol.LeaseAction.Release);
 }
 private async Task StartCopyFromBlob(long taskId, IStorageBlobManagement destChannel, CloudBlob srcBlob, CloudBlob destBlob)
 {
     try
     {
         await StartCopyFromUri(taskId, destChannel, srcBlob.GenerateUriWithCredentials(), destBlob);
     }
     catch (StorageException ex)
     {
         if (0 == string.Compare(ex.Message, BlobTypeMismatch, StringComparison.OrdinalIgnoreCase))
         {
             // Current use error message to decide whether it caused by blob type mismatch,
             // We should ask xscl to expose an error code for this..
             // Opened workitem 1487579 to track this.
             throw new InvalidOperationException(Resources.DestinationBlobTypeNotMatch);
         }
         else
         {
             throw;
         }
     }
 }