forked from rhysgodfrey/MSBuildAzure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CopyToAzureBlobStorageTask.cs
133 lines (110 loc) · 3.5 KB
/
CopyToAzureBlobStorageTask.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.IO;
namespace RhysG.MSBuild.Azure
{
public class CopyToAzureBlobStorageTask : ITask
{
private IBuildEngine _buildEngine;
private ITaskHost _taskHost;
public IBuildEngine BuildEngine
{
get
{
return _buildEngine;
}
set
{
_buildEngine = value;
}
}
[Required]
public string ContainerName
{
get;
set;
}
[Required]
public string ConnectionString
{
get;
set;
}
[Required]
public string ContentType
{
get;
set;
}
public string ContentEncoding
{
get;
set;
}
[Required]
public ITaskItem[] Files
{
get;
set;
}
public bool Execute()
{
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
{
// Provide the configSetter with the initial value
configSetter(ConnectionString);
});
CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("ConnectionSetting");
CloudBlobClient client = account.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference(ContainerName);
container.CreateIfNotExist();
container.SetPermissions(new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Container });
foreach (ITaskItem fileItem in Files)
{
FileInfo file = new FileInfo(fileItem.ItemSpec);
CloudBlob blob = container.GetBlobReference(file.Name);
try
{
blob.FetchAttributes();
}
catch (StorageClientException) { }
DateTime lastModified = DateTime.MinValue;
if (!String.IsNullOrWhiteSpace(blob.Metadata["LastModified"]))
{
long timeTicks = long.Parse(blob.Metadata["LastModified"]);
lastModified = new DateTime(timeTicks, DateTimeKind.Utc);
}
if (lastModified != file.LastWriteTimeUtc)
{
blob.UploadFile(file.FullName);
blob.Properties.ContentType = ContentType;
if (!String.IsNullOrWhiteSpace(ContentEncoding))
{
blob.Properties.ContentEncoding = ContentEncoding;
}
blob.Metadata["LastModified"] = file.LastWriteTimeUtc.Ticks.ToString();
blob.SetMetadata();
blob.SetProperties();
BuildEngine.LogMessageEvent(new BuildMessageEventArgs(String.Format("Updating: {0}", file.Name), String.Empty, "CopyToAzureBlobStorageTask", MessageImportance.Normal));
}
}
return true;
}
public ITaskHost HostObject
{
get
{
return _taskHost;
}
set
{
_taskHost = value;
}
}
}
}