public void ExecuteCommandGetAzureBlob()
        {
            AddTestBlobs();

            ((MockCommandRuntime)command.CommandRuntime).ResetPipelines();
            command.Container = "container1";
            command.Blob      = "blob*";
            command.ExecuteCmdlet();
            Assert.AreEqual(1, ((MockCommandRuntime)command.CommandRuntime).OutputPipeline.Count);
            AzureStorageBlob blob = (AzureStorageBlob)((MockCommandRuntime)command.CommandRuntime).OutputPipeline.FirstOrDefault();

            Assert.AreEqual("blob0", blob.Name);

            ((MockCommandRuntime)command.CommandRuntime).ResetPipelines();
            command.Container = "container20";
            command.Blob      = "blob12";
            command.ExecuteCmdlet();
            Assert.AreEqual(1, ((MockCommandRuntime)command.CommandRuntime).OutputPipeline.Count);
            blob = (AzureStorageBlob)((MockCommandRuntime)command.CommandRuntime).OutputPipeline.FirstOrDefault();
            Assert.AreEqual("blob12", blob.Name);

            ((MockCommandRuntime)command.CommandRuntime).ResetPipelines();
            command.Container = "container20";
            command.Blob      = "*";
            command.ExecuteCmdlet();
            Assert.AreEqual(20, ((MockCommandRuntime)command.CommandRuntime).OutputPipeline.Count);
        }
Example #2
0
        /// <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, channel.StorageContext, ClientOptions);

            azureBlob.ContinuationToken = continuationToken;
            OutputStream.WriteObject(taskId, azureBlob);
        }
 internal void OnNavigatedTo(BlobDetailNavigationContext context)
 {
     this.StorageAccount = context.Account;
     this.Container      = context.Container;
     this.Blob           = context.Blob;
     InitializeClient();
     Refresh();
 }
Example #4
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);
                }
            }
        }
        public async Task Refresh()
        {
            var fullDetail = await client.GetBlobDetail(this.Blob);

            this.Blob = fullDetail;
            Messenger.Default.Send <PageTitleChangedEvent>(new PageTitleChangedEvent()
            {
                Title = this.Blob.Name
            });
        }
Example #6
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);
        }
Example #7
0
        public void SetAzureBlobContentByContainerWithoutConfirmationTest()
        {
            AddTestBlobs();

            string           fileName  = @"c:\Windows\System32\cmd.exe";
            string           bloburi   = "http://127.0.0.1/account/container20/blob9";
            ICloudBlob       blob      = new CloudBlockBlob(new Uri(bloburi));
            AzureStorageBlob azureBlob = command.SetAzureBlobContent(fileName, blob, false);

            Assert.IsNull(azureBlob);
        }
Example #8
0
        public void GetBlobContentByNameSuccessfullyTest()
        {
            AddTestBlobs();

            string containerName = "container1";
            string blobName      = "blob0";
            string fileName      = string.Empty;

            AzureStorageBlob blob = command.GetBlobContent(containerName, blobName, fileName);

            Assert.AreEqual("blob0", blob.Name);
        }
Example #9
0
        public static AzureStorageBlob GetAzureStorageBlob(BlobItem blobItem, BlobContainerClient track2container, AzureStorageContext context, string continuationToken = null, BlobClientOptions options = null)
        {
            BlobBaseClient   blobClient = Util.GetTrack2BlobClient(track2container, blobItem.Name, context, blobItem.VersionId, blobItem.IsLatestVersion, blobItem.Snapshot, options, blobItem.Properties.BlobType);
            AzureStorageBlob outputblob = new AzureStorageBlob(blobClient, context, options, blobItem);

            if (continuationToken != null)
            {
                BlobContinuationToken token = new BlobContinuationToken();
                token.NextMarker             = continuationToken;
                outputblob.ContinuationToken = token;
            }
            return(outputblob);
        }
Example #10
0
        public static AzureStorageBlob GetAzureStorageBlob(BlobTagItem blobTagItem, BlobContainerClient track2container, AzureStorageContext context, string continuationToken = null, BlobClientOptions options = null)
        {
            BlobBaseClient   blobClient = Util.GetTrack2BlobClient(track2container, blobTagItem.BlobName, context, options: options);
            AzureStorageBlob outputblob = new AzureStorageBlob(blobClient, context, options);

            if (continuationToken != null)
            {
                BlobContinuationToken token = new BlobContinuationToken();
                token.NextMarker             = continuationToken;
                outputblob.ContinuationToken = token;
            }
            return(outputblob);
        }
