Esempio n. 1
0
 /// <summary>
 /// Azure storage blob constructor
 /// </summary>
 /// <param name="blob">ICloud blob object</param>
 public AzureStorageBlob(CloudBlob blob, AzureStorageContext storageContext)
 {
     Name       = blob.Name;
     ICloudBlob = blob;
     BlobType   = blob.BlobType;
     Length     = blob.Properties.Length;
     IsDeleted  = blob.IsDeleted;
     RemainingDaysBeforePermanentDelete = blob.Properties.RemainingDaysBeforePermanentDelete;
     ContentType  = blob.Properties.ContentType;
     LastModified = blob.Properties.LastModified;
     SnapshotTime = blob.SnapshotTime;
     this.Context = storageContext;
 }
        public void GetStorageAccountByConnectionStringAndSasToken()
        {
            // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine")]
            string sasToken             = "?st=2013-09-03T04%3A12%3A15Z&se=2013-09-03T05%3A12%3A15Z&sr=c&sp=r&sig=fN2NPxLK99tR2%2BWnk48L3lMjutEj7nOwBo7MXs2hEV8%3D";
            string endpoint             = "http://storageaccountname.blob.core.windows.net";
            string connectionString     = String.Format("BlobEndpoint={0};QueueEndpoint={0};TableEndpoint={0};SharedAccessSignature={1}", endpoint, sasToken);
            CloudStorageAccount account = command.GetStorageAccountByConnectionString(connectionString);
            AzureStorageContext context = new AzureStorageContext(account);

            connectionString = String.Format("BlobEndpoint={0};SharedAccessSignature={1}", endpoint, sasToken);
            account          = command.GetStorageAccountByConnectionString(connectionString);
            context          = new AzureStorageContext(account);
        }
Esempio n. 3
0
 /// <summary>
 /// Azure storage file constructor from Track2 get file properties output
 /// </summary>
 /// <param name="file">Cloud file object</param>
 public AzureStorageFile(ShareFileClient shareFileClient, AzureStorageContext storageContext, ShareFileProperties shareFileProperties = null, ShareClientOptions clientOptions = null)
 {
     Name = shareFileClient.Name;
     this.privateFileClient = shareFileClient;
     CloudFile = GetTrack1FileClient(shareFileClient, storageContext.StorageAccount.Credentials);
     if (shareFileProperties != null)
     {
         privateFileProperties = shareFileProperties;
         Length       = shareFileProperties.ContentLength;
         LastModified = shareFileProperties.LastModified;
     }
     Context            = storageContext;
     shareClientOptions = clientOptions;
 }
Esempio n. 4
0
        internal AzureStorageContext GetCmdletStorageContext(IStorageContext inContext, bool outputErrorMessage = true)
        {
            var context = inContext as AzureStorageContext;

            if (context == null && inContext != null)
            {
                context = new AzureStorageContext(inContext.GetCloudStorageAccount(), null, DefaultContext, WriteDebug);
            }

            if (context != null)
            {
                WriteDebugLog(String.Format(Resources.UseStorageAccountFromContext, context.StorageAccountName));
            }
            else
            {
                CloudStorageAccount account = null;
                string storageAccount;
                try
                {
                    if (TryGetStorageAccount(DefaultProfile, out storageAccount) ||
                        TryGetStorageAccount(RMProfile, out storageAccount) ||
                        TryGetStorageAccount(SMProfile, out storageAccount) ||
                        TryGetStorageAccountFromEnvironmentVariable(out storageAccount))
                    {
                        account = GetStorageAccountFromConnectionString(storageAccount);
                    }
                    else
                    {
                        throw new InvalidOperationException("Could not get the storage context.  Please pass in a storage context or set the current storage context.");
                    }
                }
                catch (Exception e)
                {
                    if (outputErrorMessage)
                    {
                        //stop the pipeline if storage account is missed.
                        WriteTerminatingError(e);
                    }
                    else
                    {
                        throw;
                    }
                }

                //Set the storage context and use it in pipeline
                context = new AzureStorageContext(account, null, DefaultContext, WriteDebug);
            }

            return(context);
        }
