Example #1
0
        public int HowManyContainers()
        {
            var blobServiceClient  = new BlobServiceClient(Environment.GetEnvironmentVariable("CONNECT_STR"));
            var randyTestContainer = "justtestingthisout" + Guid.NewGuid().ToString();

            return(blobServiceClient.GetBlobContainers().Count());
        }
Example #2
0
        public List <string> ListContainers()
        {
            var blobServiceClient  = new BlobServiceClient(Environment.GetEnvironmentVariable("CONNECT_STR"));
            var randyTestContainer = "justtestingthisout" + Guid.NewGuid().ToString();

            return(blobServiceClient.GetBlobContainers().Select(s => s.Name).ToList());
        }
Example #3
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ExecutionContext context,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) // <- This gives you access to your application settings in your local development environment
                         .AddEnvironmentVariables()                                                // <- This is what actually gets you the application settings in Azure
                         .Build();

            TokenCredential credential = new ClientSecretCredential(config["TenantId"],
                                                                    config["ApplicationId"], config["ApplicationSecret"],
                                                                    new TokenCredentialOptions()
            {
                AuthorityHost = new Uri(config["ActiveDirectoryAuthEndpoint"])
            });

            var client = new BlobServiceClient(new Uri(config["ActiveDirectoryBlobUri"]), credential);

            var containers = client.GetBlobContainers();

            var responseMessage = containers.Select(r => r.Name).ToArray();

            return(new OkObjectResult(responseMessage));
        }
Example #4
0
        private void InitContainers()
        {
            var service         = new BlobServiceClient(AppConstants.AzureStorageConnectionString);
            var containers      = service.GetBlobContainers();
            var draftsExists    = false;
            var publishedExists = false;

            foreach (var c in containers)
            {
                if (c.Name == DRAFTS)
                {
                    draftsExists = true;
                }
                if (c.Name == PUBLISHED)
                {
                    publishedExists = true;
                }

                if (draftsExists && publishedExists)
                {
                    break;
                }
            }

            if (!draftsExists)
            {
                service.CreateBlobContainer(DRAFTS);
            }

            if (!publishedExists)
            {
                service.CreateBlobContainer(PUBLISHED);
            }
        }
