public void ChangeFeedResumeWithCursor()
        {
            // Get a connection string to our Azure Storage account.
            string connectionString = ConnectionString;

            // Get a new change feed client.
            BlobChangeFeedClient       changeFeedClient = new BlobChangeFeedClient(connectionString);
            List <BlobChangeFeedEvent> changeFeedEvents = new List <BlobChangeFeedEvent>();

            string continuationToken = null;

            foreach (Page <BlobChangeFeedEvent> page in changeFeedClient.GetChanges().AsPages(pageSizeHint: 10))
            {
                foreach (BlobChangeFeedEvent changeFeedEvent in page.Values)
                {
                    changeFeedEvents.Add(changeFeedEvent);
                }

                // Get the change feed continuation token.  The continuation token is not required to get each page of events,
                // it is intended to be saved and used to resume iterating at a later date.
                continuationToken = page.ContinuationToken;
                break;
            }

            // Resume iterating from the pervious position with the cursor.
            foreach (BlobChangeFeedEvent changeFeedEvent in changeFeedClient.GetChanges(
                         continuationToken: continuationToken))
            {
                changeFeedEvents.Add(changeFeedEvent);
            }
        }
Example #2
0
        public void ChangeFeedBetweenDates()
        {
            // Get a connection string to our Azure Storage account.
            string connectionString = ConnectionString;

            // Get a new blob service client.
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

            // Get a new change feed client.
            BlobChangeFeedClient       changeFeedClient = blobServiceClient.GetChangeFeedClient();
            List <BlobChangeFeedEvent> changeFeedEvents = new List <BlobChangeFeedEvent>();

            // Create the start and end time.  The change feed client will round start time down to
            // the nearest hour, and round endTime up to the next hour if you provide DateTimeOffsets
            // with minutes and seconds.
            DateTimeOffset startTime = new DateTimeOffset(2017, 3, 2, 15, 0, 0, TimeSpan.Zero);
            DateTimeOffset endTime   = new DateTimeOffset(2020, 10, 7, 2, 0, 0, TimeSpan.Zero);

            // You can also provide just a start or end time.
            foreach (BlobChangeFeedEvent changeFeedEvent in changeFeedClient.GetChanges(
                         start: startTime,
                         end: endTime))
            {
                changeFeedEvents.Add(changeFeedEvent);
            }
        }
        // Gets a cursor from container or creates a new cursor if it does not exist
        private static string GetCursor(BlobClient blobClient, BlobChangeFeedClient changeFeedClient, ILogger log)
        {
            string continuationToken = null;

            if (blobClient.Exists())
            {
                // If the continuationToken exists in blob, download and use it
                var stream = new MemoryStream();
                blobClient.DownloadTo(stream);
                continuationToken = Encoding.UTF8.GetString(stream.ToArray());
            }
            else
            {
                // If the continuationToken does not exist in the blob, get the continuationToken from the first item
                Page <BlobChangeFeedEvent> page            = changeFeedClient.GetChanges().AsPages(pageSizeHint: 1).First <Page <BlobChangeFeedEvent> >();
                BlobChangeFeedEvent        changeFeedEvent = page.Values.First <BlobChangeFeedEvent>();
                if (containerCheck.Invoke(changeFeedEvent) && eventTypeCheck.Invoke(changeFeedEvent) && blobCheck.Invoke(changeFeedEvent))
                {
                    log.LogInformation($"event: {changeFeedEvent.EventType} at {changeFeedEvent.Subject.Replace("/blobServices/default", "")} on {changeFeedEvent.EventTime}");
                }
                continuationToken = page.ContinuationToken;
            }

            return(continuationToken);
        }
        public void SharedKeyAuth()
        {
            // Get a Storage account name, shared key, and endpoint Uri.
            //
            // You can obtain both from the Azure Portal by clicking Access
            // Keys under Settings in the Portal Storage account blade.
            //
            // You can also get access to your account keys from the Azure CLI
            // with:
            //
            //     az storage account keys list --account-name <account_name> --resource-group <resource_group>
            //
            string accountName = StorageAccountName;
            string accountKey  = StorageAccountKey;
            Uri    serviceUri  = StorageAccountBlobUri;

            // Create a SharedKeyCredential that we can use to authenticate
            StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);

            // Create a client that can authenticate with a connection string
            BlobChangeFeedClient service = new BlobChangeFeedClient(serviceUri, credential);

            List <BlobChangeFeedEvent> changeFeedEvents = new List <BlobChangeFeedEvent>();

            // Make a service request to verify we've successfully authenticated
            foreach (BlobChangeFeedEvent changeFeedEvent in service.GetChanges())
            {
                changeFeedEvents.Add(changeFeedEvent);
            }
        }
        public void ActiveDirectoryAuth()
        {
            // Create a token credential that can use our Azure Active
            // Directory application to authenticate with Azure Storage
            TokenCredential credential =
                new ClientSecretCredential(
                    ActiveDirectoryTenantId,
                    ActiveDirectoryApplicationId,
                    ActiveDirectoryApplicationSecret,
                    new TokenCredentialOptions()
            {
                AuthorityHost = ActiveDirectoryAuthEndpoint
            });

            // Create a client that can authenticate using our token credential
            BlobChangeFeedClient service = new BlobChangeFeedClient(ActiveDirectoryBlobUri, credential);

            List <BlobChangeFeedEvent> changeFeedEvents = new List <BlobChangeFeedEvent>();

            // Make a service request to verify we've successfully authenticated
            foreach (BlobChangeFeedEvent changeFeedEvent in service.GetChanges())
            {
                changeFeedEvents.Add(changeFeedEvent);
            }
        }
