-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSqlServerToAzureBlobStorage.cs
167 lines (139 loc) · 6.12 KB
/
SqlServerToAzureBlobStorage.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using CsvHelper;
using Microsoft.WindowsAzure.Storage;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace FileStreamToAzureStorageMigrator
{
/// <summary>
/// Gets all files stream contents and metadata from SQL Server database and saves it to Azure Blob Storage and local CSV (metadata)
/// </summary>
public class SqlServerToAzureBlobStorage
{
/// <summary>
/// Source SQL Server database connection string
/// </summary>
private readonly string _sourceSqlServerDatabaseConnectionString;
/// <summary>
/// Destination Azure (Blob) Storage
/// </summary>
private readonly string _destinationAzureBlobStorageConnectionString;
/// <summary>
/// The CSV filename containing the metadata of the SQL FileStream files
/// </summary>
private readonly string _filesStreamInfoCsvFile;
/// <summary>
/// SQL Query that returns the file stream (actual content) from SQL Server
/// You can also adjust it to your needs
/// </summary>
internal readonly string _selectDocumentsCommand = $@"SELECT [file_stream] FROM [DocumentsTable] WHERE [documentId] = @Id";
/// <summary>
/// SQL Query that returns a list of all files to be fectched from SQL Server
/// You can also adjust it to your needs
/// </summary>
internal readonly string _selectFileDocumentsCommand = $@"SELECT DocumentName, documentId, contentType, file_type FROM [DocumentsTable]";
public SqlServerToAzureBlobStorage(
string sourceSqlServerDatabaseConnectionString,
string destinationAzureBlobStorageConnectionString,
string filesStreamInfoCsvFile)
{
_sourceSqlServerDatabaseConnectionString = sourceSqlServerDatabaseConnectionString;
_destinationAzureBlobStorageConnectionString = destinationAzureBlobStorageConnectionString;
_filesStreamInfoCsvFile = filesStreamInfoCsvFile;
}
public async Task CopyDataAsync()
{
var fileStreamFiles = new List<FileStreamFile>();
using (var conn = new SqlConnection(_sourceSqlServerDatabaseConnectionString))
{
// List all files to be downloaded
SqlCommand cmd = new SqlCommand(_selectFileDocumentsCommand, conn);
await conn.OpenAsync();
using (var reader = await cmd.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
// You'll need to adjust the nr. of fields here
var fsf = new FileStreamFile(
reader.GetValue(0).ToString(),
reader.GetValue(1).ToString(),
reader.GetValue(2).ToString(),
reader.GetValue(3).ToString(),
reader.GetValue(4).ToString(),
reader.GetValue(5).ToString(),
reader.GetValue(6).ToString());
fileStreamFiles.Add(fsf);
}
}
// Foreach file, download content and copy to azure storage
cmd.CommandText = _selectDocumentsCommand;
cmd.Parameters.Add(new SqlParameter("@Id", string.Empty));
foreach (var fsf in fileStreamFiles)
{
cmd.Parameters[0].Value = fsf.StreamId;
using (var reader = await cmd.ExecuteReaderAsync())
{
await reader.ReadAsync();
var fileBytes = (byte[])reader[0];
await CopyFileStreamFile(fsf, fileBytes);
}
}
ExportToCsvFile(fileStreamFiles);
}
}
/// <summary>
/// Copy file bytes to Azure Blob
/// </summary>
/// <param name="fsf"></param>
/// <param name="fileBytes"></param>
/// <returns></returns>
private async Task CopyFileStreamFile(FileStreamFile fsf, byte[] fileBytes)
{
try
{
var stopwatch = Stopwatch.StartNew();
var cloudStorageAccount = CloudStorageAccount.Parse(_destinationAzureBlobStorageConnectionString);
var blobClient = cloudStorageAccount.CreateCloudBlobClient();
// Ensure container exits
var container = blobClient.GetContainerReference(fsf.Container);
await container.CreateIfNotExistsAsync();
// open stream writer to file
var blobFileName = Path.Combine(fsf.Folder, fsf.Name);
var blob = container.GetBlockBlobReference(blobFileName);
if (await blob.ExistsAsync())
{
return;
}
await blob.UploadFromByteArrayAsync(fileBytes, 0, fileBytes.Length);
fsf.BlobLocation = blob.StorageUri.PrimaryUri.AbsolutePath;
stopwatch.Stop();
Console.WriteLine($"File {fsf.Name} saved to container {fsf.Folder}, {fileBytes.Length} bytes, in {stopwatch.ElapsedMilliseconds}ms");
}
catch (Exception ex)
{
Console.WriteLine($"Error with {fsf.Name}: {ex}");
using (var writer = new StreamWriter($"{fsf.DocumentId}.csv"))
{
var csv = new CsvWriter(writer);
csv.WriteRecord(fsf);
}
}
}
/// <summary>
/// Save files stream info to CSV
/// </summary>
/// <param name="fsf"></param>
private void ExportToCsvFile(IList<FileStreamFile> fsf)
{
using (var writer = new StreamWriter($@"{_filesStreamInfoCsvFile}"))
{
var csv = new CsvWriter(writer);
csv.WriteRecords(fsf);
}
}
}
}