private void DeleteMessage(IAmazonSQS sqs, IAmazonS3 s3, Message message, SqsTransportMessage sqsTransportMessage, TransportMessage transportMessage) { sqs.DeleteMessage(_queueUrl, message.ReceiptHandle); if (!String.IsNullOrEmpty(sqsTransportMessage.S3BodyKey)) { // Delete the S3 body asynchronously. // We don't really care too much if this call succeeds or fails - if it fails, // the S3 bucket lifecycle configuration will eventually delete the message anyway. // So, we can get better performance by not waiting around for this call to finish. var s3DeleteTask = s3.DeleteObjectAsync( new DeleteObjectRequest { BucketName = ConnectionConfiguration.S3BucketForLargeMessages, Key = ConnectionConfiguration.S3KeyPrefix + transportMessage.Id }); s3DeleteTask.ContinueWith(t => { if (t.Exception != null) { // If deleting the message body from S3 fails, we don't // want the exception to make its way through to the _endProcessMessage below, // as the message has been successfully processed and deleted from the SQS queue // and effectively doesn't exist anymore. // It doesn't really matter, as S3 is configured to delete message body data // automatically after a certain period of time. Logger.Warn("Couldn't delete message body from S3. Message body data will be aged out at a later time.", t.Exception); } }); } }
public static Task RemoveFileAsync(IAmazonS3 client, string bucketName, string key, CancellationToken token) { return(client.DeleteObjectAsync(bucketName, key, token)); }
public async Task <HttpStatusCode> DeleteObjectAsync(string bucketName, string key) { var objs = await client.DeleteObjectAsync(bucketName, key); return(objs.HttpStatusCode); }
/// <summary> /// This routine will retireve all objects within the specified S3 Bucket /// Any objects which have a modified date older than our target will be removed /// </summary> /// <param name="modifiedDate"></param> /// <returns></returns> private static async Task <List <S3Object> > ListObjects(DateTime modifiedDate) { try { List <S3Object> s3Objects = new List <S3Object>(); ListObjectsV2Request request = new ListObjectsV2Request() { BucketName = bucketName }; ListObjectsV2Response response; Console.WriteLine("Connecting to S3..."); do { response = await client.ListObjectsV2Async(request); foreach (S3Object entry in response.S3Objects) { if (entry.LastModified.IsAfter(modifiedDate)) { var listResponse = client.ListVersionsAsync(new ListVersionsRequest { BucketName = bucketName, Prefix = entry.Key }); S3ObjectVersion deleteMarkerVersion = listResponse.Result.Versions.FirstOrDefault(x => x.IsLatest); if (deleteMarkerVersion != null) { await client.DeleteObjectAsync(new DeleteObjectRequest { BucketName = bucketName, Key = entry.Key, VersionId = deleteMarkerVersion.VersionId }); } s3Objects.Add(entry); } } Console.WriteLine("Next Continuation Token: {0}", response.NextContinuationToken); request.ContinuationToken = response.NextContinuationToken; } while (response.IsTruncated); return(s3Objects); } catch (AmazonS3Exception e) { Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message); return(null); } catch (Exception e) { Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message); return(null); } }
private async Task DeleteFileFromS3(string bucketName, string billId, string fileName) { string keyName = @"bills/" + billId + "/" + fileName; await _client.DeleteObjectAsync(bucketName, keyName); }