需要提前下好AWSSDK.S3以及core库,然后引用
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
下面是方法:
public void Runner()
{
// 配置你的S3兼容存储服务器的访问信息
var accessKey = "1111111111111111111";//ak代码
var secretKey = "1111111111111111111111111111111111";//sk代码
var endpointUrl = new Uri("http://1.1.2.1:8080"); // 你的S3兼容服务器的URL
// 创建S3客户端,指定自定义的endpoint
using (var client = new AmazonS3Client(accessKey, secretKey, new AmazonS3Config { ServiceURL = endpointUrl.ToString() }))
{
// 指定S3桶名称
string bucketName = "INNOGRIT";
string folderPrefix = "FT/"; // 注意末尾的斜杠,用于模拟文件夹 ,拉FT1
ListObjectsRequest request = new ListObjectsRequest()
{
MaxKeys = 1000,
BucketName = bucketName,
Prefix = folderPrefix
};
ListObjectsResponse response;
string newFold = DateTime.Today.ToString("yyyy-MM-dd");/// "2024-06-24";///
string dateString = DateTime.Today.ToString("yyyy/MM/dd") + " 00:00:00"; //"2024-06-24 00:00:00";//
DateTime newTime = DateTime.Parse(dateString);
do
{
response = client.ListObjects(request);
// 遍历并下载对象
foreach (var obj in response.S3Objects)
{
if (obj.LastModified > newTime)// 限定下拉时间的数据
{
*********************************************************************************************************************
//此处修改路径从而从不同文件夹拉取数据
string LocalPath = @"E:\MP_Log\FT\" + newFold;//FT
if (!Directory.Exists(LocalPath))// 确保目录存在
{
Directory.CreateDirectory(LocalPath);
}
string localFilePath = Path.Combine(LocalPath, obj.Key.Replace(folderPrefix, "")); // 移除前缀以获取本地文件名
GetObjectRequest getObjectRequest = new GetObjectRequest
{
BucketName = bucketName,
Key = obj.Key
};
using (GetObjectResponse responseObject = client.GetObject(getObjectRequest))
using (Stream responseStream = responseObject.ResponseStream)
using (FileStream fileStream = File.Create(localFilePath))
{
responseStream.CopyTo(fileStream);
}
Console.WriteLine($"Downloaded {obj.Key} to {localFilePath}");
}
}
如果结果有分页,则获取下一页
//request.ContinuationToken = response.NextContinuationToken;
request.Marker = response.NextMarker;
} while (response.IsTruncated);
}