internal void UseClient(ICoreAmazonS3 client, RegionEndpoint region) { if (this.clientsByRegion.ContainsKey(region.SystemName)) { this.clientsByRegion.Remove(region.SystemName); } this.clientsByRegion.Add(region.SystemName, client); }
/// <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); }
/// <summary> /// Deletes the artifacts associated with an import task using the bucket name /// and key prefix to the artifacts in Amazon S3. 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="bucketName">The name of the bucket containing the artifacts</param> /// <param name="keyPrefix">The common key prefix of the artifacts</param> /// <param name="progressCallback">Optional progress callback</param> public static void DeleteImageArtifacts(ICoreAmazonS3 s3Client, string bucketName, string keyPrefix, CleanupProgressCallback progressCallback) { // build the full collection of keys to be deleted so that any progress // indicator managed by the caller is accurate SendProgressNotification(progressCallback, "Collating keys to image file artifacts", null); var artifactKeys = s3Client.GetAllObjectKeys(bucketName, keyPrefix, null); if (artifactKeys.Count == 0) { var msg = string.Format(CultureInfo.InvariantCulture, "Found no image file artifacts with key prefix '{0}' to delete in bucket '{1}'.", keyPrefix, bucketName); SendProgressNotification(progressCallback, msg, null); return; } var deletionMsg = string.Format(CultureInfo.InvariantCulture, "Deleting {0} image file artifacts (key prefix '{1}', bucket '{2}').", artifactKeys.Count, keyPrefix, bucketName); SendProgressNotification(progressCallback, deletionMsg, 0); var index = 0; var processed = 0; do { var batchOfKeys = new List <string>(); while (batchOfKeys.Count < 1000 && index < artifactKeys.Count) { batchOfKeys.Add(artifactKeys[index++]); } s3Client.Deletes(bucketName, batchOfKeys, null); processed += batchOfKeys.Count; SendProgressNotification(progressCallback, deletionMsg, processed / artifactKeys.Count * 100); } while (processed < artifactKeys.Count); }
/// <summary> /// Deletes the artifacts associated with an import task using the bucket name /// and key prefix to the artifacts in Amazon S3. 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="bucketName">The name of the bucket containing the artifacts</param> /// <param name="keyPrefix">The common key prefix of the artifacts</param> /// <param name="progressCallback">Optional progress callback</param> public static void DeleteImageArtifacts(ICoreAmazonS3 s3Client, string bucketName, string keyPrefix, CleanupProgressCallback progressCallback) { // build the full collection of keys to be deleted so that any progress // indicator managed by the caller is accurate SendProgressNotification(progressCallback, "Collating keys to image file artifacts", null); var artifactKeys = s3Client.GetAllObjectKeys(bucketName, keyPrefix, null); if (artifactKeys.Count == 0) { var msg = string.Format(CultureInfo.InvariantCulture, "Found no image file artifacts with key prefix '{0}' to delete in bucket '{1}'.", keyPrefix, bucketName); SendProgressNotification(progressCallback, msg, null); return; } var deletionMsg = string.Format(CultureInfo.InvariantCulture, "Deleting {0} image file artifacts (key prefix '{1}', bucket '{2}').", artifactKeys.Count, keyPrefix, bucketName); SendProgressNotification(progressCallback, deletionMsg, 0); var index = 0; var processed = 0; do { var batchOfKeys = new List<string>(); while (batchOfKeys.Count < 1000 && index < artifactKeys.Count) { batchOfKeys.Add(artifactKeys[index++]); } s3Client.Deletes(bucketName, batchOfKeys, null); processed += batchOfKeys.Count; SendProgressNotification(progressCallback, deletionMsg, processed/artifactKeys.Count * 100); } while (processed < artifactKeys.Count); }
/// <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); }
/// <summary> /// Deletes the image file artifacts associated with the specified conversion task. /// If the task is still active, ignoreActiveTask must be set true to enable artifact /// deletion, which will cause the task to fail. Use this option at your own risk. /// </summary> /// <param name="ec2Client"> /// Amazon EC2 client to use in the process. This should have been instantiated /// with credentials that have access to the conversion task and the region in /// which the import was performed. /// </param> /// <param name="s3Client"> /// Amazon S3 client to use use in the process. This should have been instantiated /// with credentials that have access to the bucket containing the image file artifacts /// and the region in which the bucket exists. /// </param> /// <param name="conversionTaskId"> /// The ID of the conversion task that used the image file /// </param> /// <param name="ignoreActiveTask"> /// If true the artifacts are deleted even if the conversion task is still in progress /// </param> /// <param name="progressCallback">Optional progress callback</param> public static void DeleteImageArtifacts(IAmazonEC2 ec2Client, ICoreAmazonS3 s3Client, string conversionTaskId, bool ignoreActiveTask, CleanupProgressCallback progressCallback) { if (string.IsNullOrEmpty(conversionTaskId)) throw new ArgumentException("Missing conversion task id", "conversionTaskId"); SendProgressNotification(progressCallback, "Inspecting conversion task", null); var request = new DescribeConversionTasksRequest(); request.ConversionTaskIds.Add(conversionTaskId); try { var response = ec2Client.DescribeConversionTasks(request); if (response.ConversionTasks.Count == 0) throw new ArgumentException("Invalid conversion task id", "conversionTaskId"); var conversionTask = response.ConversionTasks[0]; if (!ignoreActiveTask && conversionTask.State.Equals(ConversionTaskState.Active)) throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Import task '{0}' is still active.", conversionTaskId)); string manifestUrl = null; try { // At this time only one disk image per task if (conversionTask.IsSetImportInstance()) manifestUrl = conversionTask.ImportInstance.Volumes[0].Image.ImportManifestUrl; else if (conversionTask.IsSetImportVolume()) manifestUrl = conversionTask.ImportVolume.Image.ImportManifestUrl; } finally { if (string.IsNullOrEmpty(manifestUrl)) throw new ArgumentException("Unable to obtain import manifest url from conversion task instance."); } DeleteImageArtifacts(s3Client, manifestUrl, progressCallback); } catch (AmazonEC2Exception e) { throw new ArgumentException("Expected the id of a valid conversion task", "conversionTaskId", e); } }
/// <summary> /// Constructs an image importer to upload and convert virtual machine image /// or disk volumes for use with Amazon EC2. The importer will use the supplied /// clients for communicating with Amazon S3 and Amazon EC2. Note that the clients /// should be configured to use the same region and AWS account. /// </summary> /// <param name="s3Client">Client to use to upload artifacts to Amazon S3</param> /// <param name="ec2Client">Client to use to request image conversion in Amazon EC2</param> /// <param name="bucketName"> /// The name of the Amazon S3 bucket that will contain the uploaded image and manifest. If the bucket /// does not exist it will be created. /// </param> public DiskImageImporter(ICoreAmazonS3 s3Client, IAmazonEC2 ec2Client, string bucketName) { S3Client = s3Client; EC2Client = ec2Client; BucketName = bucketName; }
/// <summary> /// Deletes the image file artifacts associated with the specified conversion task. /// If the task is still active, ignoreActiveTask must be set true to enable artifact /// deletion, which will cause the task to fail. Use this option at your own risk. /// </summary> /// <param name="ec2Client"> /// Amazon EC2 client to use in the process. This should have been instantiated /// with credentials that have access to the conversion task and the region in /// which the import was performed. /// </param> /// <param name="s3Client"> /// Amazon S3 client to use use in the process. This should have been instantiated /// with credentials that have access to the bucket containing the image file artifacts /// and the region in which the bucket exists. /// </param> /// <param name="conversionTaskId"> /// The ID of the conversion task that used the image file /// </param> /// <param name="ignoreActiveTask"> /// If true the artifacts are deleted even if the conversion task is still in progress /// </param> /// <param name="progressCallback">Optional progress callback</param> public static void DeleteImageArtifacts(IAmazonEC2 ec2Client, ICoreAmazonS3 s3Client, string conversionTaskId, bool ignoreActiveTask, CleanupProgressCallback progressCallback) { if (string.IsNullOrEmpty(conversionTaskId)) { throw new ArgumentException("Missing conversion task id", "conversionTaskId"); } SendProgressNotification(progressCallback, "Inspecting conversion task", null); var request = new DescribeConversionTasksRequest(); request.ConversionTaskIds.Add(conversionTaskId); try { var response = ec2Client.DescribeConversionTasks(request); if (response.ConversionTasks.Count == 0) { throw new ArgumentException("Invalid conversion task id", "conversionTaskId"); } var conversionTask = response.ConversionTasks[0]; if (!ignoreActiveTask && conversionTask.State.Equals(ConversionTaskState.Active)) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Import task '{0}' is still active.", conversionTaskId)); } string manifestUrl = null; try { // At this time only one disk image per task if (conversionTask.IsSetImportInstance()) { manifestUrl = conversionTask.ImportInstance.Volumes[0].Image.ImportManifestUrl; } else if (conversionTask.IsSetImportVolume()) { manifestUrl = conversionTask.ImportVolume.Image.ImportManifestUrl; } } finally { if (string.IsNullOrEmpty(manifestUrl)) { throw new ArgumentException("Unable to obtain import manifest url from conversion task instance."); } } DeleteImageArtifacts(s3Client, manifestUrl, progressCallback); } catch (AmazonEC2Exception e) { throw new ArgumentException("Expected the id of a valid conversion task", "conversionTaskId", e); } }