Esempio n. 5
0
        private void SetProperties(BlobBaseClient track2BlobClient, AzureStorageContext storageContext, global::Azure.Storage.Blobs.Models.BlobProperties blobProperties = null, BlobClientOptions options = null)
        {
            if (blobProperties == null)
            {
                try
                {
                    privateBlobProperties = track2BlobClient.GetProperties().Value;
                }
                catch (global::Azure.RequestFailedException e) when(e.Status == 403 || e.Status == 404)
                {
                    // privateBlobProperties will be null when there are no permission to get blob proeprties, or blob is already deleted.
                }
            }
            else
            {
                privateBlobProperties = blobProperties;
            }

            this.privateBlobBaseClient = track2BlobClient;
            Name                 = track2BlobClient.Name;
            this.Context         = storageContext;
            privateClientOptions = options;
            ICloudBlob           = GetTrack1Blob(track2BlobClient, storageContext.StorageAccount.Credentials, privateBlobProperties.BlobType);
            if (!(ICloudBlob is InvalidCloudBlob))
            {
                BlobType     = ICloudBlob.BlobType;
                SnapshotTime = ICloudBlob.SnapshotTime;
            }
            else // This code might should not be necessary, since currently only blob version will has Track1 Blob as null, and blob veresion won't have snapshot time
            {
                SnapshotTime = Util.GetSnapshotTimeFromBlobUri(track2BlobClient.Uri);
            }

            // Set the AzureStorageBlob Properties
            if (privateBlobProperties != null)
            {
                Length          = privateBlobProperties.ContentLength;
                ContentType     = privateBlobProperties.ContentType;
                LastModified    = privateBlobProperties.LastModified;
                VersionId       = privateBlobProperties.VersionId;
                IsLatestVersion = privateBlobProperties.IsLatestVersion;
                if (ICloudBlob is InvalidCloudBlob)
                {
                    BlobType = Util.convertBlobType_Track2ToTrack1(privateBlobProperties.BlobType);
                }
                AccessTier = privateBlobProperties.AccessTier is null ? null : privateBlobProperties.AccessTier.ToString();
                TagCount   = privateBlobProperties.TagCount;
            }
        }
 public SetAzureServiceDiagnosticsExtensionCmdletInfo(string service, AzureStorageContext storageContext, string config, string[] roles, string slot)
 {
     this.cmdletName = Utilities.SetAzureServiceDiagnosticsExtensionCmdletName;
     this.cmdletParams.Add(new CmdletParam("ServiceName", service));
     this.cmdletParams.Add(new CmdletParam("StorageContext", storageContext));
     this.cmdletParams.Add(new CmdletParam("Slot", slot));
     if (roles != null)
     {
         this.cmdletParams.Add(new CmdletParam("Role", roles));
     }
     if (config != null)
     {
         this.cmdletParams.Add(new CmdletParam("DiagnosticsConfigurationPath", config));
     }
 }
Esempio n. 7
0
        public override void ExecuteCmdlet()
        {
            CloudStorageAccount account = null;
            bool useHttps = (StorageNouns.HTTPS.ToLower() == protocolType.ToLower());

            switch (ParameterSetName)
            {
            case AccountNameKeyParameterSet:
                account = GetStorageAccountByNameAndKey(StorageAccountName, StorageAccountKey, useHttps, storageEndpoint);
                break;

            case AccountNameKeyEnvironmentParameterSet:
                account = GetStorageAccountByNameAndKeyFromAzureEnvironment(StorageAccountName, StorageAccountKey,
                                                                            useHttps, environmentName);
                break;

            case SasTokenParameterSet:
                account = GetStorageAccountBySasToken(StorageAccountName, SasToken, useHttps, storageEndpoint);
                break;

            case SasTokenEnvironmentParameterSet:
                account = GetStorageAccountBySasTokenFromAzureEnvironment(StorageAccountName, SasToken, useHttps, environmentName);
                break;

            case ConnectionStringParameterSet:
                account = GetStorageAccountByConnectionString(ConnectionString);
                break;

            case LocalParameterSet:
                account = GetLocalDevelopmentStorageAccount();
                break;

            case AnonymousParameterSet:
                account = GetAnonymousStorageAccount(StorageAccountName, useHttps, storageEndpoint);
                break;

            case AnonymousEnvironmentParameterSet:
                account = GetAnonymousStorageAccountFromAzureEnvironment(StorageAccountName, useHttps, environmentName);
                break;

            default:
                throw new ArgumentException(Resources.DefaultStorageCredentialsNotFound);
            }

            AzureStorageContext context = new AzureStorageContext(account);

            WriteObject(context);
        }
Esempio n. 8
0
 /// <summary>
 /// Azure storage file constructor from Track2 list file item
 /// </summary>
 /// <param name="file">Cloud file object</param>
 public AzureStorageFileDirectory(ShareDirectoryClient shareDirectoryClient, AzureStorageContext storageContext, ShareFileItem shareFileItem, ShareClientOptions clientOptions = null)
 {
     Name = shareDirectoryClient.Name;
     this.privateFileDirClient = shareDirectoryClient;
     CloudFileDirectory        = GetTrack1FileDirClient(shareDirectoryClient, storageContext.StorageAccount.Credentials);
     if (shareFileItem != null)
     {
         ListFileProperties = shareFileItem;
         if (shareFileItem.Properties != null)
         {
             LastModified = shareFileItem.Properties.LastModified;
         }
     }
     Context            = storageContext;
     shareClientOptions = clientOptions;
 }
        /// <summary>
        /// Storage table management constructor
        /// </summary>
        /// <param name="client">Cloud table client</param>
        public StorageTableManagement(AzureStorageContext context)
        {
            internalStorageContext = context;

            TableClientOptions clientOptions = new TableClientOptions();

            clientOptions.AddPolicy(new UserAgentPolicy(ApiConstants.UserAgentHeaderValue), HttpPipelinePosition.PerCall);

            if (!context.StorageAccount.Credentials.IsToken)
            {
                tableClient = internalStorageContext.TableStorageAccount.CreateCloudTableClient();
            }
            else
            {
                tableServiceClient = new TableServiceClient(context.StorageAccount.TableEndpoint, context.Track2OauthToken, clientOptions);
            }
        }
        public AzureStorageContainer(BlobContainerClient container, AzureStorageContext storageContext, BlobContainerProperties properties = null)
        {
            Name = container.Name;
            privateBlobContainerClient     = container;
            cloudBlobContainer             = GetTrack1BlobContainer(privateBlobContainerClient, storageContext.StorageAccount.Credentials);
            privateBlobContainerProperties = properties;

            if (privateBlobContainerProperties == null)
            {
                LastModified = null;
            }
            else
            {
                LastModified = privateBlobContainerProperties.LastModified;
            }
            this.Context = storageContext;
        }
