Beispiel #1
0
        public static async Task <DeleteObjectsResponse> DeleteObjectsModifiedBeforeDateAsync(this S3Helper s3,
                                                                                              string bucketName,
                                                                                              string prefix,
                                                                                              DateTime dt,
                                                                                              bool throwIfNotFound,
                                                                                              CancellationToken cancellationToken = default(CancellationToken))
        {
            var list = await s3.ListObjectsAsync(bucketName : bucketName, prefix : prefix, cancellationToken : cancellationToken);

            var toDelete = list.Where(x => x.LastModified.Ticks < dt.Ticks).Select(x => new KeyVersion()
            {
                Key = x.Key,
            }).ToArray();

            if (toDelete.IsNullOrEmpty())
            {
                if (throwIfNotFound)
                {
                    throw new Exception($"Found {list?.Length ?? 0} objects with prefix {prefix} in bucket {bucketName}, but none were marked for deletion.");
                }
                else
                {
                    return(null);
                }
            }

            var deleted = await s3.DeleteObjectsAsync(bucketName : bucketName, objects : toDelete, cancellationToken : cancellationToken);

            if (!deleted.DeleteErrors.IsNullOrEmpty())
            {
                throw new Exception($"Deleted {deleted.DeletedObjects.Count} objects, but failed {deleted.DeleteErrors.Count}, due to following errors: {deleted.DeleteErrors.JsonSerialize()}");
            }

            return(deleted);
        }
Beispiel #2
0
        /// <summary>
        /// Returns list of s3 status files in ascending order (from oldest to latest)
        /// </summary>
        /// <param name="st"></param>
        /// <returns></returns>
        public static async Task <List <S3Object> > GetStatusList(S3Helper s3h, SyncTarget st, string statusPrefix)
        {
            var cts = new CancellationTokenSource();

            var bkp    = st.status.ToBucketKeyPair();
            var prefix = $"{bkp.key}/{statusPrefix}";
            var list   = (await s3h.ListObjectsAsync(bkp.bucket, prefix, msTimeout: st.timeout, cancellationToken: cts.Token)
                          .TryCatchRetryAsync(maxRepeats: st.retry))
                         .SortAscending(x => x.Key.TrimStart(prefix).TrimEnd(".json").ToLongOrDefault(0)).ToList();

            if (cts.IsCancellationRequested)
            {
                return(null);
            }

            return(list);
        }