Example #11
0
        public void ExecuteCommandSetBlobContentTest()
        {
            AddTestContainers();
            string fileName      = @"c:\Windows\System32\cmd.exe";
            string containerName = "test";
            string blobName      = "";

            command.File      = fileName;
            command.Container = containerName;
            command.Blob      = blobName;
            command.ExecuteCmdlet();
            AzureStorageBlob azureblob = (AzureStorageBlob)((MockCommandRuntime)command.CommandRuntime).OutputPipeline.FirstOrDefault();

            Assert.AreEqual("cmd.exe", azureblob.Name);
        }
Example #12
0
        /// <summary>
        /// Write ICloudBlob to output using specified service channel
        /// </summary>
        /// <param name="blob">The output ICloudBlob object</param>
        /// <param name="channel">IStorageBlobManagement channel object</param>
        internal void WriteICloudBlobWithProperties(ICloudBlob blob, IStorageBlobManagement channel = null)
        {
            if (channel == null)
            {
                channel = Channel;
            }

            AccessCondition    accessCondition = null;
            BlobRequestOptions options         = null;

            channel.FetchBlobAttributes(blob, accessCondition, options, OperationContext);
            AzureStorageBlob azureBlob = new AzureStorageBlob(blob);

            WriteObjectWithStorageContext(azureBlob);
        }
Example #13
0
        public void SetAzureBlobContentByContainerSuccessfullyTest()
        {
            AddTestBlobs();

            string     fileName = @"c:\Windows\System32\cmd.exe";
            string     bloburi  = "http://127.0.0.1/account/container20/blob9";
            ICloudBlob blob     = new CloudBlockBlob(new Uri(bloburi));

            command.Confirm = true;
            AzureStorageBlob azureBlob = command.SetAzureBlobContent(fileName, blob, false);

            Assert.AreEqual("blob9", azureBlob.Name);

            command.Force = true;
            azureBlob     = command.SetAzureBlobContent(fileName, blob, false);
            Assert.AreEqual("blob9", azureBlob.Name);
        }
Example #14
0
        public void GetBlobContentByContainerSuccessfullyTest()
        {
            AddTestBlobs();

            CloudBlobContainer container = BlobMock.GetContainerReference("container20");
            string             blobName  = "blob10";
            string             fileName  = string.Empty;
            AzureStorageBlob   blob      = command.GetBlobContent(container, blobName, fileName);

            Assert.AreEqual("blob10", blob.Name);

            container = BlobMock.GetContainerReference("container20");
            blobName  = "blob10";
            fileName  = GetUniqueString();
            blob      = command.GetBlobContent(container, blobName, fileName);
            Assert.AreEqual("blob10", blob.Name);
        }
        /// <summary>
        /// write blobs with storage context
        /// </summary>
        /// <param name="blobList">An enumerable collection of IListBlobItem</param>
        internal void WriteBlobsWithContext(IEnumerable <Tuple <ICloudBlob, BlobContinuationToken> > blobList)
        {
            if (null == blobList)
            {
                return;
            }

            foreach (Tuple <ICloudBlob, BlobContinuationToken> blobItem in blobList)
            {
                if (blobItem == null)
                {
                    continue;
                }

                AzureStorageBlob azureBlob = new AzureStorageBlob(blobItem.Item1);
                azureBlob.ContinuationToken = blobItem.Item2;
                WriteObjectWithStorageContext(azureBlob);
            }
        }