Esempio n. 11
0
 /// <summary>
 /// Azure storage blob constructor
 /// </summary>
 /// <param name="blob">ICloud blob object</param>
 public AzureStorageBlob(CloudBlob blob, AzureStorageContext storageContext, BlobClientOptions options = null)
 {
     Name       = blob.Name;
     ICloudBlob = blob;
     BlobType   = blob.BlobType;
     Length     = blob.Properties.Length;
     IsDeleted  = blob.IsDeleted;
     RemainingDaysBeforePermanentDelete = blob.Properties.RemainingDaysBeforePermanentDelete;
     ContentType  = blob.Properties.ContentType;
     LastModified = blob.Properties.LastModified;
     SnapshotTime = blob.SnapshotTime;
     this.Context = storageContext;
     this.privateClientOptions = options;
     AccessTier = blob.Properties.StandardBlobTier is null ?
                  (blob.Properties.PremiumPageBlobTier is null ? null : blob.Properties.PremiumPageBlobTier.ToString())
         : blob.Properties.StandardBlobTier.ToString();
 }
        public async Task Create_Write_Read()
        {
            var azureContext = new AzureStorageContext("UseDevelopmentStorage=true;");
            var storage      = new AzureBlobStorage(azureContext, "test-catalyst-data");

            var buffer = new byte[40000];

            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = (byte)((i % 255) + 1);
            }
            var inStream = new MemoryStream(buffer);

            inStream.Position = 0;
            await storage.AppendAsync(inStream, "models/pos-taggers/en/01/model1");

            inStream.Position = 0;
            await storage.AppendAsync(inStream, "models/pos-taggers/uk/05/model2");

            inStream.Position = 0;
            await storage.AppendAsync(inStream, "models/ner/en/02/model3");

            inStream.Position = 0;
            await storage.AppendAsync(inStream, "models/ner/uk/07/model4");

            inStream.Position = 0;
            await storage.AppendAsync(inStream, "models/unidep/en/03/model5");

            var list = storage.ListFiles("", "", System.IO.SearchOption.AllDirectories);

            list.Should().NotBeEmpty().And.HaveCount(5);

            var outStream = await storage.OpenStreamAsync("models/unidep/en/03/model5", FileAccess.Read);

            var memoryStream = new MemoryStream(40000);

            outStream.CopyTo(memoryStream);
            var bytes = memoryStream.GetBuffer();

            bytes.Should().HaveCount(40000);

            bytes[256].Should().Be(2);
        }
