使用this other SO Post,我能够确定如何最好地确定对象是否已在我的S3存储桶中。但是,我的代码似乎不合时宜,并且我不熟悉AWS错误。
对于初学者来说,我知道我的AWS可以正常工作,因为我已经拥有访问存储桶的代码。
static func uploadData(data:Data, progressHandler: @escaping (_ progress:Progress) -> Void, completionHandlerFunction: @escaping () -> Void, DBKey:String) {
let expression = AWSS3TransferUtilityUploadExpression()
expression.progressBlock = { (task, progress) in
DispatchQueue.main.async(execute: {
progressHandler(progress)
})
}
var completionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock?
completionHandler = { (task, error) -> Void in
DispatchQueue.main.async(execute: {
completionHandlerFunction()
if(error != nil) {
print(error!.localizedDescription)
}
else {
print(task.response!)
}
})
}
let transferUtility = AWSS3TransferUtility.default()
transferUtility.uploadData(data,
bucket: self.bucketName,
key: DBKey,
contentType: "video/mp4",
expression: expression,
completionHandler: completionHandler).continueWith {
(task) -> AnyObject? in
if let error = task.error {
print("Error: \(error.localizedDescription)")
}
if let _ = task.result {
// Do something with uploadTask.
}
return nil;
}
}
所以,我尝试类似的策略来确定某项是否存在
static func checkForItem(item:String, foundHandler: @escaping () -> Void, notFoundHandler: @escaping () -> Void) {
let s3 = AWSS3.default()
let request = AWSS3HeadObjectRequest()
request!.bucket = Storage.bucketName
request!.key = item
s3.headObject(request!).continueWith { (task) -> Any? in
if let error = task.error {
notFoundHandler()
}
else {
foundHandler()
}
return 0
}
}
但是,似乎我越过let s3 = AWSS3.default()
的那一刻,它就进入
为什么有AWSS3.default()
会提供这种情况的任何方法?我什至没有访问我的存储桶或任何重要信息。我的Cocoapods是最新的(只需运行它们),但我愿意尝试任何使它起作用的方法。感谢您的帮助!
答案 0 :(得分:0)
我使用Amazon Web Service的Mobile Hub下载了awsconfiguration.json
并将其粘贴在应用程序中。
{
"UserAgent": "MobileHub/1.0",
"Version": "1.0",
"CredentialsProvider": {
"CognitoIdentity": {
"Default": {
"PoolId": "MyPoolID",
"Region": "MyRegion"
}
}
},
"IdentityManager": {
"Default": {}
},
"S3TransferUtility": {
"Default": {
"Bucket": "MyBucket",
"Region": "MyRegion"
}
},
"DynamoDBObjectMapper": {
"Default": {
"Region": "MyRegion"
}
},
"PinpointAnalytics": {
"Default": {
"AppId": "myAppId",
"Region": "MyRegion"
}
},
"PinpointTargeting": {
"Default": {
"Region": "MyRegion"
}
},
"UserFileManager": {
"Default": {
"Bucket": "MyBucket",
"Region": "MyRegion"
}
},
}
但是,我没有S3
部分。它不是来自Mobile Hub,所以我决定对其进行编辑,看看它是否有效并且确实有效!我添加了这个,
"S3": {
"Default": {
"Bucket": "MyBucket",
"Region": "MyRegion"
}
}
到json的底部,它起作用了!
对于其他遇到亚马逊问题的人,请确保您已正确配置所有aws文件!