Repository navigation
Expand file tree
/
Copy pathBlobStorageClientHelper.cs
More file actions
194 lines (174 loc) · 8.1 KB
/
Copy pathBlobStorageClientHelper.cs
File metadata and controls
194 lines (174 loc) · 8.1 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
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
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------
namespace DurableTask.ServiceBus.Tracking
{
using System;
using System.Diagnostics;
using System.Globalization;
using System.Text.RegularExpressions;
using DurableTask.Core.Tracing;
using DurableTask.Core.Common;
/// <summary>
/// A helper class for the Azure blob storage client.
/// </summary>
public class BlobStorageClientHelper
{
static readonly string DateFormat = "yyyyMMdd";
static readonly char ContainerNameDelimiter = '-';
/// <summary>
/// the blob storage accesss key is in the format of {DateTime}|{blobName}
/// </summary>
public static readonly char KeyDelimiter = '|';
/// <summary>
/// the delimiter shown in the blob name as the file path
/// </summary>
public static readonly char BlobNameDelimiter = '/';
/// <summary>
/// Build a blob key for the message.
/// </summary>
/// <param name="instanceId">The orchestration instance Id</param>
/// <param name="executionId">The orchestration execution Id</param>
/// <param name="messageFireTime">The message fire time. If it is DateTime.MinValue, use current date.</param>
/// <returns>The constructed blob key for message</returns>
public static string BuildMessageBlobKey(string instanceId, string executionId, DateTime messageFireTime)
{
string id = Guid.NewGuid().ToString("N");
return string.Format(
"{0}{1}{2}{3}{4}{3}{5}",
BuildContainerNameSuffix("message", messageFireTime),
KeyDelimiter,
instanceId,
BlobNameDelimiter,
executionId,
id);
}
static string BuildContainerNameSuffix(string containerType, DateTime blobCreationTime)
{
return $"{containerType.ToLower()}{ContainerNameDelimiter}{GetDateStringForContainerName(blobCreationTime)}";
}
/// <summary>
/// Build the container name prefix using the lower case hub name.
/// It is in the format of {hubName}-dtfx.
/// The container name prefix is not part of the generated blob key.
/// </summary>
/// <param name="hubName">The hub name. Converted to lower case to build the prefix.</param>
/// <returns>The container name prefix</returns>
public static string BuildContainerNamePrefix(string hubName)
{
return $"{hubName.ToLower()}{ContainerNameDelimiter}dtfx";
}
/// <summary>
/// Build a blob key for the session.
/// </summary>
/// <param name="sessionId">The session Id</param>
/// <returns>The constructed blob key for session</returns>
public static string BuildSessionBlobKey(string sessionId)
{
string id = Guid.NewGuid().ToString("N");
return string.Format(
"{0}{1}{2}{3}{4}",
BuildContainerNameSuffix("session", DateTimeUtils.MinDateTime),
KeyDelimiter,
sessionId,
BlobNameDelimiter,
id);
}
// use the message fire time if it is set;
// otherwise, use the current utc time as the date string as part of the container name
static string GetDateStringForContainerName(DateTime messageFireTime)
{
return messageFireTime.IsSet() ?
messageFireTime.ToString(DateFormat) :
DateTime.UtcNow.ToString(DateFormat);
}
/// <summary>
/// Parse the key for the container name suffix and the blob name.
/// </summary>
/// <param name="key">The input blob key</param>
/// <param name="containerNameSuffix">The parsed container name suffix as output</param>
/// <param name="blobName">The parsed blob name as output</param>
public static void ParseKey(string key, out string containerNameSuffix, out string blobName)
{
string[] segments = key.Split(new[] {BlobStorageClientHelper.KeyDelimiter}, 2);
if (segments.Length < 2)
{
throw new ArgumentException($"Blob key {key} does not contain required 2 or more segments: containerNameSuffix|blobName.", nameof(key));
}
containerNameSuffix = segments[0];
if (!IsValidContainerNameSuffix(containerNameSuffix))
{
throw new ArgumentException(
$"Not a valid container name suffix: {containerNameSuffix}. " +
"Container name suffix can contain only lower case letters, numbers, and the dash (-) character.",
nameof(containerNameSuffix));
}
blobName = segments[1];
}
/// <summary>
/// Validate the container name suffix.
/// Container name suffix can contain only lower case letters, numbers, and the dash (-) character.
/// </summary>
/// <param name="containerNameSuffix"></param>
/// <returns>True if the container name suffix is valid.</returns>
static bool IsValidContainerNameSuffix(string containerNameSuffix)
{
Regex regex = new Regex(@"^[a-z0-9\\-]+$");
return regex.Match(containerNameSuffix).Success;
}
/// <summary>
/// Check if the container is expired.
/// </summary>
/// <param name="containerName">The container name</param>
/// <param name="thresholdDateTimeUtc">The specified date threshold</param>
/// <returns></returns>
public static bool IsContainerExpired(string containerName, DateTime thresholdDateTimeUtc)
{
string[] segments = containerName.Split(ContainerNameDelimiter);
if (segments.Length != 4)
{
TraceHelper.Trace(
TraceEventType.Warning,
"BlobStorageClientHelper-IsContainerExpired-ContainerIgnored",
$"Container name {containerName} does not contain required 4 segments. Container {containerName} is ignored.");
return false;
}
DateTime containerDateTime;
string dateString = segments[segments.Length - 1];
bool parseSucceeded = DateTime.TryParseExact(
dateString,
DateFormat,
System.Globalization.CultureInfo.InvariantCulture,
DateTimeStyles.None,
out containerDateTime);
if (!parseSucceeded)
{
TraceHelper.Trace(
TraceEventType.Warning,
"BlobStorageClientHelper-IsContainerExpired-InvalidDate",
$"Cannot parse the the date string {dateString} in the format of yyyyMMdd. Container {containerName} is ignored.");
return false;
}
return containerDateTime < thresholdDateTimeUtc;
}
/// <summary>
/// Build a container name using prefix and suffix.
/// </summary>
/// <param name="prefix">The container name prefix</param>
/// <param name="suffix">The container name suffix</param>
/// <returns>The container name</returns>
public static string BuildContainerName(string prefix, string suffix)
{
return $"{prefix}{ContainerNameDelimiter}{suffix}";
}
}
}