2

我正在尝试将图像上传到 Aws S3 存储桶。我尝试按照教程进行操作,但收到一条错误消息:“由于协议尚未设置而返回 ENOTCONN。” 我是 swift 新手,我无法理解为什么也会发生错误。我的 S3 上传代码如下:

 let uploadRequest = AWSS3TransferManagerUploadRequest()
 uploadRequest?.body = url!
 uploadRequest?.key = remoteFileName
 uploadRequest?.bucket = S3BucketName
 uploadRequest?.contentType = "image/" + ext

 let transferManager = AWSS3TransferManager.default()

 // Perform Upload
 transferManager.upload(uploadRequest!).continueWith(block: { (task:AWSTask<AnyObject>) -> AnyObject! in

      if let error = task.error{
                print("error \(error.localizedDescription)")
            }

      if task.result != nil {
            let url = AWSS3.default().configuration.endpoint.url
            let publicURL = url?.appendingPathComponent((uploadRequest?.bucket!)!).appendingPathComponent((uploadRequest?.key!)!)
               print("Uploaded to:\(publicURL)")
            }
             return nil
 })

我的 S3 位于 ap-south-1 中,认知池 ID 位于 us-west-2 中。我想这就是问题所在。有没有办法解决这个问题,而无需在 us-west-2 中创建另一个存储桶。我收到以下错误:

错误

4

1 回答 1

0

如果没有为身份验证设置 cognito 池,您希望存储桶策略有点像这样:注意PrincipalAction桶策略

另外,你使用有什么特别的原因AWSS3TransferManagerUploadRequest吗?如果该策略无法解决您的问题,您可以使用以下代码AWSS3TransferUtilityUploadExpression异步发送数据块。

   let expression = AWSS3TransferUtilityUploadExpression()

    expression.progressBlock = progressBlock

    transferUtility.uploadData(UIImagePNGRepresentation(imageNew!)!,
                               bucket: "bucket-name",
                               key: (imgName.removeWhitespace()),
                               contentType: "image/png",
                               expression: expression,
                               completionHandler: completionHandler).continueWith { (task) -> AnyObject! in
                                                if let error = task.error {
                                                    print("Error: \(error.localizedDescription)")
                                                }

                                                if let _ = task.result {
                                                    print("Upload Starting!")
                                                    // Do something with uploadTask.
                                                }

                                                return nil;
                }
于 2017-11-22T05:18:07.277 回答