protected override async Task ExecuteCoreAsync(ReportExecutionContext context) { context.Log.LogInformation($"Started function execution: {DateTime.Now}"); var storageConnString = Environment.GetEnvironmentVariable("AzureWebJobsStorage"); BlobServiceClient bsc = new BlobServiceClient(storageConnString); BlobContainerClient bcc = bsc.GetBlobContainerClient(ContainerName); // create the container bcc.CreateIfNotExists(); context.Log.LogInformation("Storage account accessed"); DateTime to = DateTime.UtcNow; foreach (RepositoryConfiguration repositoryConfiguration in context.RepositoryConfigurations) { // retrieve the last accessed time for this repository BlobClient bc = bcc.GetBlobClient($"{repositoryConfiguration.Owner}_{repositoryConfiguration.Name}"); DateTime lastDateRun = DateTime.UtcNow.AddDays(-1); try { string content = StreamHelpers.GetContentAsString(bc.Download().Value.Content); lastDateRun = DateTime.Parse(content); } catch { } context.Log.LogInformation("Last processed date for {0} is {1}", repositoryConfiguration, lastDateRun); string owner = repositoryConfiguration.Owner; string repo = repositoryConfiguration.Name; context.Log.LogInformation("Processing repository {0}\\{1}", owner, repo); HtmlPageCreator emailBody = new HtmlPageCreator($"New items in {repo}"); // get new issues RetrieveNewItems(context, CreateQueryForNewItems(repositoryConfiguration, IssueIsQualifier.Issue), lastDateRun, emailBody, "New issues"); // get new PRs RetrieveNewItems(context, CreateQueryForNewItems(repositoryConfiguration, IssueIsQualifier.PullRequest), lastDateRun, emailBody, "New PRs"); // get needs attention issues RetrieveNeedsAttentionIssues(context, repositoryConfiguration, emailBody); emailBody.AddContent($"<p>Last checked range: {lastDateRun} -> {to} </p>"); context.Log.LogInformation("Sending email..."); // send the email EmailSender.SendEmail(context.SendGridToken, context.FromAddress, emailBody.GetContent(), repositoryConfiguration.ToEmail, repositoryConfiguration.CcEmail, $"New issues in the {repo} repo as of {to.ToShortDateString()}", context.Log); context.Log.LogInformation("Email sent..."); bc.Upload(StreamHelpers.GetStreamForString(to.ToUniversalTime().ToString()), overwrite: true); context.Log.LogInformation($"Persisted last event time for {repositoryConfiguration.Owner}\\{repositoryConfiguration.Name} as {to}"); } }
private async Task ProcessMessagesAsync(Microsoft.Azure.ServiceBus.Message message, CancellationToken token) { var msg = Encoding.UTF8.GetString(message.Body); string filePath = Path.GetTempFileName(); try { BlobClient blob = _container.GetBlobClient(msg); BlobDownloadInfo download = blob.Download(); using (FileStream file = System.IO.File.Create(filePath)) { download.Content.CopyTo(file); } var ratings = _serializationService.Deserialize(filePath); } catch (Exception ex) { } finally { await _queueClient.CompleteAsync(message.SystemProperties.LockToken); } }
private MemoryStream GetFileFromStorage(string fileName, string container) { if (!initialized) { Initialize(); } try { var containerTuple = ParseContainer(container); container = containerTuple.Item1; fileName = string.Concat(containerTuple.Item2, fileName); BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient(container); BlobClient blob = blobContainerClient.GetBlobClient(fileName); MemoryStream result = new MemoryStream(); Response <BlobDownloadInfo> download = blob.Download(); download.Value.Content.CopyTo(result); result.Position = 0; return(result); } catch (Exception) { return(null); } }
private static void BlobStuff(string connectionString) { BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString); Console.WriteLine($"Connecting to Blob Storage Account:\n\t { blobServiceClient.AccountName }\n"); string containerName = "blobfromapp"; var containerClient = blobServiceClient.GetBlobContainerClient(containerName); containerClient.CreateIfNotExists(); string fileName = "myFile" + Guid.NewGuid().ToString() + ".txt"; BlobClient blobClient = containerClient.GetBlobClient(fileName); Console.WriteLine($"Uploading to Blob Storage:\n\t { blobClient.Uri }\n"); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("Prestifilippo rulez"))) { blobClient.Upload(ms); } foreach (BlobItem item in containerClient.GetBlobs()) { Console.WriteLine(item.Name); } Console.WriteLine($"Downloading blob"); BlobDownloadInfo download = blobClient.Download(); Console.WriteLine($"Complete"); }
private string DownloadBlob(string fileName) { // Define temporary file to download string downloadPath = _config.AzureBlob.TempDownloadFilePath; if (!Directory.Exists(downloadPath)) { Directory.CreateDirectory(downloadPath); } string downloadFileName = Guid.NewGuid().ToString() + ".json"; string downloadFilePath = Path.Combine(downloadPath, downloadFileName); // Get a reference to a blob BlobClient blobClient = _insightsContainerClient.GetBlobClient(fileName); _logger.Info($"Downloading blob { fileName } to { downloadFilePath }"); // Download the blob's contents and save it to a temporary file BlobDownloadInfo download = blobClient.Download(); using (FileStream downloadFileStream = File.OpenWrite(downloadFilePath)) { download.Content.CopyTo(downloadFileStream); downloadFileStream.Close(); } return(downloadFilePath); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { var blobUrl = Environment.GetEnvironmentVariable("BLOB_URL"); var sasToken = GenerateSASToken(); log.LogInformation("C# HTTP trigger function processed a request."); var blobUri = new UriBuilder(blobUrl) { Query = sasToken }.Uri; string content = ""; var blobClient = new BlobClient(blobUri); using (var streamReader = new StreamReader(blobClient.Download().Value.Content)) { content = streamReader.ReadToEnd(); } return(String.IsNullOrWhiteSpace(content)? new BadRequestObjectResult("There is no content, or the SAS token is invalid"): (ActionResult) new OkObjectResult($"Content: {content}")); }
//--------------------------------------------------- // Use Custom Request ID //--------------------------------------------------- public void UseCustomRequestID() { string HOSTNAME = ""; string APPNAME = ""; string USERID = ""; // <Snippet_UseCustomRequestID> var connectionString = Constants.connectionString; BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString); BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient("demcontainer"); BlobClient blobClient = blobContainerClient.GetBlobClient("testImage.jpg"); string clientRequestID = String.Format("{0} {1} {2} {3}", HOSTNAME, APPNAME, USERID, Guid.NewGuid().ToString()); using (HttpPipeline.CreateClientRequestIdScope(clientRequestID)) { BlobDownloadInfo download = blobClient.Download(); using (FileStream downloadFileStream = File.OpenWrite("C:\\testImage.jpg")) { download.Content.CopyTo(downloadFileStream); downloadFileStream.Close(); } } // </Snippet_UseCustomRequestID> }
public bool ExistsInStorageContainer(string path) { BlobContainerClient containerClient = GetFileShareContainer(); BlobClient blobClient = containerClient.GetBlobClient(path); return(blobClient.Download().GetRawResponse().Status == 200); }
public Stream CreateReadStream() { var stream = new MemoryStream(); _blobClient.Download().Value.Content.CopyTo(stream); return(stream); }
public Stream Download(Guid processIdentifier) { if (processIdentifier == Guid.Empty) { throw new ArgumentException("The parameter cannot be empty.", nameof(processIdentifier)); } Process currentProcess = processRepository.Get(processIdentifier); if (currentProcess == null) { throw new Exception($"Cannot found process with identifier {processIdentifier}"); } if (currentProcess.Blob == null) { throw new Exception($"Current blob have not associated blob."); } Blob currentBlob = blobRepository.Get(currentProcess.Id); BlobContainerClient blobContainerClient = new BlobContainerClient(blobStorageOptions.Value.ConnectionString, blobStorageOptions.Value.BlobContainerName); blobContainerClient.CreateIfNotExists(); BlobClient blobClient = new BlobClient(blobStorageOptions.Value.ConnectionString, blobStorageOptions.Value.BlobContainerName, currentBlob.FileName); return(blobClient.Download().Value.Content); }
/// <summary> /// Converts a blob client of a file to an XML document /// </summary> public static XDocument BlobClientToXml(BlobClient blobClient) { //parse string as xml doc var document = XDocument.Load(blobClient.Download().Value.Content); return(document); }
public Stream Download(string fileName) { BlobClient blobClient = _containerClient.GetBlobClient(blobName: fileName); BlobDownloadInfo blobDownloadInfo = blobClient.Download(); return(blobDownloadInfo.Content); }
public AzureBlobsLogReader(BlobContainerClient blobsContainerClient, string fileName) { fileName = AzureBlobsLogsInterface.PathFixer(fileName); _logClient = blobsContainerClient.GetBlobClient(fileName); var downloadRange = new HttpRange(0); _download = _logClient.Download(downloadRange); }
public PSBlobDownloadInfo DownloadBlob() { //TODO: Please repalce with your real account BlobClient client = new BlobClient("", "", ""); BlobDownloadInfo info = client.Download().Value; return(new PSBlobDownloadInfo(info)); }
public BlobDownloadInfo Download(string file) { BlobServiceClient blobServiceClient = new BlobServiceClient(_connStr.Value.Storage); BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("webjob-pdf"); BlobClient blobClient = containerClient.GetBlobClient(file); return(blobClient.Download()); }
static void Main(string[] args) { string downloadDirectory = args[0]; // Create a BlobServiceClient object which will be used to create a container client BlobServiceClient blobServiceClient = new BlobServiceClient(AZURE_STORAGE_CONNECTION_STRING); // Create a unique name for the container string containerName = "startupfiles"; // Create the container and return a container client object BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName); List <BlobItem> blobs = containerClient.GetBlobs().ToList(); // Download the blob files in parallel - maximum of 10 at a time } Parallel.ForEach(blobs, new ParallelOptions() { MaxDegreeOfParallelism = 10 }, blob => { string downloadFilePath = Path.Combine(downloadDirectory, blob.Name); BlobClient blobClient = containerClient.GetBlobClient(blob.Name); BlobDownloadInfo download = blobClient.Download(); using (FileStream downloadFileStream = File.OpenWrite(downloadFilePath)) { download.Content.CopyTo(downloadFileStream); downloadFileStream.Close(); } }); string shareName = "startupfiles"; ShareClient shareClient = new ShareClient(AZURE_FILES_CONNECTION_STRING, shareName); if (shareClient.Exists()) { ShareDirectoryClient shareDirectoryClient = shareClient.GetRootDirectoryClient(); List <ShareFileItem> items = shareDirectoryClient.GetFilesAndDirectories().ToList(); foreach (ShareFileItem item in items) { if (!item.IsDirectory) { string downloadFilePath = Path.Combine(downloadDirectory, item.Name); ShareFileClient shareFileClient = shareDirectoryClient.GetFileClient(item.Name); ShareFileDownloadInfo download = shareFileClient.Download(); using (FileStream downloadFileStream = File.OpenWrite(downloadFilePath)) { download.Content.CopyTo(downloadFileStream); downloadFileStream.Close(); } } } } }
public int TransferAzureBlobFromPatternToDirectory(string prefixPattern, string destPath) { string continuationToken = null; int rtnVal = SetParameters(); int?segmentSize = null; if (rtnVal != 0) { return(rtnVal); } BlobContainerClient blobContainerClient = new BlobContainerClient(storageConnectionString, blobContainerName); try { // Call the listing operation and enumerate the result segment. // When the continuation token is empty, the last segment has been returned // and execution can exit the loop. do { var resultSegment = blobContainerClient.GetBlobs(prefix: prefixPattern).AsPages(continuationToken, segmentSize); foreach (Azure.Page <BlobItem> blobPage in resultSegment) { foreach (BlobItem blobItem in blobPage.Values) { //Console.WriteLine("Blob name: {0}", blobItem.Name); BlobClient blobClient = new BlobClient(storageConnectionString, blobContainerName, blobItem.Name); // Download the blob's contents and save it to a file BlobDownloadInfo download = blobClient.Download(); string outFileName = Path.Combine(destPath, Path.GetFileName(blobItem.Name)); using (FileStream file = File.OpenWrite($"{outFileName}")) { download.Content.CopyTo(file); } } // Get the continuation token and loop until it is empty. continuationToken = blobPage.ContinuationToken; //Console.WriteLine(); } } while (continuationToken != ""); } catch (RequestFailedException e) { errorMsg = e.Message; //Console.WriteLine(e.Message); //Console.ReadLine(); //throw; } return(rtnVal); }
#pragma warning disable CA1822 // Does not acces instance data can be marked static. public virtual async Task <Segment> BuildSegment( #pragma warning restore CA1822 // Can't mock static methods in MOQ. bool async, string manifestPath, SegmentCursor cursor = default) { // Models we need for later List <Shard> shards = new List <Shard>(); DateTimeOffset dateTime = BlobChangeFeedExtensions.ToDateTimeOffset(manifestPath).Value; int shardIndex = cursor?.ShardIndex ?? 0; // Download segment manifest BlobClient blobClient = _containerClient.GetBlobClient(manifestPath); BlobDownloadInfo blobDownloadInfo; if (async) { blobDownloadInfo = await blobClient.DownloadAsync().ConfigureAwait(false); } else { blobDownloadInfo = blobClient.Download(); } // Parse segment manifest JsonDocument jsonManifest; if (async) { jsonManifest = await JsonDocument.ParseAsync(blobDownloadInfo.Content).ConfigureAwait(false); } else { jsonManifest = JsonDocument.Parse(blobDownloadInfo.Content); } int i = 0; foreach (JsonElement shardJsonElement in jsonManifest.RootElement.GetProperty("chunkFilePaths").EnumerateArray()) { string shardPath = shardJsonElement.ToString().Substring("$blobchangefeed/".Length); Shard shard = await _shardFactory.BuildShard( async, shardPath, cursor?.ShardCursors?[i]) .ConfigureAwait(false); shards.Add(shard); i++; } return(new Segment( shards, shardIndex, dateTime)); }
public static void Run([BlobTrigger("conversationkm-raw/{name}", Connection = "conversationstoragev2")] Stream myBlob, string name, ILogger log) { log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes"); List <EventData> eds = null; try { BotFrameworkDataExtractor de = new BotFrameworkDataExtractor(); eds = de.ExtractFromBlob(myBlob); } catch { } if (eds != null) { BlobServiceClient blobServiceClient = new BlobServiceClient(Environment.GetEnvironmentVariable("conversationstoragev2")); BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient(Environment.GetEnvironmentVariable("outputContainerName")); foreach (EventData ed in eds) { BlobClient blobClient = blobContainerClient.GetBlobClient($"{ed.ConversationId}.json"); try { BlobDownloadInfo download = blobClient.Download(); using (StreamReader reader = new StreamReader(download.Content)) { string content = reader.ReadToEnd(); Conversation existingConversation = JsonConvert.DeserializeObject <Conversation>(content); existingConversation.Messages.Add(ed); existingConversation.Messages.Sort((x, y) => x.EventTime.CompareTo(y.EventTime)); UploadConversation(existingConversation, blobClient); } } catch (Azure.RequestFailedException ex) { if (ex.Status == 404) { Conversation conversation = new Conversation() { ConversationId = ed.ConversationId }; conversation.Messages.Add(ed); UploadConversation(conversation, blobClient); } } } } }
private BlobDownloadInfo DownloadContentItem(BlobClient blobClient) { var response = blobClient.Download(); if (response == null) { return(null); } var blobDownloadInfo = response.Value; return(blobDownloadInfo); }
static void Main(string[] args) { try { BlobServiceClient blobServiceClient = new BlobServiceClient(connString); BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName); containerClient.CreateIfNotExists(); //Creating Container #region Code //Check Existence //if(!containerClient.Exists()) //{ // containerClient = blobServiceClient.CreateBlobContainer(containerName); //} #endregion File.WriteAllText(localFilePath, "This is a blob"); BlobClient blobClient = containerClient.GetBlobClient(fileName); //Getting Blob //Check for blobclient existence if (!blobClient.Exists()) { using (FileStream uploadFileStream = File.OpenRead(localFilePath)) { blobClient.UploadAsync(uploadFileStream); } } foreach (BlobItem item in containerClient.GetBlobs()) { BlobClient blbClient = containerClient.GetBlobClient(item.Name); BlobDownloadInfo download = blbClient.Download(); string downloadFilePath = Path.Combine("../downloadeddata/", item.Name); using (FileStream downloadFileStream = File.OpenWrite(downloadFilePath)) { download.Content.CopyToAsync(downloadFileStream); downloadFileStream.Close(); } } Console.ReadLine(); } catch (Exception exception) { Console.WriteLine(exception.Message); } }
public void DownloadFile() { string connectionString = "DefaultEndpointsProtocol=https;AccountName=polichecksvc;AccountKey=+qitaIPghvVZQIm410FXCmeRJaHet9Ykj9G343ZSGbeyvaMpFCkEKZSwqiJwDMz/u0gpyihfzPmpqf7k54F9dQ==;EndpointSuffix=core.windows.net"; string containerName = "blobtest"; BlobContainerClient container = new BlobContainerClient(connectionString, containerName); BlobClient blob = container.GetBlobClient("hello.docx"); BlobDownloadInfo download = blob.Download(); using (FileStream file = File.OpenWrite(@"C:\Users\v-qiazhe\Desktop\ttt.docx")) { download.Content.CopyTo(file); } }
public BlobDownloadInfo ShowImage(string file) { BlobServiceClient blobServiceClient = new BlobServiceClient(_connStr.Value.Storage); BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("images-staging"); BlobClient blobClient = containerClient.GetBlobClient(file); if (!blobClient.Exists()) { throw new FileNotFoundException("Blob not found", file); } return(blobClient.Download()); }
/// <summary> /// Reads the signing certificate from a location. /// Supported locations: Blob storage, File, Local, KeyVault. /// </summary> /// <param name="certificate"></param> /// <param name="location"></param> /// <param name="log"></param> /// <returns>x509 certificate without private key</returns> private static X509Certificate ReadCertificate(string certificate, Location location, ILogger log) { log.LogInformation($"Reading certificate from {location}..."); X509Certificate x509 = null; try { if (location == Location.File) { x509 = DotNetUtilities.FromX509Certificate( System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromCertFile(certificate)); } if (location == Location.Local) { using (TextReader reader = new StringReader(certificate)) { Org.BouncyCastle.OpenSsl.PemReader pemReader = new Org.BouncyCastle.OpenSsl.PemReader(reader); x509 = (X509Certificate)pemReader.ReadObject(); }; } if (location == Location.Blob) { blobClient = new BlobClient(blobConnectionString, blobContainerName, certificate); BlobDownloadInfo download = blobClient.Download(); using (TextReader reader = new StreamReader(download.Content)) { Org.BouncyCastle.OpenSsl.PemReader pemReader = new Org.BouncyCastle.OpenSsl.PemReader(reader); x509 = (X509Certificate)pemReader.ReadObject(); }; } if (location == Location.KeyVault) { var client = new CertificateClient(new Uri($"https://{keyVaultName}.vault.azure.net"), new DefaultAzureCredential()); keyVaultCertificate = client.GetCertificate(certificate); x509 = new Org.BouncyCastle.X509.X509CertificateParser().ReadCertificate(keyVaultCertificate.Cer); } } catch (Exception ex) { log.LogError(ex.Message); } log.LogInformation($"Finished reading certificate {x509.SubjectDN} from {location}."); return(x509); }
/// <summary> /// Downloads the next block. /// <returns>Number of bytes that were downloaded</returns> /// </summary> private async Task DownloadBlock(bool async, CancellationToken cancellationToken) { Response <BlobDownloadInfo> response; HttpRange range = new HttpRange(_offset, _blockSize); response = async ? await _blobClient.DownloadAsync(range, cancellationToken : cancellationToken).ConfigureAwait(false) : _blobClient.Download(range); _stream = response.Value.Content; _offset += response.Value.ContentLength; _lastDownloadBytes = response.Value.ContentLength; _blobLength = GetBlobLength(response); }
private void ProcessBlobEmails(List <Models.EmailMetrics> emailMetrics, BlobClient blobClient) { BlobDownloadInfo emailBlob = blobClient.Download(); using (var reader = new StreamReader(emailBlob.Content)) { string line; while ((line = reader.ReadLine()) != null) { JsonDocument jsonObj = JsonDocument.Parse(line); // extract and count up recipients var totalRecipients = 0; try { totalRecipients += jsonObj.RootElement.GetProperty("toRecipients").GetArrayLength(); } catch { } try { totalRecipients += jsonObj.RootElement.GetProperty("ccRecipients").GetArrayLength(); } catch { } try { totalRecipients += jsonObj.RootElement.GetProperty("bccRecipients").GetArrayLength(); } catch { } var emailMetric = new Models.EmailMetrics(); try { emailMetric.Email = jsonObj.RootElement.GetProperty("sender").GetProperty("emailAddress").GetProperty("address").GetString(); } catch { } emailMetric.RecipientsToEmail = totalRecipients; // if already have this sender... var existingMetric = emailMetrics.FindLast(metric => metric.Email == emailMetric.Email); if (existingMetric != null) { existingMetric.RecipientsToEmail += emailMetric.RecipientsToEmail; } else { emailMetrics.Add(emailMetric); } } } }
static string ReadFromStorageBlob(string fname) { string conString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING"); BlobServiceClient blobServiceClient = new BlobServiceClient(conString); string containerName = Environment.GetEnvironmentVariable("AzStorageContainerName"); BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName); BlobClient blobClient = containerClient.GetBlobClient(fname); // Download the blob content. BlobDownloadInfo download = blobClient.Download(); StreamReader reader = new StreamReader(download.Content); logger.LogInformation($"ReadFromStorageBlob: Read source index data from Az Storage: {fname}"); return(reader.ReadToEnd()); }
public async Task StoreDataTestAsync() { string fileName = "storageTest.txt"; string expected = "StoreDataTest text for testing"; string actual = ""; IStorageService storageService = new BlobStorageService(_connectionString, _testContainerName); await storageService.StoreDataAsync(expected, fileName); BlobClient blobClient = _blobContainerClient.GetBlobClient(fileName); BlobDownloadInfo download = blobClient.Download(); using (StreamReader sr = new StreamReader(download.Content)) { actual = sr.ReadToEnd(); } Assert.Equal(expected, actual); }
public async void DownloadFile(BlobClient blobClient) { // Download the blob to a local file // Append the string "DOWNLOAD" before the .txt extension so you can see both files in MyDocuments //var localFilePath = @"D:\NewDownloads\downloadedfromazure\downloadedfile_" + Guid.NewGuid() + "_.pdf"; var localFilePath = @"D:\NewDownloads\downloadedfromazure"; Console.WriteLine("\nDownloading blob to\n\t{0}\n", localFilePath); // Download the blob's contents and save it to a file BlobDownloadInfo download = blobClient.Download(); FileStream downloadFileStream = File.OpenWrite(localFilePath); await download.Content.CopyToAsync(downloadFileStream); downloadFileStream.Close(); }
public async Task IndexContentFiles() { BlobContainerClient container = new BlobContainerClient(config.ConnectionString, config.Container); await container.CreateIfNotExistsAsync(); await foreach (BlobItem blobItem in container.GetBlobsAsync()) { if (blobItem.Name.EndsWith("index.md")) { logger.LogInformation($"Indexing {blobItem.Name}"); var blobClient = new BlobClient(config.ConnectionString, config.Container, blobItem.Name); var reader = new StreamReader(blobClient.Download().Value.Content); var post = ReadPost(reader).Result; Posts.Add(post); } } Posts = Posts.OrderByDescending(x => x.PublicationDate).ToList(); }