Example #16
0
        /// <summary>
        /// execute command
        /// </summary>
        public override void ExecuteCmdlet()
        {
            AzureStorageBlob blob          = null;
            string           containerName = string.Empty;

            if (BlobProperties != null)
            {
                ValidateBlobProperties(BlobProperties);
            }

            switch (ParameterSetName)
            {
            case ContainerParameterSet:
                blob          = SetAzureBlobContent(FileName, CloudBlobContainer, BlobName);
                containerName = CloudBlobContainer.Name;
                break;

            case BlobParameterSet:
                blob          = SetAzureBlobContent(FileName, ICloudBlob);
                containerName = ICloudBlob.Container.Name;
                break;

            case ManualParameterSet:
            default:
                blob          = SetAzureBlobContent(FileName, ContainerName, BlobName);
                containerName = ContainerName;
                break;
            }

            if (blob == null)
            {
                String result = String.Format(Resources.SendAzureBlobCancelled, FileName, containerName);
                WriteObject(result);
            }
            else
            {
                //set the properties and meta
                SetBlobProperties(blob, BlobProperties);
                SetBlobMeta(blob, Metadata);

                WriteObjectWithStorageContext(blob);
            }
        }
Example #17
0
        public async Task <List <AzureStorageBlob> > GetBlobs(string containerName, string prefix)
        {
            var list = new List <AzureStorageBlob>();

            var flatBlobs         = false;
            var continuationToken = new BlobContinuationToken();
            var requestOptions    = new BlobRequestOptions();
            var cancelToken       = new CancellationToken();
            var opContext         = new OperationContext();

            var container = blobClient.GetContainerReference(containerName);

            var operationOutcome = await container.ListBlobsSegmentedAsync(prefix, flatBlobs, BlobListingDetails.Metadata, 1000, continuationToken, requestOptions, opContext);

            foreach (var item in operationOutcome.Results)
            {
                var newBlobItem = new AzureStorageBlob();

                if (item is CloudBlobDirectory)
                {
                    var blobDir    = item as CloudBlobDirectory;
                    var folderName = blobDir.Prefix.Substring(prefix.Length, blobDir.Prefix.Length - prefix.Length);
                    newBlobItem.Name        = folderName.Replace("/", "");
                    newBlobItem.Path        = blobDir.Prefix;
                    newBlobItem.IsDirectory = true;
                }
                else if (item is ICloudBlob)
                {
                    var blobItem = item as ICloudBlob;
                    newBlobItem.IsDirectory = false;
                    newBlobItem.Uri         = blobItem.Uri;
                    newBlobItem             = await GetBlobDetail(newBlobItem);

                    var fileName = blobItem.Name.Substring(prefix.Length, blobItem.Name.Length - prefix.Length);
                    newBlobItem.Name = fileName;
                    newBlobItem.Path = blobItem.Name;
                }

                list.Add(newBlobItem);
            }

            return(list);
        }
Example #18
0
        /// <summary>
        /// write blobs with storage context
        /// </summary>
        /// <param name="blobList">An enumerable collection of IListBlobItem</param>
        internal void WriteBlobsWithContext(IEnumerable <IListBlobItem> blobList)
        {
            if (null == blobList)
            {
                return;
            }

            foreach (IListBlobItem blobItem in blobList)
            {
                ICloudBlob blob = blobItem as ICloudBlob;

                if (blob == null)
                {
                    continue;
                }

                AzureStorageBlob azureBlob = new AzureStorageBlob(blob);
                WriteObjectWithStorageContext(azureBlob);
            }
        }
Example #19
0
        public void GetBlobContentByICloudBlobSuccessfullyTest()
        {
            AddTestBlobs();
            string         bloburi   = "http://127.0.0.1/account/container1/blob0";
            string         fileName  = string.Empty;
            CloudBlockBlob blockBlob = new CloudBlockBlob(new Uri(bloburi));

            command.Force     = true;
            command.Exception = false;

            AzureStorageBlob blob = command.GetBlobContent(blockBlob, fileName, false);

            Assert.AreEqual("blob0", blob.Name);

            blob = command.GetBlobContent(blockBlob, fileName, true);
            Assert.AreEqual("blob0", blob.Name);

            fileName = @"c:\Windows\System32";
            command.GetBlobContent(blockBlob, fileName, false);
        }
        public override void ExecuteCmdlet()
        {
            SetUpDestinationChannel();

            ICloudBlob destinationBlob = default(ICloudBlob);

            switch (ParameterSetName)
            {
            case NameParameterSet:
                destinationBlob = StartCopyBlob(SrcContainer, SrcBlob, DestContainer, DestBlob);
                break;

            case UriParameterSet:
                destinationBlob = StartCopyBlob(AbsoluteUri, DestContainer, DestBlob, Context);
                break;

            case SrcBlobParameterSet:
                destinationBlob = StartCopyBlob(ICloudBlob, DestContainer, DestBlob);
                break;

            case ContainerPipelineParameterSet:
                destinationBlob = StartCopyBlob(CloudBlobContainer.Name, SrcBlob, DestContainer, DestBlob);
                break;

            case DestBlobPipelineParameterSet:
                destinationBlob = StartCopyBlob(ICloudBlob, DestICloudBlob);
                break;
            }

            if (destinationBlob != null)
            {
                AccessCondition    accessCondition = null;
                BlobRequestOptions options         = null;
                //Make sure we use the dest channel
                destChannel.FetchBlobAttributes(destinationBlob, accessCondition, options, OperationContext);
                AzureStorageBlob azureBlob = new AzureStorageBlob(destinationBlob);
                //Make sure the dest context is piped out
                azureBlob.Context = DestContext;
                WriteObject(azureBlob);
            }
        }
