-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlobRetriever.cs
110 lines (96 loc) · 4.38 KB
/
BlobRetriever.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.Storage.Auth;
namespace Microsoft.Azure.Documents.Tools.POC.BlobToCosmosDB
{
internal sealed class BlobRetriever
{
/// <summary>
/// Local folder into which CSV files will be download from Blob Storage
/// </summary>
private static string CSVFolder = Path.Combine(Directory.GetCurrentDirectory(), "DownloadFromBlob");
/// <summary>
/// CloudBlobContainer object to access individual containers within the Blob Storage account
/// </summary>
private CloudBlobContainer BlobContainer;
/// <summary>
/// Connection string for the Blob Storage account
/// </summary>
private string StorageAccountConnectionString;
/// <summary>
/// Initializes a new BlobRetriever instance
/// </summary>
/// <param name="StorageAccountConnectionString"> Connection string to access the CSV files from Blob Storage </param>
public BlobRetriever(string StorageAccountConnectionString)
{
string BlobStorageContainerName = ConfigurationManager.AppSettings["StorageAccountContainerName"];
this.StorageAccountConnectionString = StorageAccountConnectionString;
CloudStorageAccount StorageAccount =
CloudStorageAccount.Parse(this.StorageAccountConnectionString);
CloudBlobClient BlobClient = StorageAccount.CreateCloudBlobClient();
BlobContainer = BlobClient.GetContainerReference(BlobStorageContainerName);
}
/// <summary>
/// Downloads the specified file name (blob name) from the Azure Storage Account into a local folder
/// </summary>
///
/// <param name="fileName"> List of file names to download from Cosmos DB </param>
public void DownloadCsvFromBlobStorage(string FileName)
{
if (!bool.Parse(ConfigurationManager.AppSettings["FilesAreGZipped"]))
{
FileName = FileName.Replace(".gzip", ".csv");
}
Console.WriteLine("fileName = {0}", FileName);
CloudBlockBlob BlockBlob = this.BlobContainer.GetBlockBlobReference(FileName);
BlockBlob.DownloadToFile(Path.GetFullPath(Path.Combine(CSVFolder, FileName)), FileMode.OpenOrCreate);
if (bool.Parse(ConfigurationManager.AppSettings["FilesAreGZipped"]))
{
FileName = Path.GetFullPath(Path.Combine(CSVFolder, FileName));
string newFilePath = FileName.Replace(".gzip", ".csv");
using (FileStream decompressedFileStream = File.Create(newFilePath))
{
using (GZipStream decompressionStream = new GZipStream(File.OpenRead(FileName), CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedFileStream);
}
}
}
}
/// <summary>
/// For a list of file names (blobs), this method iterates through all corresponding Blobs within the container
/// and downloads them to a local folder.
///
/// </summary>
/// <param name="FileNames"> List of file names in Blob Storage to download and ingest into Cosmos DB </param>
public void DownloadAllCsvFilesFromBlobStorage(List<string> FileNames)
{
string BlobStorageContainerName = ConfigurationManager.AppSettings["StorageAccountContainerName"];
if(FileNames.Count == 0)
{
foreach (CloudBlockBlob EachBlob in this.BlobContainer.ListBlobs())
{
DownloadCsvFromBlobStorage(EachBlob.Name);
Console.WriteLine("Successfully downloaded Source file : {0}", EachBlob.Name);
}
}
else
{
foreach (string FileName in FileNames)
{
DownloadCsvFromBlobStorage(FileName);
Console.WriteLine("Successfully downloaded Source file : {0}", FileName);
}
}
}
}
}