Esempio n. 13
0
        /// <summary>
        /// Azure storage blob constructor
        /// </summary>
        /// <param name="blob">ICloud blob object</param>
        public AzureStorageBlob(BlobBaseClient track2BlobClient, AzureStorageContext storageContext, BlobClientOptions options = null, BlobItem listBlobItem = null)
        {
            if (listBlobItem == null)
            {
                SetProperties(track2BlobClient, storageContext, track2BlobClient.GetProperties().Value, options);
                return;
            }

            this.privateBlobBaseClient = track2BlobClient;
            Name                 = track2BlobClient.Name;
            this.Context         = storageContext;
            privateClientOptions = options;
            ICloudBlob           = GetTrack1Blob(track2BlobClient, storageContext.StorageAccount.Credentials, listBlobItem.Properties.BlobType);
            if (!(ICloudBlob is InvalidCloudBlob))
            {
                BlobType     = ICloudBlob.BlobType;
                SnapshotTime = ICloudBlob.SnapshotTime;
            }
            else
            {
                BlobType = Util.convertBlobType_Track2ToTrack1(listBlobItem.Properties.BlobType);
                if (listBlobItem.Snapshot != null)
                {
                    SnapshotTime = DateTimeOffset.Parse(listBlobItem.Snapshot);
                }
            }

            // Set the AzureStorageBlob Properties
            Length    = listBlobItem.Properties.ContentLength is null ? 0 : listBlobItem.Properties.ContentLength.Value;
            IsDeleted = listBlobItem.Deleted;
            RemainingDaysBeforePermanentDelete = listBlobItem.Properties.RemainingRetentionDays;
            ContentType     = listBlobItem.Properties.ContentType;
            LastModified    = listBlobItem.Properties.LastModified;
            VersionId       = listBlobItem.VersionId;
            IsLatestVersion = listBlobItem.IsLatestVersion;
            AccessTier      = listBlobItem.Properties.AccessTier is null? null: listBlobItem.Properties.AccessTier.ToString();
            if (listBlobItem.Tags != null)
            {
                Tags     = listBlobItem.Tags.ToHashtable();
                TagCount = listBlobItem.Tags.Count;
            }
        }
        /// <summary>
        /// Attempts to get the user's credentials from the given Storage Context or the current subscription, if the former is null. 
        /// Throws a terminating error if the credentials cannot be determined.
        /// </summary>
        public static StorageCredentials GetStorageCredentials(this ServiceManagementBaseCmdlet cmdlet, AzureStorageContext storageContext)
        {
            StorageCredentials credentials = null;

            if (storageContext != null)
            {
                credentials = storageContext.StorageAccount.Credentials;
            }
            else
            {
                var storageAccountName = cmdlet.CurrentSubscription.CurrentStorageAccountName;
                
                if (!string.IsNullOrEmpty(storageAccountName))
                {
                    var keys = cmdlet.StorageClient.StorageAccounts.GetKeys(storageAccountName);
                    
                    if (keys != null)
                    {
                        var storageAccountKey = string.IsNullOrEmpty(keys.PrimaryKey) ? keys.SecondaryKey : keys.PrimaryKey;

                        credentials = new StorageCredentials(storageAccountName, storageAccountKey);
                    }
                }
            }

            if (credentials == null)
            {
                cmdlet.ThrowTerminatingError(
                    new ErrorRecord(
                        new UnauthorizedAccessException(Resources.AzureVMDscDefaultStorageCredentialsNotFound),
                        string.Empty,
                        ErrorCategory.PermissionDenied,
                        null));
            }

            if (string.IsNullOrEmpty(credentials.AccountName))
            {
                cmdlet.ThrowInvalidArgumentError(Resources.AzureVMDscStorageContextMustIncludeAccountName);
            }

            return credentials;
        }
        public SetAzureVMDscExtensionCmdletInfo(
            string              version,
            IPersistentVM       vm,
            string              configurationArchive,
            AzureStorageContext storageContext = null,
            string              containerName = null,
            string              configurationName = null,
            Hashtable           configurationArgument = null,
            string              configurationDataPath = null
        ) 
        {
            cmdletName = Utilities.SetAzureVMDscExtensionCmdletName;

            cmdletParams.AddRange(
                new CmdletParam [] {
                    new CmdletParam("Version", version),
                    new CmdletParam("VM", vm),
                    new CmdletParam("ConfigurationArchive", configurationArchive),
                });

            if (storageContext != null)
            {
                cmdletParams.Add(new CmdletParam("StorageContext", storageContext));
            }
            if (containerName != null)
            {
                cmdletParams.Add(new CmdletParam("ContainerName", containerName));
            }
            if (configurationName != null)
            {
                cmdletParams.Add(new CmdletParam("ConfigurationName", configurationName));
            }
            if (configurationArgument != null)
            {
                cmdletParams.Add(new CmdletParam("ConfigurationArgument", configurationArgument));
            }
            if (configurationDataPath != null)
            {
                cmdletParams.Add(new CmdletParam("ConfigurationDataPath", configurationDataPath));
            }
        }
        public SetAzureVMDscExtensionCmdletInfo(
            string version,
            IPersistentVM vm,
            string configurationArchive,
            AzureStorageContext storageContext = null,
            string containerName            = null,
            string configurationName        = null,
            Hashtable configurationArgument = null,
            string configurationDataPath    = null
            )
        {
            cmdletName = Utilities.SetAzureVMDscExtensionCmdletName;

            cmdletParams.AddRange(
                new CmdletParam [] {
                new CmdletParam("Version", version),
                new CmdletParam("VM", vm),
                new CmdletParam("ConfigurationArchive", configurationArchive),
            });

            if (storageContext != null)
            {
                cmdletParams.Add(new CmdletParam("StorageContext", storageContext));
            }
            if (containerName != null)
            {
                cmdletParams.Add(new CmdletParam("ContainerName", containerName));
            }
            if (configurationName != null)
            {
                cmdletParams.Add(new CmdletParam("ConfigurationName", configurationName));
            }
            if (configurationArgument != null)
            {
                cmdletParams.Add(new CmdletParam("ConfigurationArgument", configurationArgument));
            }
            if (configurationDataPath != null)
            {
                cmdletParams.Add(new CmdletParam("ConfigurationDataPath", configurationDataPath));
            }
        }
Esempio n. 17
0
        public static BlobServiceClient GetTrack2BlobServiceClient(AzureStorageContext context, BlobClientOptions options = null)
        {
            BlobServiceClient blobServiceClient;

            if (context.StorageAccount.Credentials.IsToken) //Oauth
            {
                blobServiceClient = new BlobServiceClient(context.StorageAccount.BlobEndpoint, context.Track2OauthToken, options);
            }
            else  //sas , key or Anonymous, use connection string
            {
                string connectionString = context.ConnectionString;

                // remove the "?" at the begin of SAS if any
                if (context.StorageAccount.Credentials.IsSAS)
                {
                    connectionString = connectionString.Replace("SharedAccessSignature=?", "SharedAccessSignature=");
                }
                blobServiceClient = new BlobServiceClient(connectionString, options);
            }
            return(blobServiceClient);
        }
