ios - 如何使用 AWSUserFileManager Swift iOS 将图像上传到 S3

标签 ios swift amazon-web-services amazon-s3 aws-mobilehub

我已成功设置 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/

相关文章:

amazon-web-services - 域不会解析为 aws ec2 - 使用路由 53 和弹性 ip

mysql - 我应该多久将文档上传到 CloudSearch (Solr)?

amazon-web-services - Cloudformation 模板 - 如何获取计算属性的值

ios - 以编程方式在带有商店的 ViewController 中添加两个 UICollectionView

ios - 表达式类型 '()' 在没有更多上下文的情况下不明确

ios - 这个函数语法是什么意思?

ios - 如何快速从任何对象获取数字

ios - 是否可以在 Objective c 中打开正在运行的后台应用程序

iOS动画绘制多个圆,只显示一个

ios - Swift 和 TestFlight 适用于 iOS 8 但不适用于 iOS 7