我找不到任何方法或示例代码来列出在给定日期范围内创建的S3文件。我尝试了WithIfModifiedSince
,SetIfModifiedSince
和GetObjectRequest
。
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/ListObjectsRequest.h>
#include <aws/s3/model/Object.h>
/* list_files_s3_create_withen_given_date_range */
{
Aws::S3::S3Client s3_client;
Aws::S3::Model::ListObjectsRequest objects_request;
objects_request.WithBucket(bucket_name);
/* This lists all the files in s3 bucket. */
/* But how to get files within a given date range. */
//Aws::S3::Model::GetObjectRequest object_request;
//object_request.SetBucket(bucket_name);
//object_request.WithIfModifiedSince(DateTime)
auto list_objects_outcome = s3_client.ListObjects(objects_request);
if (list_objects_outcome.IsSuccess())
{
Aws::Vector<Aws::S3::Model::Object> object_list =
list_objects_outcome.GetResult().GetContents();
for (auto const &s3_object : object_list)
{
std::cout << "* " << s3_object.GetKey() << std::endl;
}
}
}
// ------------------------------------
Aws::Utils::DateTime startdt = Aws::Utils::DateTime::DateTime("2019-10-23T10:00:00Z", Aws::Utils::DateFormat::ISO_8601);
Aws::Utils::DateTime enddt = Aws::Utils::DateTime::Now(); Aws::S3::Model::GetObjectRequest object_request; object_request.SetBucket(bucket_name); object_request.WithIfModifiedSince(startdt);
//object_request.SetIfModifiedSince(startdt);
auto object_outcome = s3_client.GetObject(objects_request);
if (object_outcome.IsSuccess())
{
std::cout << object_outcome.GetResultWithOwnership().GetETag() << std::endl;
}
The code is not returning any object(file) from s3 bucket why ?
I Got the solution:
1. List all the files from the AWS S3 bucket using ListObjectRequest() & ListObjects()
2. iterate through all the files 1 by 1
2.1 pass file name as key to getobjectrequest()
2.2 pass startdate to GetObjectRequest using WithIfModifiedSince()
2.3 pass enddate to GetObjectRequest using WithIfUnmodifiedSince()
2.4 call GetObject()
// this will return you the object from s3 bucket only if its created or modified withen given datetime range
//But its not the most efficient solution, since we are sending separate GetObject () request for each file to aws s3 which is increasing the requests made to s3.
//please let me know if there is a more efficient way to get all files within a given datetime range in a single request.