Beispiel #1
0
        public async Task AddEntry(string countryCode, TBlobType entry, DateTime?dateTime = null, CancellationToken cancellationToken = default)
        {
            await using var stream = new MemoryStream();
            await JsonSerializer.SerializeAsync(stream, entry, _jsonSerializerOptions, cancellationToken);

            dateTime ??= DateTime.UtcNow;
            var blobName = $"{countryCode}/{dateTime.Value.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture)}/{_azureBlobRepository.Options.TargetFileName}.json";

            _logger.LogInformation($"Attempt to upload json to [{blobName}].");

            try
            {
                await _azureBlobRepository.SetContainer(_azureBlobRepository.Options.InitialContainerName);

                var uri = await _azureBlobRepository.UploadAsync(
                    blobName,
                    stream,
                    false,
                    false,
                    cancellationToken,
                    httpHeaders : new BlobHttpHeaders
                {
                    ContentType  = MimeType.Json,
                    CacheControl = $"max-age={_azureBlobRepository.Options.CacheControlInSeconds}"     // 1h
                });

                _logger.LogInformation($"Upload json to [{uri}] successfully.");
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Error uploading json file [{blobName}] to [{_azureBlobRepository.Options.GetBlobServiceClient().Uri}]");
                throw;
            }
        }