我已成功设置 AWS
移动中心,现在我可以使用我的 Facebook 凭据登录。基本上,我只想使用 AWSMobileHubHelper
将图像上传到 S3 存储桶。我知道在 AWS
Mobile hub 中,工作方式与常规 S3 上传不同。我们应该使用 AWSUserFileManager
而不是通常设置 AWScognito
凭据 + S3bucket
等。
我在他们的文档中找到了这个函数,但没有任何解释。我假设我可以将我的图像作为 NSData
传递,关键是我的路径“public/aaa”。我运行了代码,一切似乎都很好。我什至没有抛出错误。但是当我转到我的 S3 存储桶时,图像不在那里。
我的问题是:我应该传递什么作为 key ?绝对没有关于此的任何文档。
如果不是这样怎么办呢? AWSUserFileManager
是如何工作的?
func uploadWithData(data: NSData, forKey key: String) {
let userFilemanager = AWSUserFileManager.defaultUserFileManager()
let localContent = userFilemanager.localContentWithData(data, key: key)
localContent.uploadWithPinOnCompletion(false, progressBlock: {(content: AWSLocalContent?, progress: NSProgress?) -> Void in
// handle progress here
}, completionHandler: {(content: AWSContent?, error: NSError?) -> Void in
if let error = error {
// handle error here
print("Error occured in uploading: \(error)")
return
}
// handle successful upload here
})
}
谢谢。
最佳答案
我找到了解决办法。基本上,这是正确的。
要将数据上传到公共(public)文件夹,您可以简单地使用上面的确切功能。而 key = "public/yourFileName"
但如果要上传到私有(private)文件夹,则需要在声明AWSUserFileManager时加上identityId,key为= "private/(identityId!)/yourFileName":
func uploadWithData(data: NSData, forKey key: String) {
let userFilemanager = AWSUserFileManager.defaultUserFileManager().identityId!
let localContent = userFilemanager.localContentWithData(data, key: key)
localContent.uploadWithPinOnCompletion(false, progressBlock: {(content: AWSLocalContent?, progress: NSProgress?) -> Void in
// handle progress here
}, completionHandler: {(content: AWSContent?, error: NSError?) -> Void in
if let error = error {
// handle error here
print("Error occured in uploading: \(error)")
return
}
// handle successful upload here
})
}
关于ios - 如何使用 AWSUserFileManager Swift iOS 将图像上传到 S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39271135/