/// <summary> /// Gets the file path and parses its contents /// </summary> /// <param name="filePathSource"> The path of the file.</param> /// <returns>A json string of file contents.</returns> public async Task <string> ReadFromFile(string filePathSource) { FileServiceHelper.CheckArgumentNullOrEmpty(filePathSource, nameof(filePathSource)); CheckFileFormat(filePathSource); (_containerName, _blobName) = FileServiceHelper.RetrieveFilePathSourceValues(filePathSource); if (CloudStorageAccount.TryParse(_connectionString, out CloudStorageAccount storageAccount)) { CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(_containerName); if (await container.ExistsAsync()) { CloudBlockBlob blob = container.GetBlockBlobReference(_blobName); if (await blob.ExistsAsync()) { return(await blob.DownloadTextAsync()); } else { throw new IOException($"The '{_blobName}' blob doesn't exist."); } } else { throw new IOException($"The '{_containerName}' container doesn't exist."); } } throw new IOException("Failed to connect to the blob storage account."); }
public async Task BindToBlobViaPOCO() { using (JobHost host = TestHelpers.GetJobHost(this.loggerProvider, nameof(this.BindToBlobViaPOCO), false)) { await host.StartAsync(); string connectionString = TestHelpers.GetStorageConnectionString(); CloudStorageAccount account = CloudStorageAccount.Parse(connectionString); this.output.WriteLine($"Using storage account: {account.Credentials.AccountName}"); // Blob and container names need to be kept in sync with the activity code. var data = new { InputPrefix = "Input", OutputPrefix = "Output", Suffix = 42, }; const string ContainerName = "test"; string inputBlobName = $"{data.InputPrefix}-{data.Suffix}"; string outputBlobName = $"{data.OutputPrefix}-{data.Suffix}"; CloudBlobClient blobClient = account.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(ContainerName); if (await container.CreateIfNotExistsAsync()) { this.output.WriteLine($"Created container '{container.Name}'."); } string randomData = Guid.NewGuid().ToString("N"); this.output.WriteLine($"Creating blob named {outputBlobName}..."); CloudBlockBlob blob = container.GetBlockBlobReference(inputBlobName); await blob.UploadTextAsync(randomData); this.output.WriteLine($"Uploaded text '{randomData}' to {blob.Name}."); // Using StartOrchestrationArgs to start an activity function because it's easier than creating a new type. var startArgs = new StartOrchestrationArgs(); startArgs.FunctionName = nameof(TestActivities.BindToBlobViaJsonPayload); startArgs.Input = data; var timeout = Debugger.IsAttached ? TimeSpan.FromMinutes(5) : TimeSpan.FromSeconds(30); var client = await host.StartOrchestratorAsync(nameof(TestOrchestrations.CallActivity), startArgs, this.output); var status = await client.WaitForCompletionAsync(timeout, this.output); Assert.Equal(OrchestrationRuntimeStatus.Completed, status?.RuntimeStatus); this.output.WriteLine($"Searching for blob named {outputBlobName}..."); CloudBlockBlob newBlob = container.GetBlockBlobReference(outputBlobName); string copiedData = await newBlob.DownloadTextAsync(); this.output.WriteLine($"Downloaded text '{copiedData}' from {newBlob.Name}."); Assert.Equal(randomData, copiedData); await host.StopAsync(); } }
static string GetBlobData(CloudBlockBlob cloudBlockBlob) { return(AsyncHelpers.GetResult(() => cloudBlockBlob.DownloadTextAsync(TextEncoding, new AccessCondition(), new BlobRequestOptions { RetryPolicy = new ExponentialRetry() }, new OperationContext()))); }
private async Task <IEnumerable <string> > DownloadBlobAndParse(IList <Uri> blobUri) { CloudStorageAccount cloudAccount = GetCloudStorageAccountHelper(); CloudBlobClient blobClient = cloudAccount.CreateCloudBlobClient(); var result = new List <string>(); foreach (Uri uri in blobUri) { var blob = new CloudBlockBlob(uri, blobClient); string allData = await blob.DownloadTextAsync(); var splitData = allData.Split("\n"); foreach (var entry in splitData) { if (string.IsNullOrWhiteSpace(entry)) { continue; } result.Add(entry); } } return(result); }
public async Task <AggregateStats> GetAggregateStats() { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_connectionString); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); if (_readAccessGeoRedundant) { blobClient.DefaultRequestOptions.LocationMode = LocationMode.PrimaryThenSecondary; } CloudBlobContainer container = blobClient.GetContainerReference("nuget-cdnstats"); CloudBlockBlob blob = container.GetBlockBlobReference("stats-totals.json"); //Check if the report blob is present before processing it. if (!blob.Exists()) { throw new StatisticsReportNotFoundException(); } string totals = await blob.DownloadTextAsync(); var json = JsonConvert.DeserializeObject <AggregateStats>(totals); return(json); }
/// <inheritdoc/> public async Task DeleteTenantAsync(string tenantId) { ArgumentNullException.ThrowIfNull(tenantId); if (tenantId == this.Root.Id) { throw new InvalidOperationException("You can not delete the root tenant."); } try { (_, CloudBlobContainer container) = await this.GetContainerAndTenantForChildTenantsOf(TenantExtensions.GetRequiredParentId(tenantId)).ConfigureAwait(false); CloudBlockBlob blob = GetLiveTenantBlockBlobReference(tenantId, container); string blobText = await blob.DownloadTextAsync().ConfigureAwait(false); CloudBlockBlob deletedBlob = container.GetBlockBlobReference(DeletedTenantsPrefix + tenantId); await deletedBlob.UploadTextAsync(blobText).ConfigureAwait(false); await blob.DeleteIfExistsAsync().ConfigureAwait(false); } catch (FormatException fex) { throw new TenantNotFoundException("Unsupported tenant ID", fex); } catch (StorageException ex) when(ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.NotFound) { throw new TenantNotFoundException(); } }
public async Task <bool> IsAllowedToSpecifyCategory(string id) { CloudBlobClient client = _account.CreateCloudBlobClient(); CloudBlobContainer container = client.GetContainerReference("categorization"); CloudBlockBlob blob = container.GetBlockBlobReference("allowed.json"); try { string json = await blob.DownloadTextAsync(); JObject obj = JObject.Parse(json); foreach (string registration in obj["registrations"]) { if (registration.Equals(id, StringComparison.InvariantCultureIgnoreCase)) { return(true); } } return(false); } catch { return(false); } }
private string RetrieveLatestBlobContents(string storagePath) { int slashIndex = storagePath.IndexOf(BackSlash); string prefix = storagePath.Substring(0, slashIndex); var blobDirectory = _container.GetDirectoryReference(prefix); BlobContinuationToken continuationToken = null; List <IListBlobItem> blobItems = new List <IListBlobItem>(); do { BlobResultSegment blobResultSegment = blobDirectory.ListBlobsSegmentedAsync(continuationToken).Result; continuationToken = blobResultSegment.ContinuationToken; blobItems.AddRange(blobResultSegment.Results); }while (continuationToken != null); IList <CloudBlockBlob> cloudBlockBlobs = blobItems.OfType <CloudBlockBlob>().OrderByDescending(b => b.Uri.AbsolutePath).ToList(); CloudBlockBlob latestBlockBlob = cloudBlockBlobs.FirstOrDefault(); string blobContents = latestBlockBlob.DownloadTextAsync().Result; return(blobContents); }
public async Task <string> Download(Uri blobUri) { CloudBlobContainer container = GetBlobContainer(); CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobUri.ToString()); return(await blockBlob.DownloadTextAsync()); }
public async Task OpenBlobUsingSAS() { try { await OpenContainerAsync($"blobsdemoSAS{DateTime.Today:yymmdd}", "sas-blob", true); CloudBlockBlob blob = CloudBlobContainer.GetBlockBlobReference("CreatedBySas.txt"); await blob.UploadTextAsync("File created using SAS Uri"); CloudBlockBlob testBlob = CloudBlobContainer.GetBlockBlobReference(blob.Name); var fileContent = await testBlob.DownloadTextAsync(); Console.WriteLine("File text: {0}", fileContent); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("Press Y to delete Azure container."); var key = Console.ReadKey(); Console.WriteLine(); if (key.KeyChar.ToString().ToUpper() == "Y") { await DeleteContainerAsync(); } } }
public async Task CheckEncryption() { try { await OpenContainerAsync($"blobsdemo{DateTime.Today:yymmdd}"); CloudBlockBlob blob = CloudBlobContainer.GetBlockBlobReference("encrypted.txt"); await blob.UploadTextAsync("test"); await blob.FetchAttributesAsync(); Console.WriteLine($"Blob is encrypted: {blob.Properties.IsServerEncrypted}"); CloudBlockBlob testBlob = CloudBlobContainer.GetBlockBlobReference(blob.Name); await testBlob.DownloadTextAsync(); Console.WriteLine($"Blob is encrypted: {testBlob.Properties.IsServerEncrypted}"); } finally { Console.WriteLine("Press Y to delete Azure container."); var key = Console.ReadKey(); Console.WriteLine(); if (key.KeyChar.ToString().ToUpper() == "Y") { await DeleteContainerAsync(); } } }
private static async Task <string> PostScore(CloudBlockBlob leaderboard, string name, int score, ILogger log) { Leaderboard board; try { board = JsonConvert.DeserializeObject <Leaderboard>(await leaderboard.DownloadTextAsync()); if (board.Scores == null) { board.Scores = new List <PlayerScore>(); } } catch (Exception e) // File is probably corrupted somehow { //board = new Leaderboard(); log.LogError(e.Message); throw; } var newScore = new PlayerScore { Name = name, Score = score, Timestamp = DateTime.UtcNow }; board.Scores.Add(newScore); await leaderboard.UploadTextAsync(JsonConvert.SerializeObject(board)); log.LogInformation($"Score logged: {JsonConvert.SerializeObject(newScore)}"); return(JsonConvert.SerializeObject(board)); }
/// <summary> /// Download the contents of the blob at the url to text. /// </summary> /// <param name="url">The URL of the file.</param> /// <returns></returns> public async Task <string> DownloadToTextAsync(string url) { _logger.LogInformation($"Download content to text from {url}"); var cloudBlob = new CloudBlockBlob(new Uri(url)); return(await cloudBlob.DownloadTextAsync()); }
private async Task InsertDocument(CloudBlobClient blobClient, DocumentClient docClient, DocumentCollection collection, IEnumerable <IListBlobItem> blobs) { string dirName; string blobName; foreach (IListBlobItem b in blobs) { dirName = b.Uri.Segments[2].Substring(0, 8); blobName = b.Uri.Segments[3].Substring(0, 16); var container = blobClient.GetContainerReference(containerSourceName); var dir = container.GetDirectoryReference(dirName); CloudBlockBlob blobSrc = dir.GetBlockBlobReference(blobName); String doc = await blobSrc.DownloadTextAsync(); Dictionary <string, object> docDict = JsonConvert.DeserializeObject <Dictionary <string, object> >(doc); docDict["partitionKey"] = docDict["custid"] + "_" + docDict["siteid"]; try { ResourceResponse <Document> response = await docClient.CreateDocumentAsync( UriFactory.CreateDocumentCollectionUri(DatabaseName, DataCollectionName), docDict, new RequestOptions() { }); } catch (Exception e) { Console.WriteLine("Failed to write {0}. Exception was {1}", JsonConvert.SerializeObject(docDict), e); } } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "todo3/{id}")] HttpRequest request, [Blob("todos/{id}.json", Connection = "AzureWebJobsStorage")] CloudBlockBlob blob, ILogger logger) { if (blob == null) { return(new NotFoundResult()); } var updatedContent = await new StreamReader(request.Body).ReadToEndAsync(); var updatedTodo = JsonConvert.DeserializeObject <TodoUpdateDto>(updatedContent); var existingTodoContent = await blob.DownloadTextAsync(); var existingTodo = JsonConvert.DeserializeObject <TodoUpdateDto>(existingTodoContent); if (!string.IsNullOrEmpty(updatedTodo.Description)) { existingTodo.Description = updatedTodo.Description; } existingTodo.IsCompleted = updatedTodo.IsCompleted; await blob.UploadTextAsync(JsonConvert.SerializeObject(existingTodo)); return(new OkObjectResult(existingTodo)); }
public static string ReadBlob(string configBlobName) { // load signalr config CloudStorageAccount storageAccount = null; CloudBlobContainer cloudBlobContainer = null; var content = ""; var AzureStorageConnectionString = Environment.GetEnvironmentVariable("AzureStorageConnectionString"); Console.WriteLine($"AzureStorageConnectionString : {AzureStorageConnectionString }"); Console.WriteLine($"container: {Environment.GetEnvironmentVariable("ConfigBlobContainerName")}"); Console.WriteLine($"configkey: {configBlobName}"); if (CloudStorageAccount.TryParse(AzureStorageConnectionString, out storageAccount)) { try { CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); cloudBlobContainer = cloudBlobClient.GetContainerReference(Environment.GetEnvironmentVariable("ConfigBlobContainerName")); CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(Environment.GetEnvironmentVariable(configBlobName)); content = cloudBlockBlob.DownloadTextAsync().GetAwaiter().GetResult(); } catch (StorageException ex) { Console.WriteLine("Error returned from the service: {0}", ex.Message); } } return(content); }
private async Task PostApplicationInsightsQueryResultsToTeamsAsync(dynamic result, dynamic alert, string formattedStart, string formattedEnd) { _httpClient.DefaultRequestHeaders.Remove("x-api-key"); var templateUri = $"{_configValue.Invoke("MessageCardTemplateBaseUrl").TrimEnd('/')}/{alert.data.essentials.alertRule}.json"; _log.LogInformation($"Using template '{templateUri}'"); const string StorageResource = "https://storage.azure.com/"; var clientId = _configValue.Invoke("identityClientId"); var azureServiceTokenProvider = new AzureServiceTokenProvider(string.IsNullOrWhiteSpace(clientId) ? null : $"RunAs=App;AppId={clientId}"); var tokenCredential = new TokenCredential(await azureServiceTokenProvider.GetAccessTokenAsync(StorageResource)); var storageCredentials = new StorageCredentials(tokenCredential); var blob = new CloudBlockBlob(new Uri(templateUri), storageCredentials); var cardTemplate = await blob.DownloadTextAsync(); if ((int)result.tables.Count <= 0) { return; } if ((int)result.tables[0].columns.Count <= 0) { return; } var columns = new List <string>(); foreach (var column in result.tables[0].columns) { columns.Add((string)column.name); } foreach (var row in result.tables[0].rows) { var currentMessage = cardTemplate .Replace("[[alert.data.essentials.alertRule]]", (string)alert.data.essentials.alertRule) .Replace("[[alert.data.essentials.description]]", (string)alert.data.essentials.description) .Replace("[[alert.data.essentials.severity]]", (string)alert.data.essentials.severity) .Replace("[[alert.alertContext.LinkToSearchResults]]", (string)alert.data.alertContext.LinkToSearchResults) .Replace("[[alert.alertContext.Threshold]]", (string)alert.data.alertContext.Threshold) .Replace("[[alert.alertContext.Operator]]", (string)alert.data.alertContext.Operator) .Replace("[[alert.alertContext.SearchIntervalDurationMin]]", (string)alert.data.alertContext.SearchIntervalDurationMin) .Replace("[[alert.alertContext.SearchIntervalInMinutes]]", (string)alert.data.alertContext.SearchIntervalInMinutes) .Replace("[[alert.alertContext.SearchIntervalStartTimeUtc]]", formattedStart) .Replace("[[alert.alertContext.SearchIntervalEndtimeUtc]]", formattedEnd); foreach (var column in result.tables[0].columns) { currentMessage = currentMessage .Replace($"[[searchResult.{(string)column.name}]]", (string)row[columns.IndexOf((string)column.name)]); } _log.LogDebug($"Sending data: {currentMessage}"); var appId = (string)alert.data.alertContext.ApplicationId; await _httpClient.PostAsync(_configValue.Invoke($"PostToUrl-{appId}"), new StringContent(currentMessage, Encoding.UTF8, "application/json")); } }
public List <Gamer> GetAllGamers() { try { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); _blobClient = storageAccount.CreateCloudBlobClient(); _blobContainer = _blobClient.GetContainerReference(_blobContainerName); CloudBlockBlob blob = _blobContainer.GetBlockBlobReference("players.txt"); string text = blob.DownloadTextAsync().Result; var lines = text.Split("\r\n"); foreach (var line in lines) { if (!string.IsNullOrEmpty(line)) { var t = line.Split(", "); gamers.Add(new Gamer { Id = t[0], Name = t[1] }); } } return(gamers); } catch (Exception ex) { return(null); } }
public static async Task <BlobInfo> Run([BlobTrigger("%BlobContainer_Raw%/{id}.{country}")] CloudBlockBlob blob, string country, Binder binder, ILogger log) { var text = await blob.DownloadTextAsync(); var customer = JsonConvert.DeserializeObject <Customer>(text); var newFilename = $"{customer.Id}__{customer.Name.Replace(" ", "_")}__{country.ToLower()}.txt"; var path = $"%BlobContainer_Processed%/{newFilename}"; var attributes = new Attribute[] { new BlobAttribute(path, FileAccess.Write) }; using (var writer = await binder.BindAsync <TextWriter>(attributes)) { await writer.WriteAsync(text); } var blobInfo = new BlobInfo() { Container = Environment.GetEnvironmentVariable("BlobContainer_Processed"), FileName = newFilename, Country = country }; await blob.DeleteAsync(); return(blobInfo); }
public async Task QueueTriggerToBlobTest() { string id = Guid.NewGuid().ToString(); string messageContent = string.Format("{{ \"id\": \"{0}\" }}", id); CloudQueueMessage message = new CloudQueueMessage(messageContent); await Fixture.TestQueue.AddMessageAsync(message); CloudBlockBlob resultBlob = null; await TestHelpers.Await(() => { resultBlob = Fixture.TestContainer.GetBlockBlobReference(id); return(resultBlob.Exists()); }); string result = await resultBlob.DownloadTextAsync(); Assert.Equal(RemoveWhitespace(messageContent), RemoveWhitespace(result)); TraceEvent scriptTrace = Fixture.TraceWriter.Traces.SingleOrDefault(p => p.Message.Contains(id)); Assert.Equal(TraceLevel.Verbose, scriptTrace.Level); string trace = RemoveWhitespace(scriptTrace.Message); Assert.True(trace.Contains(RemoveWhitespace("script processed queue message"))); Assert.True(trace.Contains(RemoveWhitespace(messageContent))); }
private async Task <string> GetVisualModerationJsonFile(IAzureMediaServicesClient client, string resourceGroupName, string accountName, string assetName) { ListContainerSasInput parameters = new ListContainerSasInput(); AssetContainerSas assetContainerSas = client.Assets.ListContainerSas(resourceGroupName, accountName, assetName, permissions: AssetContainerPermission.Read, expiryTime: DateTime.UtcNow.AddHours(1).ToUniversalTime()); Uri containerSasUrl = new Uri(assetContainerSas.AssetContainerSasUrls.FirstOrDefault()); CloudBlobContainer container = new CloudBlobContainer(containerSasUrl); var blobs = container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.None, 200, null, null, null).Result; string jsonString = null; foreach (var blobItem in blobs.Results) { if (blobItem is CloudBlockBlob) { CloudBlockBlob blob = blobItem as CloudBlockBlob; if (blob.Name == "contentmoderation.json") { jsonString = await blob.DownloadTextAsync(); } } } return(jsonString); }
public async Task <string> DownloadBlob(string container, string name) { CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(container); CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference(name); return(await blob.DownloadTextAsync()); }
public static async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "v1/azuresearch/performance/blobstorage/serially/repetitions/{repetitions}")] HttpRequestMessage req, int repetitions, ExecutionContext executionContext, ILogger log) { List <string> ids = Common.IdsList; DateTime startTime = DateTime.Now; StorageCredentials storageCredentials = new StorageCredentials( Environment.GetEnvironmentVariable("storageAccountName", EnvironmentVariableTarget.Process), Environment.GetEnvironmentVariable("storageAccountKey", EnvironmentVariableTarget.Process)); CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(storageCredentials, useHttps: true); CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient(); CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference("providers"); List <KyruusDataStructure> providers = new List <KyruusDataStructure>(ids.Count); for (int r = 0; r < repetitions; r++) { foreach (string id in ids) { CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference($"ky-2019-04-25-16-00-00-0387-Utc/{id}.json"); string doc = await cloudBlockBlob.DownloadTextAsync(); KyruusDataStructure p = JsonConvert.DeserializeObject <KyruusDataStructure>(doc); providers.Add(p); } } return(req.CreateResponse( HttpStatusCode.OK, $"{repetitions} repetitions in {nameof(BlobStorageSerial)}->{executionContext.FunctionName}(): {(DateTime.Now - startTime).TotalMilliseconds}, per repetition {(DateTime.Now - startTime).TotalMilliseconds/repetitions}")); }
public static async Task Run( [QueueTrigger("transmission-faults", Connection = "AzureWebJobsStorage")] string fault, IBinder blobFaultBinder, ILogger log) { TransmissionFaultMessage faultData = JsonConvert.DeserializeObject <TransmissionFaultMessage>(fault); CloudBlockBlob blobReader = await blobFaultBinder.BindAsync <CloudBlockBlob>( new BlobAttribute($"transmission-faults/{faultData.id}", FileAccess.ReadWrite)); string json = await blobReader.DownloadTextAsync(); try { List <string> faultMessages = await Task <List <string> > .Factory.StartNew(() => JsonConvert.DeserializeObject <List <string> >(json)); await Utils.SendEvents(faultMessages, log); } catch { log.LogError($"FaultProcessor failed to send: {faultData.id}"); return; } await blobReader.DeleteAsync(); log.LogInformation($"C# Queue trigger function processed: {faultData.id}"); }
/// <inheritdoc/> public override async Task <ScheduleStatus> GetStatusAsync(string timerName) { CloudBlockBlob statusBlob = GetStatusBlobReference(timerName); try { string statusLine = await statusBlob.DownloadTextAsync(); ScheduleStatus status; using (StringReader stringReader = new StringReader(statusLine)) { status = (ScheduleStatus)_serializer.Deserialize(stringReader, typeof(ScheduleStatus)); } return(status); } catch (StorageException exception) { if (exception.RequestInformation != null && exception.RequestInformation.HttpStatusCode == 404) { // we haven't recorded a status yet return(null); } throw; } }
private string DownloadText(CloudBlockBlob blob) { var task = blob.DownloadTextAsync(); task.Wait(); return(task.Result); }
/// <summary> /// Gets the DateTime of the last executed job /// </summary> /// <param name="jobId">Name of the job</param> /// <returns>DateTime of the last executed job or null if no entry found</returns> public async Task <DateTime?> GetLastExecutionDatetimeAsync(string jobId) { if (_container == null) { await InitializeAsync(); } CloudBlockBlob blockBlob = _container.GetBlockBlobReference(jobId); //blob will probably have already been created by sync provider //but we're playing it safe :) if (await blockBlob.ExistsAsync()) { //get the last execution time var str = await blockBlob.DownloadTextAsync(); //empty entry if (string.IsNullOrWhiteSpace(str)) { return(null); } try { return(DateTime.Parse(str, CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal)); } //if we fail to get a valid datetimestring for any reason //e.g. the Lease provider just created the blob catch { return(null); } } return(null); }
/// <inheritdoc /> public async Task <EpochValue> GetEpochAsync(string partitionId, CancellationToken cancellationToken) { EpochValue epochValue = null; using (_logger.BeginScope("Get Epoch")) { _epochReadCounter.Increment(); _logger.LogInformation("Getting epoch for partition {partitionId}", partitionId); CloudBlockBlob epochBlob = _consumerGroupDirectory.GetBlockBlobReference(partitionId); string jsonEpoch; using (_storagePerformanceSummary.Time()) { jsonEpoch = await epochBlob.DownloadTextAsync(_leaseEncoding, _defaultAccessCondition, _defaultRequestOptions, _operationContext).ConfigureAwait(false); } _logger.LogDebug("Retrieved epoch for partition {id} :: {json}", partitionId, jsonEpoch); epochValue = JsonConvert.DeserializeObject <EpochValue>(jsonEpoch); epochValue.ConcurrencyValue = epochBlob.Properties.ETag; } return(epochValue); }
public static async Task <IActionResult> UpdateTodo( [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = Route + "/{id}")] HttpRequest req, [Blob(BlobPath + "/{id}.json", Connection = "AzureWebJobsStorage")] CloudBlockBlob blob, ILogger log, string id) { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var updated = JsonConvert.DeserializeObject <TodoUpdateModel>(requestBody); if (!await blob.ExistsAsync()) { return(new NotFoundResult()); } var existingText = await blob.DownloadTextAsync(); var existingTodo = JsonConvert.DeserializeObject <Todo>(existingText); existingTodo.IsCompleted = updated.IsCompleted; if (!string.IsNullOrEmpty(updated.TaskDescription)) { existingTodo.TaskDescription = updated.TaskDescription; } await blob.UploadTextAsync(JsonConvert.SerializeObject(existingTodo)); return(new OkObjectResult(existingTodo)); }
public async Task <StatisticsReport> Load(string name) { // In NuGet we always use lowercase names for all blobs in Azure Storage name = name.ToLowerInvariant(); string connectionString = _connectionString; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference("stats"); CloudBlockBlob blob = container.GetBlockBlobReference("popularity/" + name); //Check if the report blob is present before processing it. if (!blob.Exists()) { throw new StatisticsReportNotFoundException(); } MemoryStream stream = new MemoryStream(); await blob.FetchAttributesAsync(); string content = await blob.DownloadTextAsync(); return(new StatisticsReport(content, (blob.Properties.LastModified == null ? (DateTime?)null : blob.Properties.LastModified.Value.UtcDateTime))); }