Example #21
0
        public async Task <AzureStorageBlob> GetBlobDetail(AzureStorageBlob blob)
        {
            var blobRef = await blobClient.GetBlobReferenceFromServerAsync(blob.Uri);

            var fullDetailBlob = new AzureStorageBlob();

            fullDetailBlob.BlobType      = blobRef.BlobType.ToString();
            fullDetailBlob.Name          = blobRef.Name;
            fullDetailBlob.LastModified  = blobRef.Properties.LastModified.Value.UtcDateTime;
            fullDetailBlob.LengthInBytes = blobRef.Properties.Length;
            fullDetailBlob.Parent        = blobRef.Parent.Prefix;
            fullDetailBlob.Container     = blobRef.Container.Name;
            fullDetailBlob.ETag          = blobRef.Properties.ETag;
            fullDetailBlob.Uri           = blobRef.Uri;
            // content
            fullDetailBlob.StreamMinimumReadSizeInBytes = blobRef.StreamMinimumReadSizeInBytes;
            fullDetailBlob.StreamWriteSizeInBytes       = blobRef.StreamWriteSizeInBytes;
            fullDetailBlob.CacheControl       = blobRef.Properties.CacheControl;
            fullDetailBlob.ContentDisposition = blobRef.Properties.ContentDisposition;
            fullDetailBlob.ContentEncoding    = blobRef.Properties.ContentEncoding;
            fullDetailBlob.ContentLanguage    = blobRef.Properties.ContentLanguage;
            fullDetailBlob.ContentMD5         = blobRef.Properties.ContentMD5;
            fullDetailBlob.ContentType        = blobRef.Properties.ContentType;
            // lease
            fullDetailBlob.LeaseStatus   = blobRef.Properties.LeaseStatus.ToString();
            fullDetailBlob.LeaseState    = blobRef.Properties.LeaseState.ToString();
            fullDetailBlob.LeaseDuration = blobRef.Properties.LeaseDuration.ToString();
            // snapshot
            fullDetailBlob.IsSnapshot = blobRef.IsSnapshot;
            if (blobRef.SnapshotTime.HasValue)
            {
                fullDetailBlob.SnapshotTime = blobRef.SnapshotTime.Value.UtcDateTime;
            }
            fullDetailBlob.SnapshotQualifiedStorageUri = blobRef.SnapshotQualifiedStorageUri.PrimaryUri;
            fullDetailBlob.SnapshotQualifiedUri        = blobRef.SnapshotQualifiedUri;

            return(fullDetailBlob);
        }
