Class AWSSDKUtils
This class defines utilities and constants that can be used by all the client libraries of the SDK.
public static class AWSSDKUtils
- Inheritance
-
AWSSDKUtils
- Inherited Members
Fields
DefaultBufferSize
public const int DefaultBufferSize = 8192
Field Value
DefaultProgressUpdateInterval
public const long DefaultProgressUpdateInterval = 102400
Field Value
EPOCH_START
public static readonly DateTime EPOCH_START
Field Value
GMTDateFormat
The GMT Date Format string. Used when parsing date objects
public const string GMTDateFormat = "ddd, dd MMM yyyy HH:mm:ss \\G\\M\\T"
Field Value
ISO8601BasicDateFormat
The ISO8601 basic date format. Used during AWS4 signature computation.
public const string ISO8601BasicDateFormat = "yyyyMMdd"
Field Value
ISO8601BasicDateTimeFormat
The ISO8601 Basic date/time format string. Used when parsing date objects
public const string ISO8601BasicDateTimeFormat = "yyyyMMddTHHmmssZ"
Field Value
ISO8601DateFormat
The ISO8601Date Format string. Used when parsing date objects
public const string ISO8601DateFormat = "yyyy-MM-dd\\THH:mm:ss.fff\\Z"
Field Value
ISO8601DateFormatNoMS
The ISO8601Date Format string. Used when parsing date objects
public const string ISO8601DateFormatNoMS = "yyyy-MM-dd\\THH:mm:ss\\Z"
Field Value
RFC822DateFormat
The RFC822Date Format string. Used when parsing date objects
public const string RFC822DateFormat = "ddd, dd MMM yyyy HH:mm:ss \\G\\M\\T"
Field Value
UrlEncodedContent
The string representing Url Encoded Content in HTTP requests
public const string UrlEncodedContent = "application/x-www-form-urlencoded; charset=utf-8"
Field Value
UserAgentHeader
The user agent string header
public const string UserAgentHeader = "User-Agent"
Field Value
ValidTraceIdHeaderValueCharacters
The set of characters which are not to be encoded as part of the X-Amzn-Trace-Id header values
public const string ValidTraceIdHeaderValueCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=;:+&[]{}\"',"
Field Value
ValidUrlCharacters
The Set of accepted and valid Url characters per RFC3986. Characters outside of this set will be encoded.
public const string ValidUrlCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"
Field Value
ValidUrlCharactersRFC1738
The Set of accepted and valid Url characters per RFC1738. Characters outside of this set will be encoded.
public const string ValidUrlCharactersRFC1738 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_."
Field Value
Properties
CorrectedUtcNow
Returns DateTime.UtcNow + ManualClockCorrection when ManualClockCorrection is set. This value should be used instead of DateTime.UtcNow to factor in manual clock correction
[Obsolete("This property does not account for endpoint specific clock skew. Use CorrectClockSkew.GetCorrectedUtcNowForEndpoint() instead.")]
public static DateTime CorrectedUtcNow { get; }
Property Value
FormattedCurrentTimestampGMT
Formats the current date as a GMT timestamp
public static string FormattedCurrentTimestampGMT { get; }
Property Value
- string
A GMT formatted string representation of the current date and time
FormattedCurrentTimestampISO8601
Formats the current date as ISO 8601 timestamp
public static string FormattedCurrentTimestampISO8601 { get; }
Property Value
- string
An ISO 8601 formatted string representation of the current date and time
FormattedCurrentTimestampRFC822
Formats the current date as ISO 8601 timestamp
public static string FormattedCurrentTimestampRFC822 { get; }
Property Value
- string
An ISO 8601 formatted string representation of the current date and time
Methods
BytesToHexString(byte[])
Convert bytes to a hex string
public static string BytesToHexString(byte[] value)
Parameters
value
byte[]Bytes to convert.
Returns
- string
Hexadecimal string representing the byte array.
CanonicalizeResourcePath(Uri, string)
Returns the canonicalized resource path for the service endpoint with single URL encoded path segments.
[Obsolete("Use CanonicalizeResourcePathV2 instead")]
public static string CanonicalizeResourcePath(Uri endpoint, string resourcePath)
Parameters
endpoint
UriEndpoint URL for the request
resourcePath
stringResource path for the request
Returns
- string
Canonicalized resource path for the endpoint
Remarks
If resourcePath begins or ends with slash, the resulting canonicalized path will follow suit.
CanonicalizeResourcePath(Uri, string, bool)
Returns the canonicalized resource path for the service endpoint
[Obsolete("Use CanonicalizeResourcePathV2 instead")]
public static string CanonicalizeResourcePath(Uri endpoint, string resourcePath, bool detectPreEncode)
Parameters
endpoint
UriEndpoint URL for the request
resourcePath
stringResource path for the request
detectPreEncode
boolIf true pre URL encode path segments if necessary. S3 is currently the only service that does not expect pre URL encoded segments.
Returns
- string
Canonicalized resource path for the endpoint
Remarks
If resourcePath begins or ends with slash, the resulting canonicalized path will follow suit.
CanonicalizeResourcePath(Uri, string, bool, IDictionary<string, string>, int)
Returns the canonicalized resource path for the service endpoint
[Obsolete("Use CanonicalizeResourcePathV2 instead")]
public static string CanonicalizeResourcePath(Uri endpoint, string resourcePath, bool detectPreEncode, IDictionary<string, string> pathResources, int marshallerVersion)
Parameters
endpoint
UriEndpoint URL for the request
resourcePath
stringResource path for the request
detectPreEncode
boolIf true pre URL encode path segments if necessary. S3 is currently the only service that does not expect pre URL encoded segments.
pathResources
IDictionary<string, string>Dictionary of key/value parameters containing the values for the ResourcePath key replacements
marshallerVersion
intThe version of the marshaller that constructed the request object.
Returns
- string
Canonicalized resource path for the endpoint
Remarks
If resourcePath begins or ends with slash, the resulting canonicalized path will follow suit.
CanonicalizeResourcePathV2(Uri, string, bool, IDictionary<string, string>)
Returns the canonicalized resource path for the service endpoint.
public static string CanonicalizeResourcePathV2(Uri endpoint, string resourcePath, bool encode, IDictionary<string, string> pathResources)
Parameters
endpoint
UriEndpoint URL for the request.
resourcePath
stringResource path for the request.
encode
boolIf true will URL-encode path segments including "/". "S3" is currently the only service that does not expect pre URL-encoded segments.
pathResources
IDictionary<string, string>Dictionary of key/value parameters containing the values for the ResourcePath key replacements.
Returns
- string
Canonicalized resource path for the endpoint.
Remarks
If resourcePath begins or ends with slash, the resulting canonicalized path will follow suit.
CompressSpaces(string)
Utility method that accepts a string and replaces white spaces with a space.
public static string CompressSpaces(string data)
Parameters
data
string
Returns
ConvertDateTimetoMilliseconds(DateTime)
public static long ConvertDateTimetoMilliseconds(DateTime dateTime)
Parameters
dateTime
DateTime
Returns
ConvertFromUnixEpochMilliseconds(long)
Utility method for converting Unix epoch milliseconds to DateTime structure.
public static DateTime ConvertFromUnixEpochMilliseconds(long milliseconds)
Parameters
milliseconds
longThe number of milliseconds since January 1, 1970.
Returns
- DateTime
Converted DateTime structure
ConvertFromUnixEpochSeconds(int)
Utility method for converting Unix epoch seconds to DateTime structure.
public static DateTime ConvertFromUnixEpochSeconds(int seconds)
Parameters
seconds
intThe number of seconds since January 1, 1970.
Returns
- DateTime
Converted DateTime structure
ConvertTimeSpanToMilliseconds(TimeSpan)
public static long ConvertTimeSpanToMilliseconds(TimeSpan timeSpan)
Parameters
timeSpan
TimeSpan
Returns
ConvertToUnixEpochMilliSeconds(DateTime)
[Obsolete("This method isn't named correctly: it returns seconds instead of milliseconds. Use ConvertToUnixEpochSecondsDouble instead.", false)]
public static double ConvertToUnixEpochMilliSeconds(DateTime dateTime)
Parameters
dateTime
DateTime
Returns
ConvertToUnixEpochSeconds(DateTime)
public static int ConvertToUnixEpochSeconds(DateTime dateTime)
Parameters
dateTime
DateTime
Returns
ConvertToUnixEpochSecondsDouble(DateTime)
public static double ConvertToUnixEpochSecondsDouble(DateTime dateTime)
Parameters
dateTime
DateTime
Returns
ConvertToUnixEpochSecondsString(DateTime)
public static string ConvertToUnixEpochSecondsString(DateTime dateTime)
Parameters
dateTime
DateTime
Returns
CopyStream(Stream, Stream)
Utility method for copy the contents of the source stream to the destination stream.
public static void CopyStream(Stream source, Stream destination)
Parameters
CopyStream(Stream, Stream, int)
Utility method for copy the contents of the source stream to the destination stream.
public static void CopyStream(Stream source, Stream destination, int bufferSize)
Parameters
DetermineRegion(string)
Attempt to infer the region for a service request based on the endpoint
public static string DetermineRegion(string url)
Parameters
url
stringEndpoint to the service to be called
Returns
- string
Region parsed from the endpoint; DefaultRegion (or DefaultGovRegion) if it cannot be determined/is not explicit
DetermineService(string)
Attempt to infer the service name for a request (in short form, eg 'iam') from the service endpoint.
public static string DetermineService(string url)
Parameters
url
stringEndpoint to the service to be called
Returns
- string
Short-form name of the service parsed from the endpoint; empty string if it cannot be determined
DownloadStringContent(Uri)
public static string DownloadStringContent(Uri uri)
Parameters
uri
Uri
Returns
DownloadStringContent(Uri, IWebProxy)
public static string DownloadStringContent(Uri uri, IWebProxy proxy)
Parameters
uri
Uriproxy
IWebProxy
Returns
DownloadStringContent(Uri, TimeSpan)
public static string DownloadStringContent(Uri uri, TimeSpan timeout)
Parameters
uri
Uritimeout
TimeSpan
Returns
DownloadStringContent(Uri, TimeSpan, IWebProxy)
public static string DownloadStringContent(Uri uri, TimeSpan timeout, IWebProxy proxy)
Parameters
Returns
ExecuteHttpRequest(Uri, string, string, TimeSpan, IWebProxy, IDictionary<string, string>)
Executes an HTTP request and returns the response as a string. This method throws WebException and HttpRequestException. In the event HttpRequestException is thrown the StatusCode is sent as user defined data on the exception under the key "StatusCode".
public static string ExecuteHttpRequest(Uri uri, string requestType, string content, TimeSpan timeout, IWebProxy proxy, IDictionary<string, string> headers)
Parameters
uri
UriThe URI to make the request to
requestType
stringThe request type: GET, PUT, POST
content
stringnull or the content to send with the request
timeout
TimeSpanTimeout for the request
proxy
IWebProxyProxy for the request
headers
IDictionary<string, string>null or any headers to send with the request
Returns
- string
The response as a string.
ForceCanonicalPathAndQuery(Uri)
public static void ForceCanonicalPathAndQuery(Uri uri)
Parameters
uri
Uri
GenerateChecksumForBytes(byte[], bool)
Generates an MD5 Digest for the given byte array
public static string GenerateChecksumForBytes(byte[] content, bool fBase64Encode)
Parameters
content
byte[]The content for which the MD5 Digest needs to be computed.
fBase64Encode
boolWhether the returned checksum should be base64 encoded.
Returns
- string
A string representation of the hash with or w/o base64 encoding
GenerateChecksumForContent(string, bool)
Generates an MD5 Digest for the string-based content
public static string GenerateChecksumForContent(string content, bool fBase64Encode)
Parameters
content
stringThe content for which the MD5 Digest needs to be computed.
fBase64Encode
boolWhether the returned checksum should be base64 encoded.
Returns
- string
A string representation of the hash with or w/o base64 encoding
GenerateMD5ChecksumForStream(Stream)
Generates an MD5 Digest for the stream specified
public static string GenerateMD5ChecksumForStream(Stream input)
Parameters
input
StreamThe Stream for which the MD5 Digest needs to be computed.
Returns
- string
A string representation of the hash with base64 encoding
GenerateMemoryStreamFromString(string)
Utility method for converting a string to a MemoryStream.
public static MemoryStream GenerateMemoryStreamFromString(string s)
Parameters
s
string
Returns
GetExtension(string)
Returns an extension of a path. This has the same behavior as System.IO.Path.GetExtension, but does not check the path for invalid characters.
public static string GetExtension(string path)
Parameters
path
string
Returns
GetFormattedTimestampISO8601(int)
Gets the ISO8601 formatted timestamp that is minutesFromNow in the future.
public static string GetFormattedTimestampISO8601(int minutesFromNow)
Parameters
minutesFromNow
intThe number of minutes from the current instant for which the timestamp is needed.
Returns
- string
The ISO8601 formatted future timestamp.
GetFormattedTimestampRFC822(int)
Gets the RFC822 formatted timestamp that is minutesFromNow in the future.
public static string GetFormattedTimestampRFC822(int minutesFromNow)
Parameters
minutesFromNow
intThe number of minutes from the current instant for which the timestamp is needed.
Returns
- string
The ISO8601 formatted future timestamp.
GetTimeSpanInTicks(DateTime)
public static TimeSpan GetTimeSpanInTicks(DateTime dateTime)
Parameters
dateTime
DateTime
Returns
HasBidiControlCharacters(string)
Returns true if the string has any bidirectional control characters.
public static bool HasBidiControlCharacters(string input)
Parameters
input
string
Returns
HexStringToBytes(string)
Convert a hex string to bytes
public static byte[] HexStringToBytes(string hex)
Parameters
hex
stringHexadecimal string
Returns
- byte[]
Byte array corresponding to the hex string.
InvokeInBackground<T>(EventHandler<T>, T, object)
Calls a specific EventHandler in a background thread
public static void InvokeInBackground<T>(EventHandler<T> handler, T args, object sender) where T : EventArgs
Parameters
handler
EventHandler<T>args
Tsender
object
Type Parameters
T
IsAbsolutePath(string)
Determines whether the given string is an absolute path to a root.
public static bool IsAbsolutePath(string path)
Parameters
path
stringThe hypothetical absolute path.
Returns
- bool
True if
path
is an absolute path.
IsPropertySet(object, string)
This method allows to check whether a property of an object returned by a service call is set. This method is needed to discriminate whether a field is not set (not present in the service response) or if it is set to the default value for its type. Using this method is not required for nullable properties (non-ValueType and Nullable) because they will be simply set to null when not included in the service response. This method can also be used on objects used as part of service requests. This method doesn't support objects that are part of the S3 service model.
public static bool IsPropertySet(object awsServiceObject, string propertyName)
Parameters
awsServiceObject
objectAn object that is used in an AWS service request or is returned as part of an AWS service response.
propertyName
stringThe name of the property of awsServiceObject to check.
Returns
- bool
True if the property is set, otherwise false.
Join(List<string>)
Returns a new string created by joining each of the strings in the specified list together, with a comma between them.
public static string Join(List<string> strings)
Parameters
Returns
- string
A new string created by joining each of the strings in the specified list together, with a comma between strings.
JoinResourcePathSegments(IEnumerable<string>, bool)
Joins all path segments with the / character and encodes each segment before joining.
public static string JoinResourcePathSegments(IEnumerable<string> pathSegments, bool path)
Parameters
pathSegments
IEnumerable<string>The segments of a URL path split at each / character
path
boolIf the path property is specified, the accepted path characters {/+:} are not encoded.
Returns
- string
A joined URL with encoded segments
OpenStream(Uri)
public static Stream OpenStream(Uri uri)
Parameters
uri
Uri
Returns
OpenStream(Uri, IWebProxy)
public static Stream OpenStream(Uri uri, IWebProxy proxy)
Parameters
uri
Uriproxy
IWebProxy
Returns
ParseQueryParameters(string)
Parses a query string of a URL and returns the parameters as a string-to-string dictionary.
public static Dictionary<string, string> ParseQueryParameters(string url)
Parameters
url
string
Returns
PreserveStackTrace(Exception)
This method is used preserve the stacktrace used from clients that support async calls. This make sure that exceptions thrown during EndXXX methods has the orignal stacktrace that happen in the background thread.
public static void PreserveStackTrace(Exception exception)
Parameters
exception
Exception
ProtectEncodedSlashUrlEncode(string, bool)
URL encodes a string per the specified RFC with the exception of preserving the encoding of previously encoded slashes. If the path property is specified, the accepted path characters {/+:} are not encoded.
[Obsolete("This method is not supported in AWSSDK 3.5")]
public static string ProtectEncodedSlashUrlEncode(string data, bool path)
Parameters
Returns
- string
The encoded string with any previously encoded %2F preserved
ResolveResourcePath(string, IDictionary<string, string>)
Takes a patterned resource path and resolves it using the key/value path resources into a segmented encoded URL.
public static string ResolveResourcePath(string resourcePath, IDictionary<string, string> pathResources)
Parameters
resourcePath
stringThe patterned resourcePath
pathResources
IDictionary<string, string>The key/value lookup for the patterned resourcePath
Returns
- string
A segmented encoded URL
ResolveResourcePath(string, IDictionary<string, string>, bool)
Takes a patterned resource path and resolves it using the key/value path resources into a segmented encoded URL.
public static string ResolveResourcePath(string resourcePath, IDictionary<string, string> pathResources, bool skipEncodingValidPathChars)
Parameters
resourcePath
stringThe patterned resourcePath
pathResources
IDictionary<string, string>The key/value lookup for the patterned resourcePath
skipEncodingValidPathChars
boolIf true valid path characters {/+:} are not encoded
Returns
- string
A segmented encoded URL
RunProcess(ProcessStartInfo)
Runs a process with the below input parameters.
public static ProcessExecutionResult RunProcess(ProcessStartInfo processStartInfo)
Parameters
processStartInfo
ProcessStartInfo
Returns
RunProcessAsync(ProcessStartInfo)
public static Task<ProcessExecutionResult> RunProcessAsync(ProcessStartInfo processStartInfo)
Parameters
processStartInfo
ProcessStartInfo
Returns
Sleep(int)
public static void Sleep(int ms)
Parameters
ms
int
Sleep(TimeSpan)
public static void Sleep(TimeSpan ts)
Parameters
ts
TimeSpan
SplitResourcePathIntoSegments(string, IDictionary<string, string>)
Splits the resourcePath at / into segments then resolves any keys with the path resource values. Greedy key values will be split into multiple segments at each /.
public static IEnumerable<string> SplitResourcePathIntoSegments(string resourcePath, IDictionary<string, string> pathResources)
Parameters
resourcePath
stringThe patterned resourcePath
pathResources
IDictionary<string, string>The key/value lookup for the patterned resourcePath
Returns
- IEnumerable<string>
A list of path segments where all keys in the resourcePath have been resolved to one or more path segment values
ToHex(byte[], bool)
Helper function to format a byte array into string
public static string ToHex(byte[] data, bool lowercase)
Parameters
Returns
- string
String version of the data
UrlEncode(int, string, bool)
URL encodes a string per the specified RFC. If the path property is specified, the accepted path characters {/+:} are not encoded.
public static string UrlEncode(int rfcNumber, string data, bool path)
Parameters
rfcNumber
intRFC number determing safe characters
data
stringThe string to encode
path
boolWhether the string is a URL path or not
Returns
- string
The encoded string
Remarks
Currently recognised RFC versions are 1738 (Dec '94) and 3986 (Jan '05). If the specified RFC is not recognised, 3986 is used by default.
UrlEncode(string, bool)
URL encodes a string per RFC3986. If the path property is specified, the accepted path characters {/+:} are not encoded.
public static string UrlEncode(string data, bool path)
Parameters
Returns
- string
The encoded string