我在一个包含一些对象的亚马逊存储桶中有一个文件夹。
如何获取最旧的添加对象?
public FileMetaData Poll()
{
var config = new AmazonS3Config();
config.ServiceURL = "s3.amazonaws.com";
config.CommunicationProtocol = Protocol.HTTP;
string bucketName = "bucketname1";
string accessKey = "accesskey1";
string secretKey = "secretkey1";
Amazon.S3.AmazonS3 client = AWSClientFactory.CreateAmazonS3Client(accessKey,
secretKey,
config);
var request = new GetObjectRequest();
request.WithBucketName(bucketName);
// how to get the oldest object?
GetObjectResponse response = client.GetObject(request);
// todo
return null;
}
我已经尝试过下面的代码,它工作正常,但问题是它加载了所有对象,然后找到最旧的,我认为这是一个不好的做法:
var request = new ListObjectsRequest()
.WithBucketName(bucketName)
.WithPrefix(this._folderPath);
ListObjectsResponse response = client.ListObjects(request);
S3Object s3Object = response.S3Objects
.Where(p => !p.Key.EndsWith("_$folder$"))
.OrderBy(k => k.LastModified).FirstOrDefault();
var getObjectRequest = new GetObjectRequest()
.WithBucketName(bucketName)
.WithKey(s3Object.Key);
GetObjectResponse getObjectResponse = client.GetObject(getObjectRequest);
// provider
string provider = getObjectResponse.Metadata.Get("x-amz-meta-provider");
string site = getObjectResponse.Metadata.Get("x-amz-meta-sitename");
string identifier = s3Object.Key.Remove(0, this._folderPath.Length);
string xmlData = new StreamReader(getObjectResponse.ResponseStream, true).ReadToEnd();
return new FileMetaData()
{
Identifier = identifier,
Provider = provider,
SiteName = site,
XmlData = xmlData
};