[英]Checking metadata of Amazon S3 file using iOS AWS SDK in Swift
I am using the AWS iOS SDK v2, and Swift 1.2. 我正在使用AWS iOS SDK v2和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 since the last run. 我将应用程序的
app.json
文件存储在S3上,并希望在启动时进行检查以查看自上次运行以来是否已更新。 According to research, simply doing a HEAD request on the object should return the "Last-Modified" attribute which can then be compared to the previous. 根据研究,仅对对象执行HEAD请求应返回“ Last-Modified”属性,然后可以将其与前一个属性进行比较。
The problem is that doing a HEAD request on an Object doesn't really seem to be well documented. 问题在于,对Object进行HEAD请求似乎并没有得到很好的记录。 I've got the following:
我有以下几点:
var metaDataRequest = AWSS3HeadObjectRequest()
metaDataRequest.bucket = S3BucketName
metaDataRequest.key = S3AppJSONKey
This seems like a decent start, however I cannot find a way to execute the request. 这似乎是一个不错的开始,但是我找不到执行请求的方法。 The
AWSS3TransferManager
has a download()
method, but the method requires an AWSS3TransferManagerDownloadRequest
type, which an AWSS3HeadObjectRequest
cannot be cast as. AWSS3TransferManager
具有download()
方法,但是该方法需要AWSS3TransferManagerDownloadRequest
类型,无法将其转换为AWSS3HeadObjectRequest
。
Not sure where to go from here, short of just doing the request outside of the SDK. 不确定仅从SDK外部执行请求,就不能从这里去。 I did, however, want to leverage as much of the SDK as possible, so if this is possible I would love to know how.
但是,我确实想尽可能多地利用SDK,因此,如果可能的话,我很想知道如何做。
您需要使用AWSS3
(而不是AWSS3TransferManager
)来调用- headObject:
You need to call headobject method of AWSS3 您需要调用AWSS3的headobject方法
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. 如果您的对象是从指定日期修改的,则它将返回对象,否则将返回状态代码304。
You can use following Swift 2.2 method to check if file exist or not 您可以使用以下Swift 2.2方法检查文件是否存在
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" 假设您有一个名为“ ABC”的存储桶,而不是名为“ XYZ”的文件夹,并且在“ XYZ”内部,则有文件“ abc123”
than write 比写
headObjectsRequest.bucket = "ABC/XYZ" headObjectsRequest.bucket =“ ABC / XYZ”
and 和
headObjectsRequest.key = "abc123" headObjectsRequest.key =“ abc123”
and if you want to check for the entire folder 以及是否要检查整个文件夹
than use 比使用
headObjectsRequest.bucket = "ABC/XYZ" headObjectsRequest.bucket =“ ABC / XYZ”
and 和
headObjectsRequest.key = "" headObjectsRequest.key =“”
Thanks Mohsin 谢谢莫辛
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.