我试图将图像上传到Aws S3存储桶。我试图按照教程进行操作,并且我收到错误说“#34;返回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和cognito pool id in us-west-2。我想这就是创造问题。有没有办法解决问题,而不在us-west-2中创建另一个存储桶。 我收到以下错误:
答案 0 :(得分:0)
如果未将身份验证池设置为进行身份验证,您希望存储桶策略有点像这样:注意 Principal 和 Action 值
另外,您使用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;
}