Example #6
0
        public void ChangeFeedPollForEventsWithCursor()
        {
            // Get a connection string to our Azure Storage account.
            string connectionString = ConnectionString;

            // Get a new change feed client.
            BlobChangeFeedClient       changeFeedClient = new BlobChangeFeedClient(connectionString);
            List <BlobChangeFeedEvent> changeFeedEvents = new List <BlobChangeFeedEvent>();

            // Create the start time.  The change feed client will round start time down to
            // the nearest hour if you provide DateTimeOffsets
            // with minutes and seconds.
            DateTimeOffset startTime = DateTimeOffset.Now;

            // Create polling interval.
            TimeSpan pollingInterval = TimeSpan.FromMinutes(5);

            // Get initial set of events.
            IEnumerable <Page <BlobChangeFeedEvent> > pages = changeFeedClient.GetChanges(start: startTime).AsPages();

            string continuationToken = null;

            while (true)
            {
                foreach (Page <BlobChangeFeedEvent> page in pages)
                {
                    foreach (BlobChangeFeedEvent changeFeedEvent in page.Values)
                    {
                        changeFeedEvents.Add(changeFeedEvent);
                    }

                    // Get the change feed continuation token.  The continuation token is not required to get each page of events,
                    // it is intended to be saved and used to resume iterating at a later date.
                    // For the purpose of actively listening to events the continuation token from last page is used.
                    continuationToken = page.ContinuationToken;
                }

                // Wait before processing next batch of events.
                Thread.Sleep(pollingInterval);

                // Resume from last continuation token and fetch latest set of events.
                pages = changeFeedClient.GetChanges(continuationToken).AsPages();
            }
        }
Example #7
0
        public void Test()
        {
            BlobServiceClient              service = GetServiceClient_SharedKey();
            BlobChangeFeedClient           blobChangeFeedClient = service.GetChangeFeedClient();
            Pageable <BlobChangeFeedEvent> blobChangeFeedPagable
                = blobChangeFeedClient.GetChanges();
            IList <BlobChangeFeedEvent> list = blobChangeFeedPagable.ToList();

            foreach (BlobChangeFeedEvent e in list)
            {
                Console.WriteLine(e);
            }
        }
