-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
287 lines (249 loc) · 13.4 KB
/
Program.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using System;
using System.IO;
using System.Security.Permissions;
using System.Threading.Tasks;
namespace blob_quickstart
{
class Program
{
// VALORES DE CONFIGURACION //
private const string contenedorNombre = "textosprueba1";
private const string ruta = "C://Seguimiento";
private const string extension = "*.TXT";
public const string cliente = "JEALSA";
public static void Main()
{
// Ajuste la cantidad de guiones (-) para que coincidan.
Console.WriteLine("-----------------------------------" + lineas() + " \n");
Console.WriteLine("--\\ Importer3a - " + cliente + " -- \n");
Console.WriteLine("-----------------------------------" + lineas() + " \n");
Console.WriteLine("\n");
Run();
//Console.WriteLine("Press any key to exit the sample application.");
//Console.ReadLine();
}
/* --------------------------------------------------------------------------------------- */
// Subirá todos los archivos de la carpeta.
private static async Task ProcessAsync()
{
// Variable de Entorno para la conexión con el servicio
Console.WriteLine("Obteniendo Variable de Entorno...");
string storageConnectionString = Environment.GetEnvironmentVariable("STORAGE_CONNECTION_STRING");
Console.WriteLine(" [OK]\n\n");
Console.WriteLine("Realizando conexión...");
// Check whether the connection string can be parsed.
CloudStorageAccount storageAccount;
if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
{
Console.WriteLine("[OK]\n\n");
// If the connection string is valid, proceed with operations against Blob
// storage here.
// Create the CloudBlobClient that represents the
// Blob storage endpoint for the storage account.
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
// Create a container called 'quickstartblobs' and
// append a GUID value to it to make the name unique.
CloudBlobContainer cloudBlobContainer =
cloudBlobClient.GetContainerReference(contenedorNombre); // Nombre del contenedor
// SI NO EXISTE EL CONTENEDOR LO CREA, SI EXISTE MODIFICA SU CONTENIDO
await cloudBlobContainer.CreateIfNotExistsAsync();
// Set the permissions so the blobs are public.
BlobContainerPermissions permissions = new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
};
await cloudBlobContainer.SetPermissionsAsync(permissions);
// AQUI SE CAMBIA EL DIRECTORIO DONDE SE ENCUENTRA LA CARPETA QUE SE QUIERE SUBIR SU CONTENIDO Y EL TIPO DE ARCHIVO.
string[] files = Directory.GetFiles(ruta, extension);
Console.WriteLine("Subiendo archivos de " + ruta + "\n\n");
foreach (string filePath in files)
{
string fileName = Path.GetFileName(filePath);
Console.WriteLine("Archivo = {0}", filePath);
Console.WriteLine();
Console.WriteLine("Subiendo a Blob storage como blob '{0}'...", fileName);
Console.WriteLine();
// Get a reference to the blob address, then upload the file to the blob.
// Use the value of localFileName for the blob name.
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(fileName);
await cloudBlockBlob.UploadFromFileAsync(filePath);
}
textoInicio();
}
else
{
Console.WriteLine("[ERROR]\n\n");
// Otherwise, let the user know that they need to define the environment variable.
Console.WriteLine(
"La conexión falló.\n " +
"Puede ser por las siguiente opciones:\n" +
" - Variable de Entorno no definida. \n" +
" - Variable de Entorno incorrecta o caduca. \n\n" +
"En caso de la primera opción, Añada una Variable de Entorno de Sistema llamada 'STORAGE_CONNECTION_STRING'\n" +
" y como valor añada la key Connection String. Ejemplo: 'DefaultEndpointsProtocol=https;AccountName=triplealpha;AccountKey=fQsntF2...'\n\n");
Console.WriteLine("Pulse cualquier tecla para salir de la aplicación.");
Console.ReadLine();
}
}
/* --------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------- */
// Subirá solo los modificados.
private static async Task ProcessAsync(string fileOld, string fileNew)
{
// Variable de Entorno para la conexión con el servicio
Console.WriteLine("Obteniendo Variable de Entorno...");
string storageConnectionString = Environment.GetEnvironmentVariable("STORAGE_CONNECTION_STRING");
Console.WriteLine(" [OK]\n\n");
Console.WriteLine("Realizando conexión...");
// Check whether the connection string can be parsed.
CloudStorageAccount storageAccount;
if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
{
// If the connection string is valid, proceed with operations against Blob
// storage here.
// ADD OTHER OPERATIONS HERE
// Create the CloudBlobClient that represents the
// Blob storage endpoint for the storage account.
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
// Create a container called 'quickstartblobs' and
// append a GUID value to it to make the name unique.
CloudBlobContainer cloudBlobContainer =
cloudBlobClient.GetContainerReference(contenedorNombre); // Nombre del contenedor
// Guid.NewGuid().ToString());
// await cloudBlobContainer.CreateAsync();
// SI NO EXISTE EL CONTENEDOR LO CREA, SI EXISTE MODIFICA SU CONTENIDO
await cloudBlobContainer.CreateIfNotExistsAsync();
// Set the permissions so the blobs are public.
BlobContainerPermissions permissions = new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
};
await cloudBlobContainer.SetPermissionsAsync(permissions);
// Create a file in your local MyDocuments folder to upload to a blob.
/*
string localPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string localFileName = "QuickStart_" + Guid.NewGuid().ToString() + ".txt";
string sourceFile = Path.Combine(localPath, localFileName);
*/
// Write text to the file.
// File.WriteAllText(sourceFile, "Hello, World!");
// AQUI SE CAMBIA EL DIRECTORIO DONDE SE ENCUENTRA LA CARPETA QUE SE QUIERE SUBIR SU CONTENIDO Y EL TIPO DE ARCHIVO.
// string[] files = Directory.GetFiles(ruta, extension);
if (fileOld.Equals(fileNew))
{
string sourceFile = Path.Combine(ruta, fileNew);
Console.WriteLine("Subiendo a Blob storage como blob '{0}'...", fileNew);
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(fileNew);
await cloudBlockBlob.UploadFromFileAsync(sourceFile);
Console.WriteLine("[OK]\n");
}
else
{
string sourceFile = Path.Combine(ruta, fileNew);
Console.WriteLine("sustituyendo " + fileOld + " como " + fileNew + " a Blob storage como blob...");
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(fileOld);
await cloudBlockBlob.DeleteIfExistsAsync();
cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(fileNew);
await cloudBlockBlob.UploadFromFileAsync(sourceFile);
Console.WriteLine("[OK]");
textoInicio();
}
}
else
{
Console.WriteLine("[ERROR]\n\n");
// Otherwise, let the user know that they need to define the environment variable.
Console.WriteLine(
"La conexión falló.\n " +
"Puede ser por las siguiente opciones:\n" +
" - Variable de Entorno no definida. \n" +
" - Variable de Entorno incorrecta o caduca. \n\n" +
"En caso de la primera opción, Añada una Variable de Entorno de Sistema llamada 'STORAGE_CONNECTION_STRING'\n" +
" y como valor añada la key Connection String. Ejemplo: 'DefaultEndpointsProtocol=https;AccountName=triplealpha;AccountKey=fQsntF2...'\n\n");
Console.WriteLine("Pulse cualquier tecla para salir de la aplicación.");
Console.ReadLine();
}
}
/* --------------------------------------------------------------------------------------- */
// SE ENCARGA DE NOTIFICAR CUALQUIER CAMBIO EN LA CARPETA DE DESTINO
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
private static void Run()
{
// Create a new FileSystemWatcher and set its properties.
using (FileSystemWatcher watcher = new FileSystemWatcher())
{
//watcher.Path = args[1];
watcher.Path = ruta;
// Watch for changes in LastAccess and LastWrite times, and
// the renaming of files or directories.
watcher.NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;
// Only watch text files.
// SOLO ESCUCHA ESTE TIPO DE ARCHIVO, EN CASO DE QUERER TODOS LOS ARCHIVOS, DEJAR VACIO ESTE CAMPO.
watcher.Filter = extension;
// Add event handlers.
watcher.Changed += OnChanged;
watcher.Created += OnCreated;
watcher.Deleted += OnDeleted;
watcher.Renamed += OnRenamed;
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("----------------------------------------");
Console.WriteLine("- Opciones |'1' Subir Todo | 'q' Salir -");
Console.WriteLine("----------------------------------------");
if (Console.Read() == '1')
{
ProcessAsync().GetAwaiter().GetResult(); ;
}
while (Console.Read() != 'q') ;
}
}
private static void textoInicio()
{
Console.WriteLine("----------------------------------------");
Console.WriteLine("- Opciones |'1' Subir Todo | 'q' Salir -");
Console.WriteLine("----------------------------------------");
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e) //=>
{
// Specify what is done when a file is changed, created, or deleted.
ProcessAsync(e.Name, e.Name).GetAwaiter().GetResult();
Console.WriteLine($"Archivo: {e.FullPath} {e.ChangeType} \n\n");
textoInicio();
}
private static void OnCreated(object source, FileSystemEventArgs e) //=>
{
// Specify what is done when a file is changed, created, or deleted.
ProcessAsync(e.Name, e.Name).GetAwaiter().GetResult();
Console.WriteLine($"Archivo: {e.FullPath} {e.ChangeType} \n\n");
textoInicio();
}
private static void OnDeleted(object source, FileSystemEventArgs e) //=>
{
Console.WriteLine($"Archivo: {e.Name} {e.ChangeType} \n\n");
textoInicio();
}
private static void OnRenamed(object source, RenamedEventArgs e) //=>
{
// Specify what is done when a file is renamed.
ProcessAsync(e.OldName, e.Name).GetAwaiter().GetResult();
Console.WriteLine($"Archivo: {e.OldFullPath} renombrado a {e.FullPath} \n\n");
textoInicio();
}
private static string lineas()
{
string lineas = "";
for (int i = 0; i < cliente.Length; i++)
{
lineas += "-";
}
return lineas;
}
}
}