-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
69 lines (60 loc) · 2.57 KB
/
Program.cs
File metadata and controls
69 lines (60 loc) · 2.57 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CleanupTask
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start to execute custom activity.");
try
{
ExcuteCustomActivityAsync().Wait();
}
catch (Exception ex)
{
ex = ex.InnerException ?? ex;
Console.Error.WriteLine($"Custom activity execution failed: {ex.Message}");
Console.Error.WriteLine($"StackTrace {ex.StackTrace}");
throw;
}
Console.WriteLine("Execute custom activity complete.");
}
static async Task ExcuteCustomActivityAsync()
{
JObject activity = await CustomActivityHelper.ParseActivityFromInputFileAsync(true);
string connectionString = activity.GetProperty<string>(@"$..extendedProperties.connectionString");
string folderName = activity.GetProperty<string>(@"$..extendedProperties.folderPath");
string containerName = activity.GetProperty<string>(@"$..extendedProperties.container");
// Create blob client
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Delete all files in folder
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
CloudBlobDirectory folder = container.GetDirectoryReference(folderName);
bool result = false;
List<string> deleteFiles = new List<string>();
foreach (IListBlobItem blob in folder.ListBlobs(true))
{
if (blob.GetType() == typeof(CloudBlob) || blob.GetType().BaseType == typeof(CloudBlob))
{
string deletedBlobUri = blob.StorageUri.ToString();
result |= ((CloudBlob)blob).DeleteIfExists();
deleteFiles.Add(deletedBlobUri);
}
}
Console.WriteLine($"{containerName}:{folderName} delete result: {result}");
// Put result to output of custom activity
if (deleteFiles != null)
{
File.WriteAllText("outputs.json", JsonConvert.SerializeObject(new { deletedFile = deleteFiles }));
}
}
}
}