Example #22
0
        public override void ExecuteCmdlet()
        {
            AzureStorageBlob azureBlob     = null;
            string           blobName      = string.Empty;
            string           containerName = string.Empty;

            switch (ParameterSetName)
            {
            case BlobParameterSet:
                azureBlob     = GetBlobContent(ICloudBlob, FileName, true);
                blobName      = ICloudBlob.Name;
                containerName = ICloudBlob.Container.Name;
                break;

            case ContainerParameterSet:
                azureBlob     = GetBlobContent(CloudBlobContainer, BlobName, FileName);
                blobName      = BlobName;
                containerName = CloudBlobContainer.Name;
                break;

            case ManualParameterSet:
                azureBlob     = GetBlobContent(ContainerName, BlobName, FileName);
                blobName      = BlobName;
                containerName = ContainerName;
                break;
            }

            if (azureBlob == null)
            {
                String result = String.Format(Resources.DownloadBlobCancelled, blobName, containerName);
                WriteObject(result);
            }
            else
            {
                WriteObjectWithStorageContext(azureBlob);
            }
        }
        /// <summary>
        /// write blobs with storage context
        /// </summary>
        /// <param name="blobList">An enumerable collection of IListBlobItem</param>
        internal void WriteBlobsWithContext(IEnumerable<IListBlobItem> blobList)
        {
            if (null == blobList)
            {
                return;
            }

            foreach (IListBlobItem blobItem in blobList)
            {
                ICloudBlob blob = blobItem as ICloudBlob;
                
                if (blob == null)
                {
                    continue;
                }

                AzureStorageBlob azureBlob = new AzureStorageBlob(blob);
                WriteObjectWithStorageContext(azureBlob);
            }
        }
        /// <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);
            }
        }
 /// <summary>
 /// set blob properties
 /// </summary>
 /// <param name="azureBlob">azure storage blob object</param>
 /// <param name="meta">blob properties hashtable</param>
 private void SetBlobProperties(AzureStorageBlob azureBlob, Hashtable properties)
 {
     SetBlobProperties(azureBlob.ICloudBlob, properties);
 }
