/// <summary>
        /// Deletes the artifacts associated with an import task using a presigned
        /// url to address the manifest for the import. No check is performed to
        /// determine whether the associated conversion task is in progress.
        /// </summary>
        /// <param name="s3Client">
        /// An Amazon S3 client for the operation to use. This should have been constructed
        /// using credentials that have access to the bucket containing the image file
        /// artifacts and be scoped to the region containing the bucket.
        /// </param>
        /// <param name="manifestUrl">
        /// Presigned URL to the import manifest file
        /// </param>
        /// <param name="progressCallback">Optional progress callback</param>
        public static void DeleteImageArtifacts(ICoreAmazonS3 s3Client,
                                                string manifestUrl,
                                                CleanupProgressCallback progressCallback)
        {
            if (string.IsNullOrEmpty(manifestUrl))
            {
                throw new ArgumentException("Expected valid presigned url to the import manifest.");
            }

            var s3Uri = new S3Uri(manifestUrl);
            // strip the manifest object name away from the key to get the overall key prefix
            // to the objects
            var lastSlashPos = s3Uri.Key.LastIndexOf('/');

            DeleteImageArtifacts(s3Client, s3Uri.Bucket, s3Uri.Key.Substring(0, lastSlashPos), progressCallback);
        }
Example #2
0
        /// <summary>
        /// Returns the canonicalized resource path for the service endpoint
        /// </summary>
        /// <param name="endpoint">Endpoint URL for the request</param>
        /// <param name="resourcePath">Resource path for the request</param>
        /// <param name="detectPreEncode">If true pre URL encode path segments if necessary.
        /// S3 is currently the only service that does not expect pre URL encoded segments.</param>
        /// <remarks>
        /// If resourcePath begins or ends with slash, the resulting canonicalized
        /// path will follow suit.
        /// </remarks>
        /// <returns>Canonicalized resource path for the endpoint</returns>
        public static string CanonicalizeResourcePath(Uri endpoint, string resourcePath, bool detectPreEncode)
        {
            if (endpoint != null)
            {
                var path = endpoint.AbsolutePath;
                if (string.IsNullOrEmpty(path) || string.Equals(path, Slash, StringComparison.Ordinal))
                {
                    path = string.Empty;
                }

                if (!string.IsNullOrEmpty(resourcePath) && resourcePath.StartsWith(Slash, StringComparison.Ordinal))
                {
                    resourcePath = resourcePath.Substring(1);
                }

                if (!string.IsNullOrEmpty(resourcePath))
                {
                    path = path + Slash + resourcePath;
                }

                resourcePath = path;
            }

            if (string.IsNullOrEmpty(resourcePath))
            {
                return(Slash);
            }

            // split path at / into segments
            var pathSegments = resourcePath.Split(new char[] { SlashChar }, StringSplitOptions.None);

            IEnumerable <string> encodedSegments = pathSegments;
            var pathWasPreEncoded = false;

            if (detectPreEncode)
            {
                if (endpoint == null)
                {
                    throw new ArgumentNullException(nameof(endpoint), "A non-null endpoint is necessary to decide whether or not to pre URL encode.");
                }

                // S3 is a special case.  For S3 skip the pre encode.
                // For everything else URL pre encode the resource path segments.
                if (!S3Uri.IsS3Uri(endpoint))
                {
                    encodedSegments   = encodedSegments.Select(segment => ProtectEncodedSlashUrlEncode(segment, true));
                    pathWasPreEncoded = true;
                }
            }

            // Encode for canonicalization
            encodedSegments = encodedSegments.Select(segment => UrlEncode(segment, false));

            // join the encoded segments with /
            var canonicalizedResourcePath = string.Join(Slash, encodedSegments.ToArray());

            // Get the logger each time (it's cached) because we shouldn't store it in a static variable.
            Logger.GetLogger(typeof(AWSSDKUtils)).DebugFormat("{0} encoded {1}{2} for canonicalization: {3}",
                                                              pathWasPreEncoded ? "Double" : "Single",
                                                              resourcePath,
                                                              endpoint == null ? "" : " with endpoint " + endpoint.AbsoluteUri,
                                                              canonicalizedResourcePath);

            return(canonicalizedResourcePath);
        }