Example #5
0
        public static async Task <string> WriteBlob([ActivityTrigger] IDurableActivityContext writeBlobContext, ILogger log)
        {
            BlobContainerClient containerClient = null;
            var newMsg = writeBlobContext.GetInput <NewMessage>();

            //log.LogInformation($"Message: {JsonConvert.SerializeObject(newMsg)}");
            //log.LogInformation($"MessageId:{msgId}");

            // Create a BlobServiceClient object which will be used to create a container client
            BlobServiceClient blobServiceClient = new BlobServiceClient(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));

            //Create a container if it does not exist
            string containerName = "durablefunctest";

            if (blobServiceClient.GetBlobContainers().Where(cont => cont.Name.ToLowerInvariant() == containerName).ToList().Count() > 0)
            {
                containerClient = blobServiceClient.GetBlobContainerClient(containerName);
            }
            else
            {
                // Create the container and return a container client object
                containerClient = blobServiceClient.CreateBlobContainer(containerName);
            }

            // Get a reference to a blob
            BlobClient blobClient = containerClient.GetBlobClient(msgId);

            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(newMsg))))
            {
                await blobClient.UploadAsync(stream);
            }

            return(blobClient.Uri.AbsoluteUri);
        }
 public void Dispose()
 {
     foreach (var testContainer in _blobServiceClient.GetBlobContainers(prefix: TestArtifactPrefix))
     {
         _blobServiceClient.GetBlobContainerClient(testContainer.Name).Delete();
     }
 }
        /// <summary>
        /// List containers by container name prefix
        /// </summary>
        /// <param name="prefix">Container name prefix</param>
        /// <returns>An enumerable collection of cloudblobcontainer</returns>
        internal IEnumerable <Tuple <AzureStorageContainer, BlobContinuationToken> > ListContainersByPrefix(string prefix, Func <string, bool> containerFilter = null)
        {
            BlobServiceClient   blobServiceClient = Util.GetTrack2BlobServiceClient(this.Channel.StorageContext, ClientOptions);
            BlobContainerTraits traits            = BlobContainerTraits.Metadata;
            BlobContainerStates states            = BlobContainerStates.None;

            if (this.IncludeDeleted.IsPresent)
            {
                states = BlobContainerStates.Deleted;
            }
            if (!string.IsNullOrEmpty(prefix) && !NameUtil.IsValidContainerPrefix(prefix))
            {
                throw new ArgumentException(String.Format(Resources.InvalidContainerName, prefix));
            }

            int    listCount         = InternalMaxCount;
            int    MaxListCount      = 5000;
            int    requestCount      = MaxListCount;
            int    realListCount     = 0;
            string continuationToken = this.ContinuationToken is null ? null : this.ContinuationToken.NextMarker;

            do
            {
                requestCount  = Math.Min(listCount, MaxListCount);
                realListCount = 0;

                IEnumerator <Page <BlobContainerItem> > enumerator = blobServiceClient.GetBlobContainers(traits, states, prefix, this.CmdletCancellationToken)
                                                                     .AsPages(continuationToken, requestCount)
                                                                     .GetEnumerator();

                Page <BlobContainerItem> page;
                enumerator.MoveNext();
                page = enumerator.Current;

                foreach (BlobContainerItem item in page.Values)
                {
                    if (containerFilter == null || containerFilter(item.Name))
                    {
                        yield return(new Tuple <AzureStorageContainer, BlobContinuationToken>(
                                         new AzureStorageContainer(item, Channel.StorageContext, blobServiceClient),
                                         string.IsNullOrEmpty(page.ContinuationToken) ? null : new BlobContinuationToken()
                        {
                            NextMarker = page.ContinuationToken
                        }));

                        realListCount++;
                    }
                    realListCount++;
                }
                continuationToken = page.ContinuationToken;

                if (InternalMaxCount != int.MaxValue)
                {
                    listCount -= realListCount;
                }
            }while (listCount > 0 && !string.IsNullOrEmpty(continuationToken));
        }
 public void RefreshContainerDetails()
 {
     BlobContainerClient = new Dictionary <string, BlobContainerClient>();
     BlobContainerItems  = BlobServiceClient.GetBlobContainers();
     foreach (var container in BlobContainerItems)
     {
         BlobContainerClient.Add(container.Name, BlobServiceClient.GetBlobContainerClient(container.Name));
     }
 }
Example #9
0
 public TestScriptController()
 {
     _aviMock = new Mock <IAviBL>();
     _blobSC  = new BlobServiceClient("DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;");
     foreach (var c in _blobSC.GetBlobContainers())
     {
         _blobSC.DeleteBlobContainer(c.Name);
     }
 }
        public IEnumerable <StorageContainerModel> GetContainers()
        {
            Pageable <BlobContainerItem> response = _blobServiceClient.GetBlobContainers();

            return(response.Select(c => new StorageContainerModel()
            {
                Name = c.Name
            }));
        }
    private async Task DeleteContainersAsync()
    {
        Console.WriteLine($"Deleting containers in storage account '{_serviceClient.AccountName}'");
        foreach (var container in _serviceClient.GetBlobContainers())
        {
            await _serviceClient.DeleteBlobContainerAsync(container.Name);

            Console.WriteLine($"\t{container.Name}");
        }
    }
        private void PopulateContainerList()
        {
            var containerList = _blobServiceClient.GetBlobContainers();

            foreach (var container in containerList)
            {
                Containers.Add(new KeyValuePair <string, BlobContainerClient>(container.Name,
                                                                              new BlobContainerClient(Constants.StorageAccountConnectionString, container.Name)));
            }
        }
