namespace CA.Handle { public class AliyunHandle { public static string accessKeyId = "xxxxxxxxxxx"; public static string accessKeySecret = "9JDdggMdT2crQeXXXXXXXXXX"; public static string endpoint = "oss-cn-qingdao.aliyuncs.com"; public static string bucket = "bucket-oss"; OssClient client = new OssClient(endpoint, accessKeyId, accessKeySecret); public void Get() { var flag = true; string maker = string.Empty; var dayTime = 24 * 60 * 60 * 1000; int k = 0; do { var listObjectsRequest = new ListObjectsRequest(bucket); listObjectsRequest.Prefix = "beijing/47383"; //指定下一级文件 listObjectsRequest.Marker = maker; //获取下一页的起始点,它的下一项 listObjectsRequest.MaxKeys = 100;//设置分页的页容量 var result = client.ListObjects(listObjectsRequest); Console.WriteLine("List objects succeeded"); foreach (var summary in result.ObjectSummaries) { Console.WriteLine($"{k}File name:{summary.Key}\r\n\r\n"); TimeSpan timepsan = DateTime.Now - summary.LastModified;//最后修改时间 //时间超过一天就删除 if (timepsan.TotalMilliseconds > dayTime) { //删除oss文件 client.DeleteObject(bucket, summary.Key); Console.WriteLine($"删除{summary.Key}"); } k++; } maker = result.NextMarker; flag = result.IsTruncated;//全部执行完后,为false } while (flag); } } }
阿里云SDK:https://help.aliyun.com/document_detail/32085.html?spm=5176.doc32092.6.729.ZBkOj1