Esempio n. 18
0
        // Convert Track1 Blob object to Track 2 blob Client
        public static BlobClient GetTrack2BlobClient(CloudBlob cloubBlob, AzureStorageContext context, BlobClientOptions options = null)
        {
            BlobClient blobClient;

            if (cloubBlob.ServiceClient.Credentials.IsToken) //Oauth
            {
                if (context == null)
                {
                    //TODO : Get Oauth context from current login user.
                    throw new System.Exception("Need Storage Context to convert Track1 Blob object in token credentail to Track2 Blob object.");
                }
                blobClient = new BlobClient(cloubBlob.SnapshotQualifiedUri, context.Track2OauthToken, options);
            }
            else if (cloubBlob.ServiceClient.Credentials.IsSAS) //SAS
            {
                string sas     = Util.GetSASStringWithoutQuestionMark(cloubBlob.ServiceClient.Credentials.SASToken);
                string fullUri = cloubBlob.SnapshotQualifiedUri.ToString();
                if (cloubBlob.IsSnapshot)
                {
                    // Since snapshot URL already has '?', need remove '?' in the first char of sas
                    fullUri = fullUri + "&" + sas;
                }
                else
                {
                    fullUri = fullUri + "?" + sas;
                }
                blobClient = new BlobClient(new Uri(fullUri), options);
            }
            else if (cloubBlob.ServiceClient.Credentials.IsSharedKey) //Shared Key
            {
                blobClient = new BlobClient(cloubBlob.SnapshotQualifiedUri,
                                            new StorageSharedKeyCredential(context.StorageAccountName, cloubBlob.ServiceClient.Credentials.ExportBase64EncodedKey()), options);
            }
            else //Anonymous
            {
                blobClient = new BlobClient(cloubBlob.SnapshotQualifiedUri, options);
            }

            return(blobClient);
        }
        /// <summary>
        /// Initialize the storage account endpoint if it's not specified.
        /// We can get the value from multiple places, we only take the one with higher precedence. And the precedence is:
        /// 1. The one get from StorageContext parameter
        /// 2. The one get from the storage account
        /// 3. The one get from PrivateConfig element in config file
        /// 4. The one get from current Azure Environment
        /// </summary>
        public static string InitializeStorageAccountEndpoint(string storageAccountName, string storageAccountKey, IStorageManagementClient storageClient,
                                                              AzureStorageContext storageContext = null, string configurationPath = null, AzureContext defaultContext = null)
        {
            string         storageAccountEndpoint = null;
            StorageAccount storageAccount         = null;

            if (storageContext != null)
            {
                // Get value from StorageContext
                storageAccountEndpoint = GetEndpointFromStorageContext(storageContext);
            }
            else if (TryGetStorageAccount(storageClient, storageAccountName, out storageAccount) &&
                     storageAccount.Properties.Endpoints.Count >= 4)
            {
                // Get value from StorageAccount
                var endpoints = storageAccount.Properties.Endpoints;
                var context   = CreateStorageContext(endpoints[0], endpoints[1], endpoints[2], endpoints[3], storageAccountName, storageAccountKey);
                storageAccountEndpoint = GetEndpointFromStorageContext(context);
            }
            else if (!string.IsNullOrEmpty(
                         storageAccountEndpoint = GetStorageAccountInfoFromPrivateConfig(configurationPath, PrivConfEndpointAttr)))
            {
                // We can get value from PrivateConfig
            }
            else if (defaultContext != null && defaultContext.Environment != null)
            {
                // Get value from default azure environment. Default to use https
                Uri blobEndpoint  = defaultContext.Environment.GetStorageBlobEndpoint(storageAccountName);
                Uri queueEndpoint = defaultContext.Environment.GetStorageQueueEndpoint(storageAccountName);
                Uri tableEndpoint = defaultContext.Environment.GetStorageTableEndpoint(storageAccountName);
                Uri fileEndpoint  = defaultContext.Environment.GetStorageFileEndpoint(storageAccountName);
                var context       = CreateStorageContext(blobEndpoint, queueEndpoint, tableEndpoint, fileEndpoint, storageAccountName, storageAccountKey);
                storageAccountEndpoint = GetEndpointFromStorageContext(context);
            }

            return(storageAccountEndpoint);
        }