Example #26
0
        /// <summary>
        /// Upload File to blob with storage Client library API
        /// </summary>
        internal virtual async Task UploadBlobwithSdk(long taskId, IStorageBlobManagement localChannel, string filePath, StorageBlob.CloudBlob blob)
        {
            BlobClientOptions options = this.ClientOptions;

            if (!string.IsNullOrEmpty(this.EncryptionScope))
            {
                options = SetClientOptionsWithEncryptionScope(this.EncryptionScope);
            }

            if (this.Force.IsPresent ||
                !blob.Exists() ||
                ShouldContinue(string.Format(Resources.OverwriteConfirmation, blob.Uri), null))
            {
                // Prepare blob Properties, MetaData, accessTier
                BlobHttpHeaders blobHttpHeaders       = CreateBlobHttpHeaders(BlobProperties);
                IDictionary <string, string> metadata = new Dictionary <string, string>();
                SetBlobMeta_Track2(metadata, this.Metadata);
                AccessTier?accesstier = GetAccessTier_Track2(this.standardBlobTier, this.pageBlobTier);

                //Prepare progress handler
                long             fileSize        = new FileInfo(ResolvedFileName).Length;
                string           activity        = String.Format(Resources.SendAzureBlobActivity, this.File, blob.Name, blob.Container.Name);
                string           status          = Resources.PrepareUploadingBlob;
                ProgressRecord   pr              = new ProgressRecord(OutputStream.GetProgressId(taskId), activity, status);
                IProgress <long> progressHandler = new Progress <long>((finishedBytes) =>
                {
                    if (pr != null)
                    {
                        // Size of the source file might be 0, when it is, directly treat the progress as 100 percent.
                        pr.PercentComplete   = 0 == fileSize ? 100 : (int)(finishedBytes * 100 / fileSize);
                        pr.StatusDescription = string.Format(CultureInfo.CurrentCulture, Resources.FileTransmitStatus, pr.PercentComplete);
                        Console.WriteLine(finishedBytes);
                        this.OutputStream.WriteProgress(pr);
                    }
                });

                BlobBaseClient outputBlobClient = null;
                using (FileStream stream = System.IO.File.OpenRead(ResolvedFileName))
                {
                    //block blob
                    if (string.Equals(blobType, BlockBlobType, StringComparison.InvariantCultureIgnoreCase))
                    {
                        BlobClient blobClient = GetTrack2BlobClient(blob, localChannel.StorageContext, options);
                        outputBlobClient = blobClient;
                        StorageTransferOptions trasnferOption = new StorageTransferOptions()
                        {
                            MaximumConcurrency = this.GetCmdletConcurrency()
                        };
                        BlobUploadOptions uploadOptions = new BlobUploadOptions();
                        if (this.BlobTag != null)
                        {
                            uploadOptions.Tags = this.BlobTag.Cast <DictionaryEntry>().ToDictionary(d => (string)d.Key, d => (string)d.Value);
                        }
                        uploadOptions.Metadata        = metadata;
                        uploadOptions.HttpHeaders     = blobHttpHeaders;
                        uploadOptions.Conditions      = this.BlobRequestConditions;
                        uploadOptions.AccessTier      = accesstier;
                        uploadOptions.ProgressHandler = progressHandler;
                        uploadOptions.TransferOptions = trasnferOption;

                        await blobClient.UploadAsync(stream, uploadOptions, CmdletCancellationToken).ConfigureAwait(false);
                    }
                    //Page or append blob
                    else if (string.Equals(blobType, PageBlobType, StringComparison.InvariantCultureIgnoreCase) ||
                             string.Equals(blobType, AppendBlobType, StringComparison.InvariantCultureIgnoreCase))
                    {
                        PageBlobClient   pageblobClient   = null;
                        AppendBlobClient appendblobClient = null;

                        //Create Blob
                        if (string.Equals(blobType, PageBlobType, StringComparison.InvariantCultureIgnoreCase)) //page
                        {
                            if (fileSize % 512 != 0)
                            {
                                throw new ArgumentException(String.Format("File size {0} Bytes is invalid for PageBlob, must be a multiple of 512 bytes.", fileSize.ToString()));
                            }
                            pageblobClient   = GetTrack2PageBlobClient(blob, localChannel.StorageContext, options);
                            outputBlobClient = pageblobClient;
                            PageBlobCreateOptions createOptions = new PageBlobCreateOptions();
                            if (this.BlobTag != null)
                            {
                                createOptions.Tags = this.BlobTag.Cast <DictionaryEntry>().ToDictionary(d => (string)d.Key, d => (string)d.Value);
                            }
                            createOptions.Metadata    = metadata;
                            createOptions.HttpHeaders = blobHttpHeaders;
                            createOptions.Conditions  = this.PageBlobRequestConditions;
                            Response <BlobContentInfo> blobInfo = await pageblobClient.CreateAsync(fileSize, createOptions, CmdletCancellationToken).ConfigureAwait(false);
                        }
                        else //append
                        {
                            appendblobClient = GetTrack2AppendBlobClient(blob, localChannel.StorageContext, options);
                            outputBlobClient = appendblobClient;
                            AppendBlobCreateOptions createOptions = new AppendBlobCreateOptions();
                            if (this.BlobTag != null)
                            {
                                createOptions.Tags = this.BlobTag.Cast <DictionaryEntry>().ToDictionary(d => (string)d.Key, d => (string)d.Value);
                            }
                            createOptions.Metadata    = metadata;
                            createOptions.HttpHeaders = blobHttpHeaders;
                            createOptions.Conditions  = this.AppendBlobRequestConditions;
                            Response <BlobContentInfo> blobInfo = await appendblobClient.CreateAsync(createOptions, CmdletCancellationToken).ConfigureAwait(false);
                        }

                        // Upload blob content
                        byte[] uploadcache4MB = null;
                        byte[] uploadcache    = null;
                        progressHandler.Report(0);
                        long offset = 0;
                        while (offset < fileSize)
                        {
                            // Get chunk size and prepare cache
                            int chunksize = size4MB;
                            if (chunksize <= (fileSize - offset)) // Chunk size will be 4MB
                            {
                                if (uploadcache4MB == null)
                                {
                                    uploadcache4MB = new byte[size4MB];
                                }
                                uploadcache = uploadcache4MB;
                            }
                            else // last chunk can < 4MB
                            {
                                chunksize = (int)(fileSize - offset);
                                if (uploadcache4MB == null)
                                {
                                    uploadcache = new byte[chunksize];
                                }
                                else
                                {
                                    uploadcache = uploadcache4MB;
                                }
                            }

                            //Get content to upload for the chunk
                            int readoutcount = await stream.ReadAsync(uploadcache, 0, (int)chunksize).ConfigureAwait(false);

                            MemoryStream chunkContent = new MemoryStream(uploadcache, 0, readoutcount);

                            //Upload content
                            if (string.Equals(blobType, PageBlobType, StringComparison.InvariantCultureIgnoreCase)) //page
                            {
                                Response <PageInfo> pageInfo = await pageblobClient.UploadPagesAsync(chunkContent, offset, null, null, null, CmdletCancellationToken).ConfigureAwait(false);
                            }
                            else //append
                            {
                                Response <BlobAppendInfo> pageInfo = await appendblobClient.AppendBlockAsync(chunkContent, null, null, null, CmdletCancellationToken).ConfigureAwait(false);
                            }

                            // Update progress
                            offset += readoutcount;
                            progressHandler.Report(offset);
                        }
                        if (string.Equals(blobType, PageBlobType, StringComparison.InvariantCultureIgnoreCase) && accesstier != null)
                        {
                            await pageblobClient.SetAccessTierAsync(accesstier.Value, cancellationToken : CmdletCancellationToken).ConfigureAwait(false);
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException(string.Format(
                                                                CultureInfo.CurrentCulture,
                                                                Resources.InvalidBlobType,
                                                                blobType,
                                                                BlobName));
                    }
                }
                AzureStorageBlob outputBlob = new AzureStorageBlob(outputBlobClient, localChannel.StorageContext, null, ClientOptions);
                OutputStream.WriteObject(taskId, outputBlob);
            }
        }
Example #27
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>
 /// set blob meta
 /// </summary>
 /// <param name="azureBlob">azure storage blob object</param>
 /// <param name="meta">meta data hashtable</param>
 private void SetBlobMeta(AzureStorageBlob azureBlob, Hashtable meta)
 {
     SetBlobMeta(azureBlob.ICloudBlob, meta);
 }
Example #30
0
        /// <summary>
        /// list blobs by blob name and container name
        /// </summary>
        /// <param name="containerName">container name</param>
        /// <param name="blobName">blob name pattern</param>
        /// <returns>An enumerable collection of IListBlobItem</returns>
        internal async Task ListBlobsByName(long taskId, IStorageBlobManagement localChannel, string containerName, string blobName, bool includeDeleted = false, bool includeVersion = false)
        {
            CloudBlobContainer container       = null;
            BlobRequestOptions requestOptions  = RequestOptions;
            AccessCondition    accessCondition = null;

            string prefix = string.Empty;

            if (String.IsNullOrEmpty(blobName) || WildcardPattern.ContainsWildcardCharacters(blobName) || includeDeleted)
            {
                container = await GetCloudBlobContainerByName(localChannel, containerName).ConfigureAwait(false);

                prefix = NameUtil.GetNonWildcardPrefix(blobName);
                WildcardOptions options  = WildcardOptions.IgnoreCase | WildcardOptions.Compiled;
                WildcardPattern wildcard = null;

                if (!String.IsNullOrEmpty(blobName))
                {
                    wildcard = new WildcardPattern(blobName, options);
                }

                Func <string, bool> blobFilter = (blobNameToFilte) => wildcard == null || wildcard.IsMatch(blobNameToFilte);
                await ListBlobsByPrefix(taskId, localChannel, containerName, prefix, blobFilter, includeDeleted, IncludeVersion).ConfigureAwait(false);
            }
            else
            {
                container = await GetCloudBlobContainerByName(localChannel, containerName, true).ConfigureAwait(false);

                BlobContainerClient track2container = AzureStorageContainer.GetTrack2BlobContainerClient(container, localChannel.StorageContext, ClientOptions);

                if (!NameUtil.IsValidBlobName(blobName))
                {
                    throw new ArgumentException(String.Format(Resources.InvalidBlobName, blobName));
                }

                BlobBaseClient blobClient = null;
                if (UseTrack2Sdk()) // User Track2 SDK
                {
                    blobClient = Util.GetTrack2BlobClient(track2container, blobName, localChannel.StorageContext, this.VersionId, false, this.SnapshotTime is null ? null : this.SnapshotTime.Value.ToString("o"), ClientOptions);
                    global::Azure.Storage.Blobs.Models.BlobProperties blobProperties;
                    try
                    {
                        blobProperties = blobClient.GetProperties(BlobRequestConditions, cancellationToken: CmdletCancellationToken);
                    }
                    catch (global::Azure.RequestFailedException e) when(e.Status == 404)
                    {
                        throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, containerName));
                    }
                    blobClient = Util.GetTrack2BlobClient(track2container, blobName, localChannel.StorageContext, this.VersionId, blobProperties.IsLatestVersion, this.SnapshotTime is null ? null : this.SnapshotTime.Value.ToString("o"), ClientOptions, blobProperties.BlobType);

                    AzureStorageBlob outputBlob = new AzureStorageBlob(blobClient, localChannel.StorageContext, blobProperties, ClientOptions);
                    OutputStream.WriteObject(taskId, outputBlob);
                }
                else // Use Track1 SDK
                {
                    CloudBlob blob = await localChannel.GetBlobReferenceFromServerAsync(container, blobName, this.SnapshotTime, accessCondition, requestOptions, OperationContext, CmdletCancellationToken).ConfigureAwait(false);

                    if (null == blob)
                    {
                        throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, containerName));
                    }
                    else
                    {
                        OutputStream.WriteObject(taskId, new AzureStorageBlob(blob, localChannel.StorageContext, ClientOptions));
                    }
                }
            }
        }
