Ejemplo n.º 1
0
        /// <summary>
        /// Constructor for the Google Cloud Storage subscription storage
        /// </summary>
        /// <param name="storageClient">Reference to the single instance of Google StorageClient</param>
        /// <param name="loggerFactory">Reference to the logger factory to create the logger</param>
        /// <param name="options">Options to configure the subscription storage</param>
        public GoogleCloudStorageSubscriptionsStorage(StorageClient storageClient, IRebusLoggerFactory loggerFactory, GoogleCloudStorageSubscriptionOptions options)
        {
            _storageClient = storageClient ?? throw new ArgumentNullException(nameof(storageClient));
            _options       = options ?? throw new ArgumentNullException(nameof(options));

            // Auto create the bucket when we start up, if required
            CloudUtils.CreateBucketIfNotExists(storageClient, loggerFactory, options);

            // Create our retry policy with Polly with a exponential back off strategy for retries, with jitter and retrying immediately on the first failure
            _retry = CloudUtils.CreateRetryPolicy(options);
        }
        /// <summary>
        /// Reads data from the data bus
        /// </summary>
        /// <param name="id">ID of the data bus object</param>
        /// <returns>Stream to read the object from</returns>
        public async Task <Stream> Read(string id)
        {
            return(await RetryCatchAsync(id, async objectName =>
            {
                // Make sure we update the read time for the object
                if (!_options.DoNotUpdateLastReadTime)
                {
                    await UpdateLastReadTimeAsync(objectName);
                }

                // Build an HTTP request to be able to stream the object from the bucket as the API does not have a
                // direct function for that.
                return await CloudUtils.DownloadAsStreamAsync(_storageClient, _options.BucketName, objectName);
            }));
        }