Esempio n. 20
0
        /// <summary>
        /// Azure storage blob constructor
        /// </summary>
        /// <param name="blob">ICloud blob object</param>
        public AzureStorageBlob(TaggedBlobItem blob, AzureStorageContext storageContext, string continuationToken = null, BlobClientOptions options = null, bool getProperties = false)
        {
            // Get Track2 blob client
            BlobUriBuilder uriBuilder = new BlobUriBuilder(storageContext.StorageAccount.BlobEndpoint)
            {
                BlobContainerName = blob.BlobContainerName,
                BlobName          = blob.BlobName
            };
            Uri blobUri = uriBuilder.ToUri();

            if (storageContext.StorageAccount.Credentials.IsSAS)
            {
                blobUri = new Uri(blobUri.ToString() + storageContext.StorageAccount.Credentials.SASToken);
            }
            this.privateBlobBaseClient = Util.GetTrack2BlobClient(blobUri, storageContext, options);

            // Set continuationToken
            if (continuationToken != null)
            {
                BlobContinuationToken token = new BlobContinuationToken();
                token.NextMarker       = continuationToken;
                this.ContinuationToken = token;
                ICloudBlob             = storageContext.StorageAccount.CreateCloudBlobClient().GetContainerReference(blob.BlobContainerName).GetBlobReference(blob.BlobName);
            }

            // Set other properties
            if (!getProperties)
            {
                getLazyProperties = false;
                Name         = blob.BlobName;
                this.Context = storageContext;
            }
            else
            {
                SetProperties(this.privateBlobBaseClient, storageContext, null, options);
            }
        }
Esempio n. 21
0
        // Convert Blob object to Track 2 blob Client
        public static BlobClient GetTrack2BlobClient(BlobBaseClient blobBaseClient, AzureStorageContext context, BlobClientOptions options = null)
        {
            if (blobBaseClient is BlobClient)
            {
                return((BlobClient)blobBaseClient);
            }
            BlobClient blobClient;

            if (context.StorageAccount.Credentials.IsToken) //Oauth
            {
                blobClient = new BlobClient(blobBaseClient.Uri, context.Track2OauthToken, options);
            }
            else if (context.StorageAccount.Credentials.IsSharedKey) //Shared Key
            {
                blobClient = new BlobClient(blobBaseClient.Uri,
                                            new StorageSharedKeyCredential(context.StorageAccountName, context.StorageAccount.Credentials.ExportBase64EncodedKey()), options);
            }
            else //Anonymous or SAS
            {
                blobClient = new BlobClient(blobBaseClient.Uri, options);
            }

            return(blobClient);
        }
Esempio n. 22
0
        /// <summary>
        /// Initialize the storage account endpoint if it's not specified.
        /// We can get the value from multiple places, we only take the one with higher precedence. And the precedence is:
        /// 1. The one get from StorageContext parameter
        /// 2. The one get from the storage account
        /// 3. The one get from PrivateConfig element in config file
        /// 4. The one get from current Azure Environment
        /// </summary>
        public static string InitializeStorageAccountEndpoint(string storageAccountName, string storageAccountKey, IStorageManagementClient storageClient,
                                                              AzureStorageContext storageContext = null, string configurationPath = null, AzureContext defaultContext = null)
        {
            string         storageAccountEndpoint = null;
            StorageAccount storageAccount         = null;

            if (storageContext != null)
            {
                // Get value from StorageContext
                storageAccountEndpoint = GetEndpointFromStorageContext(storageContext);
            }
            else if (TryGetStorageAccount(storageClient, storageAccountName, out storageAccount))
            {
                // Get value from StorageAccount
                var endpoints = storageAccount.PrimaryEndpoints;
                var context   = CreateStorageContext(new Uri(endpoints.Blob), new Uri(endpoints.Queue), new Uri(endpoints.Table), new Uri(endpoints.File), storageAccountName, storageAccountKey);
                storageAccountEndpoint = GetEndpointFromStorageContext(context);
            }
            else if (!string.IsNullOrEmpty(
                         storageAccountEndpoint = GetConfigValueFromPrivateConfig(configurationPath, StorageAccountElemStr, PrivConfEndpointAttr)))
            {
                // We can get the value from PrivateConfig
            }
            else if (defaultContext != null && defaultContext.Environment != null)
            {
                // Get value from default azure environment. Default to use https
                Uri blobEndpoint  = defaultContext.Environment.GetStorageBlobEndpoint(storageAccountName);
                Uri queueEndpoint = defaultContext.Environment.GetStorageQueueEndpoint(storageAccountName);
                Uri tableEndpoint = defaultContext.Environment.GetStorageTableEndpoint(storageAccountName);
                Uri fileEndpoint  = defaultContext.Environment.GetStorageFileEndpoint(storageAccountName);
                var context       = CreateStorageContext(blobEndpoint, queueEndpoint, tableEndpoint, fileEndpoint, storageAccountName, storageAccountKey);
                storageAccountEndpoint = GetEndpointFromStorageContext(context);
            }

            return(storageAccountEndpoint);
        }