Example #31
0
 public HomeController()
 {
     productoInventRepository = new ProductoInventRepository();
     azureStorageblob         = new AzureStorageBlob();
 }
Example #32
0
 /// <summary>
 /// set blob properties
 /// </summary>
 /// <param name="azureBlob">azure storage blob object</param>
 /// <param name="meta">blob properties hashtable</param>
 private void SetBlobProperties(AzureStorageBlob azureBlob, Hashtable properties)
 {
     SetBlobProperties(azureBlob.ICloudBlob, properties);
 }
Example #33
0
 /// <summary>
 /// set blob meta
 /// </summary>
 /// <param name="azureBlob">azure storage blob object</param>
 /// <param name="meta">meta data hashtable</param>
 private void SetBlobMeta(AzureStorageBlob azureBlob, Hashtable meta)
 {
     SetBlobMeta(azureBlob.ICloudBlob, meta);
 }
        public override void ExecuteCmdlet()
        {
            SetUpDestinationChannel();

            ICloudBlob destinationBlob = default(ICloudBlob);

            switch (ParameterSetName)
            {
                case NameParameterSet:
                    destinationBlob = StartCopyBlob(SrcContainer, SrcBlob, DestContainer, DestBlob);
                    break;

                case UriParameterSet:
                    destinationBlob = StartCopyBlob(AbsoluteUri, DestContainer, DestBlob, Context);
                    break;

                case SrcBlobParameterSet:
                    destinationBlob = StartCopyBlob(ICloudBlob, DestContainer, DestBlob);
                    break;

                case ContainerPipelineParameterSet:
                    destinationBlob = StartCopyBlob(CloudBlobContainer.Name, SrcBlob, DestContainer, DestBlob);
                    break;

                case DestBlobPipelineParameterSet:
                    destinationBlob = StartCopyBlob(ICloudBlob, DestICloudBlob);
                    break;
            }

            if (destinationBlob != null)
            {
                AccessCondition accessCondition = null;
                BlobRequestOptions options = null;
                //Make sure we use the dest channel
                destChannel.FetchBlobAttributes(destinationBlob, accessCondition, options, OperationContext);
                AzureStorageBlob azureBlob = new AzureStorageBlob(destinationBlob);
                //Make sure the dest context is piped out
                azureBlob.Context = DestContext;
                WriteObject(azureBlob);
            }
        }
        /// <summary>
        /// write blobs with storage context
        /// </summary>
        /// <param name="blobList">An enumerable collection of IListBlobItem</param>
        internal void WriteBlobsWithContext(IEnumerable<Tuple<ICloudBlob, BlobContinuationToken>> blobList)
        {
            if (null == blobList)
            {
                return;
            }

            foreach (Tuple<ICloudBlob, BlobContinuationToken> blobItem in blobList)
            {
                if (blobItem == null)
                {
                    continue;
                }

                AzureStorageBlob azureBlob = new AzureStorageBlob(blobItem.Item1);
                azureBlob.ContinuationToken = blobItem.Item2;
                WriteObjectWithStorageContext(azureBlob);
            }
        }