Checking metadata of Amazon S3 file using iOS AWS SDK in Swift

前端 未结 3 1866
暗喜
暗喜 2021-01-14 23:08

I am using the AWS iOS SDK v2, and Swift 1.2.

I am storing my app\'s app.json file on S3 and want to check it at launch to see if it\'s been updated sin

相关标签:
3条回答
  • 2021-01-14 23:21

    You need to call headobject method of AWSS3

         var request = AWSS3HeadObjectRequest()
    
        request.bucket = "flkasdhflhad"
        request.key = "hfsdahfjkhadjkshf"
    
       request.ifModifiedSince = NSDate()
    
    
      var s3 = AWSS3.defaultS3()
    
        s3.headObject(request) { ( output1 : AWSS3HeadObjectOutput?, error : NSError?) -> Void in
       print( output1?.description())
        }
    

    if your object is modified from specified date then it will return u the object otherwise it will return u the status code 304.

    0 讨论(0)
  • 2021-01-14 23:28

    You need to use AWSS3 (instead of AWSS3TransferManager) to call - headObject:.

    0 讨论(0)
  • 2021-01-14 23:37

    You can use following Swift 2.2 method to check if file exist or not

    func checkIfFileExist() {
        let s3 = AWSS3.defaultS3()
        let headObjectsRequest = AWSS3HeadObjectRequest()
        headObjectsRequest.bucket = "YourBucketName" //Don't add "/" at end of your bucket Name    
    
        headObjectsRequest.key = "YourFileNameYouWantToCheckFor" //Don't add "/" in start of file name 
    
        s3.headObject(headObjectsRequest).continueWithBlock { (task) -> AnyObject! in
            if let error = task.error {
                print("Error to find file: \(error)")
    
            } else {
                print("fileExist")
            }
        }
    
    }
    

    Note: Example to set bucket and key

    suppose you have bucket named "ABC" and than folder named "XYZ" and than inside "XYZ" you have file "abc123"

    than write

    headObjectsRequest.bucket = "ABC/XYZ"

    and

    headObjectsRequest.key = "abc123"

    and if you want to check for the entire folder

    than use

    headObjectsRequest.bucket = "ABC/XYZ"

    and

    headObjectsRequest.key = ""

    Thanks Mohsin

    0 讨论(0)
提交回复
热议问题