private bool CreateAttemptFile(Amazon.S3.Util.S3EventNotification.S3Entity s3Event, int chunkId, string prefix, int processAttempt) { if (!_attemptFileRemoved) { return(false); } var attempt = 0; var key = $"{Settings.Current.Building.Vendor}.{Settings.Current.Building.Id}.{chunkId}.{prefix}.{processAttempt}.txt"; //if (_lastSavedPersonIdOutput.HasValue) // key = $"{chunkId}.{prefix}.{_lastSavedPersonIdOutput.Value}.{processAttempt}.txt"; while (true) { try { attempt++; using (var memoryStream = new MemoryStream()) using (var writer = new StreamWriter(memoryStream)) using (var tu = new TransferUtility(this.S3Client)) { foreach (var rp in _restorePoint) { writer.WriteLine($"{rp.Key}:{rp.Value}"); } writer.Flush(); tu.Upload(memoryStream, s3Event.Bucket.Name, key); } Console.WriteLine($"Attempt file was created - {key} | attempt={attempt}"); return(true); } catch (Exception e) { if (attempt > 5) { Console.WriteLine($"WARN_EXC - Can't create new attempt [{key}]"); Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); return(false); } } } }
private async Task DeleteOriginalSourceFile( ILogWriter <ImageResizeHandler> logger, Amazon.S3.Util.S3EventNotification.S3Entity s3Event, string urlDecodedKey) { DeleteObjectRequest deleteRequest = new DeleteObjectRequest { BucketName = s3Event.Bucket.Name, Key = urlDecodedKey }; logger.LogInformation($"Deleting source file"); await S3Client.DeleteObjectAsync(deleteRequest); }
private bool GetRestorePoint(Amazon.S3.Util.S3EventNotification.S3Entity s3Event) { var attempt = 0; while (true) { try { attempt++; var msg = new StringBuilder(); var timer = new Stopwatch(); timer.Start(); using (var transferUtility = new TransferUtility(this.S3Client)) using (var responseStream = transferUtility.OpenStream(s3Event.Bucket.Name, s3Event.Object.Key)) using (var reader = new StreamReader(responseStream)) { string line; while ((line = reader.ReadLine()) != null) { var fileName = line.Split(':')[0]; var rowIndex = long.Parse(line.Split(':')[1]); _restorePoint.Add(fileName, rowIndex); msg.Append($"{fileName}:{rowIndex};"); } } timer.Stop(); Console.WriteLine("Restore point:" + msg + " | " + timer.ElapsedMilliseconds + "ms"); return(true); } catch (Exception e) { if (attempt > 5) { Console.WriteLine($"WARN_EXC - GetRestorePoint [{s3Event.Object.Key}]"); Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); throw; } } } }
private bool RemoveAttemptFile(Amazon.S3.Util.S3EventNotification.S3Entity s3Event) { if (_attemptFileRemoved) { return(true); } var attempt = 0; var key = s3Event.Object.Key; Console.WriteLine(s3Event.Bucket.Name + "." + key); while (true) { try { attempt++; var task = this.S3Client.DeleteObjectAsync(new DeleteObjectRequest { BucketName = s3Event.Bucket.Name, Key = key }); task.Wait(); Console.WriteLine($"Attempt file was removed - {key} | attempt={attempt}"); _attemptFileRemoved = true; return(true); } catch (Exception e) { if (attempt > 5) { Console.WriteLine($"WARN_EXC - Can't remove [{key}]"); Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); return(false); } } } }