/// <summary>AWS S3 여러 객체 삭제</summary> public DeleteObjectsResponse DeleteObjectList(List<string> pKeyList) { try { using (AmazonS3Client client = new AmazonS3Client()) { DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(); multiObjectDeleteRequest.BucketName = strAwsBucketName; foreach (string key in pKeyList) { multiObjectDeleteRequest.AddKey(key); } DeleteObjectsResponse response = client.DeleteObjects(multiObjectDeleteRequest); //response.DeleteErrors.Count = 실패한 삭제 객체 //response.DeletedObjects.Count = 성공한 삭제 객체 //.Key, .Code, .Message로 정보 확인 가능. return response; } } catch (AmazonS3Exception amazonS3Exception) { throw amazonS3Exception; } }
Task ICoreAmazonS3.DeletesAsync(string bucketName, IEnumerable<string> objectKeys, IDictionary<string, object> additionalProperties, CancellationToken cancellationToken) { var request = new DeleteObjectsRequest { BucketName = bucketName, }; foreach (var key in objectKeys) { request.AddKey(key); } InternalSDKUtils.ApplyValues(request, additionalProperties); return this.DeleteObjectsAsync(request, cancellationToken); }
void ICoreAmazonS3.Deletes(string bucketName, IEnumerable<string> objectKeys, IDictionary<string, object> additionalProperties) { var request = new DeleteObjectsRequest { BucketName = bucketName }; foreach(var key in objectKeys) { request.AddKey(key); } InternalSDKUtils.ApplyValues(request, additionalProperties); this.DeleteObjects(request); }
/// <summary> /// Delete a folder /// </summary> /// <param name="prefix">prefix</param> public void DeleteFolder(string prefix) { // Get all object with specified prefix var listRequest = new ListObjectsRequest() { BucketName = _bucketName, Prefix = prefix }; var deleteRequest = new DeleteObjectsRequest { BucketName = _bucketName }; do { var listResponse = _client.ListObjects(listRequest); // Add all object with specified prefix to delete request. foreach (var entry in listResponse.S3Objects) { deleteRequest.AddKey(entry.Key); } if (listResponse.IsTruncated) { listRequest.Marker = listResponse.NextMarker; } else { listRequest = null; } } while (listRequest != null); // Delete all the object with specified prefix. if (deleteRequest.Objects.Count > 0) { var deleteResponse = _client.DeleteObjects(deleteRequest); deleteResponse.DisposeIfDisposable(); } }
public virtual void DeleteBucket(AmazonS3Client s3Client, string bucketName) { // First, try to delete the bucket. var deleteBucketRequest = new DeleteBucketRequest { BucketName = bucketName }; try { s3Client.DeleteBucket(deleteBucketRequest); // If we get here, no error was generated so we'll assume the bucket was deleted and return. return; } catch (AmazonS3Exception ex) { if (!ex.ErrorCode.Equals("BucketNotEmpty")) { // We got an unanticipated error. Just rethrow. throw; } } // If we got here, then our bucket isn't empty so we need to delete the items in it first. DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest {BucketName = bucketName}; foreach (S3Object obj in s3Client.ListObjects(new ListObjectsRequest {BucketName = bucketName}).S3Objects) { // Add keys for the objects to the delete request deleteObjectsRequest.AddKey(obj.Key, null); } // Submit the request s3Client.DeleteObjects(deleteObjectsRequest); // The bucket is empty now, so delete the bucket. s3Client.DeleteBucket(deleteBucketRequest); }
private void FindKeys(string BucketName, DeleteObjectsRequest deleteRequest, string SearchString, AmazonS3 Client) { ListObjectsRequest request = new ListObjectsRequest { BucketName = BucketName }; using (Client) { do { ListObjectsResponse response = Client.ListObjects(request); foreach (S3Object entry in response.S3Objects) { if (entry.Key.Contains(SearchString)) { Project.Log(Level.Info, "Deleting file: {0}", entry.Key); deleteRequest.AddKey(entry.Key, null); numKeys++; } } // If response is truncated, set the marker to get the next // set of keys. if (response.IsTruncated) { request.Marker = response.NextMarker; } else { request = null; } } while (request != null); } }
/// <summary> /// Removes the manifest and iterates through the parts list to see which parts had been completed when /// failures occur and removes those objects to avoid storage cost to the user (if the user retries the /// command, a different root key guid will be generated leading to potential orphans). /// </summary> /// <param name="manifestFileKey">The object key of the manifest file.</param> /// <param name="partsList">The set of parts that should have been uploaded</param> /// <returns>True if all objects were successfully deleted, false if objects remain that the user should manually clean up</returns> bool RemoveUploadedArtifacts(string manifestFileKey, IEnumerable<ImageFilePart> partsList) { var allRemoved = true; try { S3Client.DeleteObject(new DeleteObjectRequest { BucketName = this.BucketName, Key = manifestFileKey }); } catch (Exception) { allRemoved = false; } var keysToDelete = (from part in partsList where part.UploadCompleted select part.Key).ToList(); var keyIndex = 0; while (keyIndex < keysToDelete.Count) { var request = new DeleteObjectsRequest {BucketName = this.BucketName}; while (keyIndex < keysToDelete.Count && request.Objects.Count <= 1000) { request.AddKey(keysToDelete[keyIndex++]); } try { S3Client.DeleteObjects(request); } catch (Exception) { allRemoved = false; } } return allRemoved; }
/// <summary> /// Removes all objects from the bucket /// </summary> /// <param name="prefix">Only delete objects that begin with the specified prefix.</param> /// <param name="lastModified">Only delete objects that where modified prior to this date.</param> /// <param name="settings">The <see cref="S3Settings"/> required to delete from Amazon S3.</param> public IList<string> DeleteAll(string prefix, DateTimeOffset lastModified, S3Settings settings) { //Get S3 Objects IList<S3Object> objects = this.GetObjects(prefix, settings); List<string> list = new List<string>(); foreach (S3Object obj in objects) { if ((lastModified == DateTimeOffset.MinValue) && (obj.LastModified < lastModified)) { list.Add(obj.Key); } } //Delete AmazonS3Client client = this.GetClient(settings); while (list.Count > 0) { int max = list.Count; if (max > 1000) { max = 1000; } DeleteObjectsRequest request = new DeleteObjectsRequest(); request.BucketName = settings.BucketName; for (int index = 0; index < max; index++) { request.AddKey(list[index]); } client.DeleteObjects(request); _Log.Verbose("Deleting {0} objects from bucket {1}...", max, settings.BucketName); list.RemoveRange(0, max); } return list; }
public void Delete(IEnumerable<string> names) { if (names.Count() > 0) { DeleteObjectsRequest request = new DeleteObjectsRequest(); foreach (var name in names) request.AddKey(name); request.BucketName = BucketName; DeleteObjectsResponse response = _client.DeleteObjects(request); } }
public void DeleteFiles(IEnumerable<string> filePaths) { var batches = filePaths .BatchesOf(MultiObjectLimit); foreach (var batch in batches) { var request = new DeleteObjectsRequest { BucketName = BucketName, }; foreach (var filePath in batch) { request.AddKey(filePath); } AmazonS3.DeleteObjects(request); } }