【问题标题】:How to upload image to S3 using AWSUserFileManager Swift iOS如何使用 AWSUserFileManager Swift iOS 将图像上传到 S3
【发布时间】:2016-09-01 12:05:08
【问题描述】:

我已成功设置 AWS 移动集线器,现在可以使用我的 Facebook 凭据登录。基本上我只想使用AWSMobileHubHelper 将图像上传到 S3 存储桶。我知道AWS 移动集线器的工作方式与常规 S3 上传不同。我们应该使用 AWSUserFileManager 而不是通常的设置 AWScognito 凭据 + S3bucket 等。

我在他们的文档中找到了这个功能,但没有任何解释。我假设我可以将我的图像作为NSData 传递,关键是我的路径“public/aaa”。我运行代码一切似乎都很好。我什至没有抛出错误。但是当我转到我的 S3 存储桶时,图像不存在。

我的问题是:我想传递什么作为密钥?这方面绝对没有任何文档。

如果这不是怎么办呢? 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
    })
}

谢谢。

【问题讨论】:

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


    【解决方案1】:

    我找到了解决方案。基本上,这是正确的。

    要将数据上传到公用文件夹,您只需使用上面的确切功能即可。而 key = "public/yourFileName"

    但如果要上传到私有文件夹,则需要在声明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
        })
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 2013-08-27
      • 1970-01-01
      • 2019-02-21
      • 2017-07-29
      • 2020-12-23
      • 2018-01-19
      • 1970-01-01
      • 2014-12-07
      相关资源
      最近更新 更多