Esempio n. 23
0
        /// <summary>
        /// Attempts to get the user's credentials from the given Storage Context or the current subscription, if the former is null.
        /// Throws a terminating error if the credentials cannot be determined.
        /// </summary>
        internal static StorageCredentials GetStorageCredentials(this AzureSMCmdlet cmdlet, AzureStorageContext storageContext)
        {
            StorageCredentials credentials = null;

            if (storageContext != null)
            {
                credentials = storageContext.StorageAccount.Credentials;
            }
            else
            {
                var storageAccount = cmdlet.Profile.Context.GetCurrentStorageAccount();
                if (storageAccount != null)
                {
                    credentials = storageAccount.Credentials;
                }
            }

            if (credentials == null)
            {
                cmdlet.ThrowTerminatingError(
                    new ErrorRecord(
                        new UnauthorizedAccessException(Resources.AzureVMDscDefaultStorageCredentialsNotFound),
                        "CredentialsNotFound",
                        ErrorCategory.PermissionDenied,
                        null));
            }

            if (string.IsNullOrEmpty(credentials.AccountName))
            {
                ThrowInvalidArgumentError(cmdlet, Resources.AzureVMDscStorageContextMustIncludeAccountName);
            }

            return(credentials);
        }
Esempio n. 24
0
 public HardwareController(AzureStorageContext storage)
 {
     _storage = storage;
 }
 /// <summary>
 /// Init blob management
 /// </summary>
 /// <param name="client">a cloud blob object</param>
 public StorageBlobManagement(AzureStorageContext context)
 {
     internalStorageContext = context;
 }
Esempio n. 26
0
 public StorageFileManagement(AzureStorageContext context)
 {
     this.StorageContext = context;
 }
Esempio n. 27
0
        /// <summary>
        /// Start copy operation by source uri
        /// </summary>
        /// <param name="srcCloudBlob">Source uri</param>
        /// <param name="destContainer">Destinaion container name</param>
        /// <param name="destBlobName">Destination blob name</param>
        /// <returns>Destination CloudBlob object</returns>
        private void StartCopyBlob(IStorageBlobManagement destChannel, string srcUri, string destContainer, string destBlobName, AzureStorageContext context)
        {
            if (context != null)
            {
                Uri sourceUri  = new Uri(srcUri);
                Uri contextUri = new Uri(context.BlobEndPoint);

                if (sourceUri.Host.ToLower() == contextUri.Host.ToLower())
                {
                    CloudBlobClient blobClient    = context.StorageAccount.CreateCloudBlobClient();
                    CloudBlob       blobReference = null;

                    try
                    {
                        blobReference = Util.GetBlobReferenceFromServer(blobClient, sourceUri);
                    }
                    catch (InvalidOperationException)
                    {
                        blobReference = null;
                    }

                    if (null == blobReference)
                    {
                        throw new ResourceNotFoundException(String.Format(Resources.BlobUriNotFound, sourceUri.ToString()));
                    }

                    StartCopyBlob(destChannel, blobReference, destContainer, destBlobName);
                }
                else
                {
                    WriteWarning(String.Format(Resources.StartCopySourceContextMismatch, srcUri, context.BlobEndPoint));
                }
            }
            else
            {
                CloudBlobContainer container     = destChannel.GetContainerReference(destContainer);
                Func <long, Task>  taskGenerator = (taskId) => StartCopyAsync(taskId, destChannel, new Uri(srcUri), container, destBlobName);
                RunTask(taskGenerator);
            }
        }
Esempio n. 28
0
        public GatewayGetOperationStatusResponse StartDiagnostics(string vnetName, int captureDurationInSeconds, string containerName, AzureStorageContext storageContext)
        {
            StorageCredentials credentials         = storageContext.StorageAccount.Credentials;
            string             customerStorageKey  = credentials.ExportBase64EncodedKey();
            string             customerStorageName = credentials.AccountName;

            return(StartDiagnostics(vnetName, captureDurationInSeconds, containerName, customerStorageKey, customerStorageName));
        }
 /// <summary>
 /// Queue management constructor
 /// </summary>
 /// <param name="client">Cloud queue client</param>
 public StorageQueueManagement(AzureStorageContext context)
 {
     internalStorageContext = context;
     queueClient            = internalStorageContext.StorageAccount.CreateCloudQueueClient();
 }
        /// <summary>
        /// Start copy operation by source uri
        /// </summary>
        /// <param name="srcICloudBlob">Source uri</param>
        /// <param name="destContainer">Destinaion container name</param>
        /// <param name="destBlobName">Destination blob name</param>
        /// <returns>Destination ICloudBlob object</returns>
        private void StartCopyBlob(IStorageBlobManagement destChannel, string srcUri, string destContainer, string destBlobName, AzureStorageContext context)
        {
            if (context != null)
            {
                Uri sourceUri = new Uri(srcUri);
                Uri contextUri = new Uri(context.BlobEndPoint);

                if (sourceUri.Host.ToLower() == contextUri.Host.ToLower())
                {
                    CloudBlobClient blobClient = context.StorageAccount.CreateCloudBlobClient();
                    ICloudBlob blobReference = blobClient.GetBlobReferenceFromServer(sourceUri);
                    StartCopyBlob(destChannel, blobReference, destContainer, destBlobName);
                }
                else
                {
                    WriteWarning(String.Format(Resources.StartCopySourceContextMismatch, srcUri, context.BlobEndPoint));
                }
            }

            CloudBlobContainer container = destChannel.GetContainerReference(destContainer);
            Func<long, Task> taskGenerator = (taskId) => StartCopyInTransferManager(taskId, destChannel, new Uri(srcUri), container, destBlobName);
            RunTask(taskGenerator);
        }
