阿里云OSS 获取目录下所有文件

时间:2021-02-10 23:33:52
  public class AliyunHandle
{
public static string accessKeyId = "a1uI5xxxxxxxxxrP4H";
public static string accessKeySecret = "9JDdggMdT2cxxxxxxxxxxxxxxx5VaE9z";
public static string endpoint = "oss-cn-qingdao.aliyuncs.com";
public static string bucket = "testoss";
OssClient client = new OssClient(endpoint, accessKeyId, accessKeySecret); public void Get()
{
var flag = true;
string maker = string.Empty; var dayTime = * * * ;
int k = ;
do
{
var listObjectsRequest = new ListObjectsRequest(bucket);
listObjectsRequest.Prefix = "test/47383/"; //指定下一级文件
listObjectsRequest.Marker = maker; //获取下一页的起始点,它的下一项
listObjectsRequest.MaxKeys = ;//设置分页的页容量
listObjectsRequest.Delimiter = "/";//跳出递归循环,只去指定目录下的文件。使用它时 Prefix文件路径要以“/”结尾
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);
} }