Example #8
0
        public void ChangeFeedResumeWithCursor()
        {
            // Get a connection string to our Azure Storage account.
            string connectionString = ConnectionString;

            // Get a new blob service client.
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

            // Get a new change feed client.
            BlobChangeFeedClient       changeFeedClient = blobServiceClient.GetChangeFeedClient();
            List <BlobChangeFeedEvent> changeFeedEvents = new List <BlobChangeFeedEvent>();

            IEnumerator <Page <BlobChangeFeedEvent> > enumerator = changeFeedClient
                                                                   .GetChanges()
                                                                   .AsPages(pageSizeHint: 10)
                                                                   .GetEnumerator();

            ;

            enumerator.MoveNext();

            foreach (BlobChangeFeedEvent changeFeedEvent in enumerator.Current.Values)
            {
                changeFeedEvents.Add(changeFeedEvent);
            }

            // get the change feed cursor.  The cursor is not required to get each page of events,
            // it is intended to be saved and used to resume iterating at a later date.
            string cursor = enumerator.Current.ContinuationToken;

            // Resume iterating from the pervious position with the cursor.
            foreach (BlobChangeFeedEvent changeFeedEvent in changeFeedClient.GetChanges(
                         continuationToken: cursor))
            {
                changeFeedEvents.Add(changeFeedEvent);
            }
        }
        public void ChangeFeed()
        {
            // Get a connection string to our Azure Storage account.
            string connectionString = ConnectionString;

            // Get a new change feed client.
            BlobChangeFeedClient changeFeedClient = new BlobChangeFeedClient(connectionString);

            // Get all the events in the change feed.
            List <BlobChangeFeedEvent> changeFeedEvents = new List <BlobChangeFeedEvent>();

            foreach (BlobChangeFeedEvent changeFeedEvent in changeFeedClient.GetChanges())
            {
                changeFeedEvents.Add(changeFeedEvent);
            }
        }
        public void SharedAccessSignatureAuth()
        {
            // Create a blob level SAS that allows reading change events from blob
            // level APIs
            AccountSasBuilder sas = new AccountSasBuilder
            {
                // Allow access to blobs
                Services = AccountSasServices.Blobs,

                // Allow access to the service level APIs
                ResourceTypes = AccountSasResourceTypes.All,

                // Access expires in 1 hour!
                ExpiresOn = DateTimeOffset.UtcNow.AddHours(1)
            };

            // Allow all access
            sas.SetPermissions(AccountSasPermissions.All);

            // Create a SharedKeyCredential that we can use to sign the SAS token
            StorageSharedKeyCredential credential = new StorageSharedKeyCredential(StorageAccountName, StorageAccountKey);

            // Build a SAS URI
            UriBuilder sasUri = new UriBuilder(StorageAccountBlobUri);

            sasUri.Query = sas.ToSasQueryParameters(credential).ToString();

            // Create a client that can authenticate with the SAS URI
            BlobChangeFeedClient service = new BlobChangeFeedClient(sasUri.Uri);

            List <BlobChangeFeedEvent> changeFeedEvents = new List <BlobChangeFeedEvent>();

            // Make a service request to verify we've successfully authenticated
            foreach (BlobChangeFeedEvent changeFeedEvent in service.GetChanges())
            {
                changeFeedEvents.Add(changeFeedEvent);
            }
        }
        public static void Run([TimerTrigger("0 */30 * * * *")] TimerInfo myTimer, ILogger log)
        {
            // Create client for Changefeed
            BlobServiceClient    blobServiceClient = new BlobServiceClient(connectionString);
            BlobChangeFeedClient changeFeedClient  = blobServiceClient.GetChangeFeedClient();

            try
            {
                BlobContainerClient containerClient = blobServiceClient.CreateBlobContainer("cursorstoragecontainer");
            }
            catch
            {
                BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("cursorstoragecontainer");
            }
            BlobClient blobClient = new BlobClient(connectionString, "cursorstoragecontainer", "cursorBlob");

            // Create and get cursor for Changefeed
            string continuationToken = GetCursor(blobClient, changeFeedClient, log);

            // Loop through the events in Changefeed with the continuationToken
            foreach (Page <BlobChangeFeedEvent> page in changeFeedClient.GetChanges(continuationToken: continuationToken).AsPages())
            {
                foreach (BlobChangeFeedEvent changeFeedEvent in page.Values)
                {
                    if (containerCheck.Invoke(changeFeedEvent) && eventTypeCheck.Invoke(changeFeedEvent) && blobCheck.Invoke(changeFeedEvent))
                    {
                        log.LogInformation($"event: {changeFeedEvent.EventType} at {changeFeedEvent.Subject.Replace("/blobServices/default","")} on {changeFeedEvent.EventTime}");
                    }
                }
                continuationToken = page.ContinuationToken;
            }

            // Upload new continuationToken to blob
            var upStream = new MemoryStream(Encoding.ASCII.GetBytes(continuationToken));

            blobClient.Upload(upStream, true);
        }
        public void ConnectionStringAuth()
        {
            // Get a connection string to our Azure Storage account.  You can
            // obtain your connection string from the Azure Portal (click
            // Access Keys under Settings in the Portal Storage account blade)
            // or using the Azure CLI with:
            //
            //     az storage account show-connection-string --name <account_name> --resource-group <resource_group>
            //
            // And you can provide the connection string to your application
            // using an environment variable.
            string connectionString = ConnectionString;

            // Create a client that can authenticate with a connection string
            BlobChangeFeedClient service = new BlobChangeFeedClient(connectionString);

            List <BlobChangeFeedEvent> changeFeedEvents = new List <BlobChangeFeedEvent>();

            // Make a service request to verify we've successfully authenticated
            foreach (BlobChangeFeedEvent changeFeedEvent in service.GetChanges())
            {
                changeFeedEvents.Add(changeFeedEvent);
            }
        }