AWSS3GetObjectRequest.ifModifiedSince似乎不起作用

时间:2017-03-31 19:59:26

标签: ios swift amazon-web-services amazon-s3 aws-sdk

我无法让ifModifiedSince工作。这是我的代码:

func updateDatabase()
{
    let objectRequest = AWSS3GetObjectRequest()
    objectRequest?.key = "wa/wa2016/idahoGmu.tiff"
    objectRequest?.bucket = bucketName

    let dateComponents = NSDateComponents()
    dateComponents.day = 10
    dateComponents.month = 4
    dateComponents.year = 2018
    let date = NSCalendar.current.date(from: dateComponents as DateComponents)

    // TODO This isn't working.  It grabs the file regardless of date.
    objectRequest?.ifModifiedSince = date

    let s3 = AWSS3.default()

    s3.getObject(objectRequest!).continueWith
    {
        (task) -> AnyObject! in if let error = task.error
        {
            print("Error: \(error.localizedDescription)")
        }

        if let result = task.result
        {
            let fileManager = FileManager.default
            let documents = Bundle.main.resourcePath
            let writePath = documents?.appending("/Content/idahoGmu.tiff")
            let output = result as AWSS3GetObjectOutput

            let fileData = output.body as! Data
            fileManager.createFile(atPath: writePath!, contents: fileData, attributes: nil)
        }

        return nil;
    }
}

格式化日期有什么问题吗?我读了关于日期格式的这个问题(但现在报告为已修复): AWSS3GetObjectRequest ifModifiedSince not working

当我打印出日期时,它看起来像这样:

2018-04-10 07:00:00 +0000

1 个答案:

答案 0 :(得分:1)

也许您可以将ifModifiedSince替换为AWSS3HeadObjectRequest(),例如:

// create a date to compare
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd"
let checkDate = formatter.date(from: "2016/10/08")

// make a request to AWS S3, to get a info of file
let request = AWSS3HeadObjectRequest()!
request.bucket = bucket
request.key = key
AWSS3.default().headObject(request).continue({
    task -> Any? in

    if let result = task.result,
      ((result as AWSS3HeadObjectOutput) != nil) {

        let servDate = result.lastModified!
        if checkDate < servDate {
            // my file was modified after 2016/10/08
        } else {
            // my file was NOT modified after 2016/10/08
        }
    }
 }

You can see a example where.我在checkDownloadCache(bucket:key)函数中使用类似的东西来创建缓存系统。