Example #13
0
        public static void CreateBlobContainerIfNotExists()
        {
            BlobServiceClient client = new BlobServiceClient(ConnectionString);

            if (client.GetBlobContainers(prefix: ContainerName).Any(container => container.Name.Equals(ContainerName)))
            {
                return;
            }
            client.CreateBlobContainer(ContainerName, PublicAccessType.BlobContainer);
        }
Example #14
0
        private static async Task ReadInfos(string connString)
        {
            BlobServiceClient storageAccount;

            try
            {
                storageAccount = new BlobServiceClient(connString);
                AccountInfo accountInfo = await storageAccount.GetAccountInfoAsync();

                Console.WriteLine("AccountKind: " + accountInfo.AccountKind);
                Console.WriteLine("AccountSku: " + accountInfo.SkuName);

                if (accountInfo.SkuName == SkuName.StandardRagrs)
                {
                    BlobServiceStatistics stats = await storageAccount.GetStatisticsAsync();

                    Console.WriteLine("Statistics (GeoReplication): " + stats.GeoReplication);
                }

                var containers = storageAccount.GetBlobContainers();

                foreach (var container in containers)
                {
                    Console.WriteLine("checking Container: " + container.Name);

                    if (container.IsDeleted ?? true)
                    {
                        BlobContainerClient     containerClient     = new BlobContainerClient(connString, container.Name);
                        BlobContainerProperties containerProperties = await containerClient.GetPropertiesAsync();

                        //TODO: Generate Demo SASPolicy
                        GenerateSASPolicy(containerClient);
                        Console.WriteLine("Container has LegalHold? " + containerProperties.HasLegalHold);
                        Console.WriteLine("Container is Immutable? " + containerProperties.HasImmutabilityPolicy);

                        await foreach (BlobItem blob in containerClient.GetBlobsAsync())
                        {
                            Console.WriteLine("Blob: " + blob.Name);
                            Console.WriteLine("     Type Of:" + blob.Properties.BlobType);
                            Console.WriteLine(blob.Properties.ContentLength > 1024 ?
                                              "     Size (KB):" + (blob.Properties.ContentLength / 1024) :
                                              "     Size (byte):" + blob.Properties.ContentLength);
                        }
                    }
                    else
                    {
                        Console.WriteLine("    >>>> Is Deleted!" + container.Properties.RemainingRetentionDays);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("OH SNAP!: " + e.Message);
            }
        }
Example #15
0
        /// <summary>
        /// Constructor responsible for obtaining an <see cref="AzureStorageOptions"/> from DI.
        /// </summary>
        /// <param name="options">Azure storage options.</param>
        public AzureStorageService(AzureStorageOptions options)
        {
            this.serviceClient = new BlobServiceClient(options.ConnectionString);
            this.StorageName   = options.StorageName;

            // add existing containers into indexed property
            foreach (var containerItem in serviceClient.GetBlobContainers())
            {
                this[containerItem.Name] = new AzureContainer(serviceClient.GetBlobContainerClient(containerItem.Name));
            }
        }
Example #16
0
        //------------------------------------------
        // Download multiple blobs simultaneously
        //------------------------------------------
        // <Snippet_DownloadFilesAsync>
        private static async Task DownloadFilesAsync()
        {
            BlobServiceClient blobServiceClient = GetBlobServiceClient();

            // Path to the directory to upload
            string downloadPath = Directory.GetCurrentDirectory() + "\\download\\";

            Directory.CreateDirectory(downloadPath);
            Console.WriteLine($"Created directory {downloadPath}");

            // Specify the StorageTransferOptions
            var options = new StorageTransferOptions
            {
                // Set the maximum number of workers that
                // may be used in a parallel transfer.
                MaximumConcurrency = 8,

                // Set the maximum length of a transfer to 50MB.
                MaximumTransferSize = 50 * 1024 * 1024
            };

            List <BlobContainerClient> containers = new List <BlobContainerClient>();

            foreach (BlobContainerItem container in blobServiceClient.GetBlobContainers())
            {
                containers.Add(blobServiceClient.GetBlobContainerClient(container.Name));
            }

            // Start a timer to measure how long it takes to download all the files.
            Stopwatch timer = Stopwatch.StartNew();

            // Download the blobs
            try
            {
                int count = 0;

                // Create a queue of tasks that will each upload one file.
                var tasks = new Queue <Task <Response> >();

                foreach (BlobContainerClient container in containers)
                {
                    // Iterate through the files
                    foreach (BlobItem blobItem in container.GetBlobs())
                    {
                        string fileName = downloadPath + blobItem.Name;
                        Console.WriteLine($"Downloading {blobItem.Name} to {downloadPath}");

                        BlobClient blob = container.GetBlobClient(blobItem.Name);

                        // Add the download task to the queue
                        tasks.Enqueue(blob.DownloadToAsync(fileName, default, options));
        /// <summary>
        /// Create the sprite settings.
        /// </summary>
        /// <param name="spriteFilename">The name of the sprite file that will be created or overwritten.</param>
        /// <param name="spriteUrl">The URL of the sprite file.</param>
        /// <param name="cssFilename">The name of the CSS file that will be created or overwritten.</param>
        /// <param name="connectionString"></param>
        /// <param name="cssContainerName"></param>
        /// <param name="spriteContainerName"></param>
        public AzureStorageSpriteSettings(string spriteFilename, string spriteUrl, string cssFilename, string connectionString, string cssContainerName, string spriteContainerName)
        {
            SpriteFilename      = spriteFilename;
            SpriteUrl           = spriteUrl;
            CssFilename         = cssFilename;
            ConnectionString    = connectionString;
            CssContainerName    = cssContainerName;
            SpriteContainerName = spriteContainerName;

            var blobServiceClient = new BlobServiceClient(ConnectionString);

            var cssBlobContainer = blobServiceClient.GetBlobContainers().FirstOrDefault(x => x.Name == CssContainerName);

            _cssBlobContainerClient = cssBlobContainer == null
                ? blobServiceClient.CreateBlobContainer(CssContainerName)
                : blobServiceClient.GetBlobContainerClient(CssContainerName);

            var spriteBlobContainer = blobServiceClient.GetBlobContainers().FirstOrDefault(x => x.Name == SpriteContainerName);

            _spriteBlobContainerClient = spriteBlobContainer == null
                ? blobServiceClient.CreateBlobContainer(SpriteContainerName)
                : blobServiceClient.GetBlobContainerClient(SpriteContainerName);
        }
Example #18
0
        public MainPage()
        {
            this.InitializeComponent();
            storageAccount     = CloudStorageAccount.Parse(connectionString);
            tableClient        = storageAccount.CreateCloudTableClient();
            _blobServiceClient = new BlobServiceClient(connectionString);

            var container = _blobServiceClient.GetBlobContainers().FirstOrDefault(c => c.Name == CONTAINER_NAME);

            if (container is null)
            {
                _blobServiceClient.CreateBlobContainer(CONTAINER_NAME);
            }
        }
Example #19
0
        public IEnumerable <string> ListContainersAsync(string prefix, CancellationToken cancel)
        {
            var containerNames = new List <string>();
            var containers     = blobService.GetBlobContainers(prefix: prefix, cancellationToken: cancel);

            using var containerEnumerator = containers.GetEnumerator();
            while (containerEnumerator.MoveNext())
            {
                if (containerEnumerator.Current != null)
                {
                    containerNames.Add(containerEnumerator.Current.Name);
                }
            }

            return(containerNames);
        }
Example #20
0
        private static async Task ListContainers()
        {
            await ConnectToStorageAccount();

            foreach (BlobContainerItem ci in blobServiceClient.GetBlobContainers())
            {
                if (config.IsVerbose())
                {
                    Console.WriteLine($"container: {ci.Name} properties: {JsonConvert.SerializeObject(ci.Properties)}");
                }
                else
                {
                    Console.WriteLine($"container: {ci.Name}");
                }
            }
        }
Example #21
0
        public async Task OnGet()
        {
            string connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");

            // Create a BlobServiceClient object which will be used to create a container client
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

            var containers = blobServiceClient.GetBlobContainers();

            foreach (var item in containers)
            {
                Containers += item.Name + "\n";
            }

            Containers.TrimEnd();
        }
        static async Task <BlobContainerClient> CreateContainerIfNotExists()
        {
            var containerName     = "blobstorageolivier";
            var blobServiceClient = new BlobServiceClient(connectionString);

            var containers = blobServiceClient.GetBlobContainers().ToList();

            if (containers.FirstOrDefault(c => c.Name.Equals(containerName)) == null)
            {
                var response = await blobServiceClient.CreateBlobContainerAsync(containerName);

                return(response.Value);
            }

            return(blobServiceClient.GetBlobContainerClient(containerName));
        }
Example #23
0
        static async Task Main(string[] args)
        {
            try
            {
                Stopwatch.Start();

                SetConnectionString();

                var blobServiceClient = new BlobServiceClient(_connectionString);

                SetBasePath();

                _availableBlobContainers = blobServiceClient.GetBlobContainers().AsPages().SelectMany(p => p.Values).ToArray();
                SetBlobContainerFolder();

                var blobContainer = blobServiceClient.GetBlobContainerClient(_blobsContainer);
                var blobs         = blobContainer.GetBlobs(BlobTraits.All);
                _blobCount = blobs.Count();
                foreach (var blobItem in blobs)
                {
                    _currentLocalPath = Path.Combine(_currentLocalPath, blobItem.Name);
                    var blobClient = blobContainer.GetBlobClient(blobItem.Name);

                    if (DirOrFileAlreadyExists(blobItem))
                    {
                        continue;
                    }

                    await Download(blobClient);

                    ResetPath();
                    Console.WriteLine($"Downloaded {blobItem.Name}");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.WriteLine((_downloadCount + _skippedCount) != _blobCount
                ? "Not all blobs seem to have been processed, try run tool again"
                : "All blobs where processed");

            Stopwatch.Stop();
            Console.WriteLine($"Tool took {Stopwatch.Elapsed:g} to finish");
        }
Example #24
0
        private async Task <BlobContainerClient> StartAzureConnection()
        {
            string            connectionString  = "DefaultEndpointsProtocol=https;AccountName=azurepoc1993;AccountKey=GeDcGm2eVpVOwIoJxhd28Q5tbE3LYOqK+LOsSBoykLk6dnvq9jkwHeGCCgKFLxgeLryzdTaM5WDJEV6pIOakuQ==;EndpointSuffix=core.windows.net";
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

            //Create a unique name for the container
            string containerName = "poc4";
            // Create the container and return a container client object
            BlobContainerClient containerClient;
            BlobContainerItem   container = blobServiceClient.GetBlobContainers().Where(b => b.Name == containerName).FirstOrDefault();

            if (container == null)
            {
                containerClient = await blobServiceClient.CreateBlobContainerAsync(containerName, PublicAccessType.Blob);
            }
            else
            {
                containerClient = blobServiceClient.GetBlobContainerClient(containerName);
            }
            return(containerClient);
        }
        /// <summary>
        /// Validates the connection to the storage account. This method will try to perform list containers
        /// operation (fetching just one container) if <paramref name="containerName"/> parameter is not defined
        /// otherwise it will try to perform list blobs operation (fetching just one blob). The objective of this
        /// method is to capture 403 error.
        /// </summary>
        /// <param name="containerName"></param>
        /// <returns>True if connecting is validated else false.</returns>
        public static async Task <bool> ValidateConnectionAsync(string containerName = null)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(containerName) || containerName == "*")
                {
                    s_blobServiceClient.GetBlobContainers();
                }
                else
                {
                    BlobContainerClient blobContainer = s_blobServiceClient.GetBlobContainerClient(containerName);
                    await blobContainer.GetPropertiesAsync();
                }

                return(true);
            }
            catch (RequestFailedException ex) when(ex.Status == 403)
            {
                return(false);
            }
        }
        private async Task Initialize()
        {
            // If the container client has already been created, then there is nothing to do here
            if (containerClient != null)
            {
                return;
            }

            // Create a BlobServiceClient object which will be used to create a container client
            BlobServiceClient blobServiceClient = new BlobServiceClient(this.connectionString);
            BlobContainerItem item = blobServiceClient.GetBlobContainers(prefix: this.containerName).FirstOrDefault();

            if (item == null)
            {
                this.containerClient = await blobServiceClient.CreateBlobContainerAsync(this.containerName);
            }
            else
            {
                this.containerClient = blobServiceClient.GetBlobContainerClient(this.containerName);
            }
        }
Example #27
0
        public async Task DownloadAllBlobsInContainer()
        {
            var containerClients = _client.GetBlobContainers();

            foreach (BlobContainerItem container in containerClients)
            {
                var containerClient = _client.GetBlobContainerClient(container.Name);
                Console.WriteLine(container.Name);
                await foreach (var blobItem in containerClient.GetBlobsAsync())
                {
                    Console.WriteLine(blobItem.Name);

                    Console.WriteLine($"Downloading blob to: {_downloadPath}");

                    BlobClient       blobClient       = containerClient.GetBlobClient(blobItem.Name);
                    BlobDownloadInfo blobDownloadInfo = blobClient.Download();

                    // Access blob properties
                    BlobProperties props = blobClient.GetProperties();
                    Console.WriteLine("Properties");
                    Console.WriteLine($"Access tier: {props.AccessTier}");
                    Console.WriteLine($"Blob expires on: {props.ExpiresOn.UtcDateTime.ToLongDateString()}");

                    // Access blob metadata
                    Console.WriteLine("Metadata keyvalue pair");
                    foreach (var metadata in props.Metadata)
                    {
                        Console.WriteLine($"{metadata.Key}: {metadata.Value}");
                    }

                    using (FileStream fs = File.OpenWrite($"{_downloadPath}\\{blobItem.Name}"))
                    {
                        blobDownloadInfo.Content.CopyTo(fs);
                        fs.Close();
                    }
                }
            }
        }
Example #28
0
        public async Task UploadFile(MemoryStream fileStream, string fileName)
        {
            BlobContainerClient blobContainerClient;
            BlobServiceClient   blobServiceClient = new BlobServiceClient(containerConnectionString);
            var containers = blobServiceClient.GetBlobContainers();

            var container = containers.FirstOrDefault(o => o.Name.Equals(Constants.ContainerName));

            if (container == null)
            {
                blobContainerClient = blobServiceClient.CreateBlobContainer(Constants.ContainerName);
            }
            else
            {
                blobContainerClient = blobServiceClient.GetBlobContainerClient(Constants.ContainerName);
            }
            //blobServiceClient.GetChangeFeedClient();
            fileStream.Position = 0;
            await blobContainerClient.UploadBlobAsync(fileName, fileStream);

            await this.AddMetadata(blobContainerClient, fileName);

            fileStream.Dispose();
        }
 public static IActionResult Run(
     [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
     [AzureClient("StorageConnection")] BlobServiceClient client)
 {
     return(new OkObjectResult(client.GetBlobContainers().ToArray()));
 }
Example #30
0
 public Azure.Pageable <BlobContainerItem> GetContainers()
 {
     return(_blobServiceClient.GetBlobContainers());
 }