Esempio n. 31
0
 public ManufacturerEntity()
 {
     _storage = (AzureStorageContext)System.Web.Mvc.DependencyResolver.Current.GetService(typeof(AzureStorageContext));
 }
        private static string GetEndpointFromStorageContext(AzureStorageContext context)
        {
            var scheme = context.BlobEndPoint.StartsWith("https://", StringComparison.OrdinalIgnoreCase) ? "https://" : "http://";

            return(scheme + context.EndPointSuffix);
        }
 public PSStorageService(AzureStorageContext context)
 {
     _context = context;
 }
Esempio n. 34
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);
        }
        /// <summary>
        /// Start copy operation by source uri
        /// </summary>
        /// <param name="srcCloudBlob">Source uri</param>
        /// <param name="destContainer">Destinaion container name</param>
        /// <param name="destBlobName">Destination blob name</param>
        /// <returns>Destination CloudBlob object</returns>
        private void StartCopyBlob(IStorageBlobManagement destChannel, string srcUri, string destContainer, string destBlobName, AzureStorageContext context)
        {
            if (context != null)
            {
                Uri sourceUri = new Uri(srcUri);
                Uri contextUri = new Uri(context.BlobEndPoint);

                if (sourceUri.Host.ToLower() == contextUri.Host.ToLower())
                {
                    CloudBlobClient blobClient = context.StorageAccount.CreateCloudBlobClient();
                    CloudBlob blobReference = null;

                    try
                    {
                        blobReference = Util.GetBlobReferenceFromServer(blobClient, sourceUri);
                    }
                    catch (InvalidOperationException)
                    {
                        blobReference = null;
                    }

                    if (null == blobReference)
                    {
                        throw new ResourceNotFoundException(String.Format(Resources.BlobUriNotFound, sourceUri.ToString()));
                    }

                    StartCopyBlob(destChannel, blobReference, destContainer, destBlobName);
                }
                else
                {
                    WriteWarning(String.Format(Resources.StartCopySourceContextMismatch, srcUri, context.BlobEndPoint));
                }
            }
            else
            {
                CloudBlobContainer container = destChannel.GetContainerReference(destContainer);
                Func<long, Task> taskGenerator = (taskId) => StartCopyAsync(taskId, destChannel, new Uri(srcUri), container, destBlobName);
                RunTask(taskGenerator);
            }
        }
Esempio n. 36
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);
        }
        /// <summary>
        /// Start copy operation by source uri
        /// </summary>
        /// <param name="srcICloudBlob">Source uri</param>
        /// <param name="destContainer">Destinaion container name</param>
        /// <param name="destBlobName">Destination blob name</param>
        /// <returns>Destination ICloudBlob object</returns>
        private ICloudBlob StartCopyBlob(string srcUri, string destContainer, string destBlobName, AzureStorageContext context)
        {
            if (context != null)
            {
                Uri sourceUri = new Uri(srcUri);
                Uri contextUri = new Uri(context.BlobEndPoint);

                if (sourceUri.Host.ToLower() == contextUri.Host.ToLower())
                {
                    CloudBlobClient blobClient = context.StorageAccount.CreateCloudBlobClient();
                    ICloudBlob blobReference = blobClient.GetBlobReferenceFromServer(sourceUri);
                    return StartCopyBlob(blobReference, destContainer, destBlobName);
                }
                else
                {
                    WriteWarning(String.Format(Resources.StartCopySourceContextMismatch, srcUri, context.BlobEndPoint));
                }
            }

            CloudBlobContainer container = destChannel.GetContainerReference(destContainer);
            return StartCopyInTransferManager(new Uri(srcUri), container, destBlobName);
        }
        public override void ExecuteCmdlet()
        {
            CloudStorageAccount account = null;
            bool useHttps = (StorageNouns.HTTPS.ToLower() == protocolType.ToLower());

            switch (ParameterSetName)
            {
                case AccountNameKeyParameterSet:
                    account = GetStorageAccountByNameAndKey(StorageAccountName, StorageAccountKey, useHttps, storageEndpoint);
                    break;
                case AccountNameKeyEnvironmentParameterSet:
                    account = GetStorageAccountByNameAndKeyFromAzureEnvironment(StorageAccountName, StorageAccountKey,
                        useHttps, environmentName);
                    break;
                case ConnectionStringParameterSet:
                    account = GetStorageAccountByConnectionString(ConnectionString);
                    break;
                case LocalParameterSet:
                    account = GetLocalDevelopmentStorageAccount();
                    break;
                case AnonymousParameterSet:
                    account = GetAnonymousStorageAccount(StorageAccountName, useHttps, storageEndpoint);
                    break;
                case AnonymousEnvironmentParameterSet:
                    account = GetAnonymousStorageAccountFromAzureEnvironment(StorageAccountName, useHttps, environmentName);
                    break;
                default:
                    throw new ArgumentException(Resources.DefaultStorageCredentialsNotFound);
            }

            AzureStorageContext context = new AzureStorageContext(account);
            WriteObject(context);
        }