使用 Swift3 将图像上传到 Amazon S3
Upload Image to Amazon S3 Using Swift3
在查看了诸如此类的不同主题 (), and () 之后,我已经非常接近可以正常上传图片了,但无法弄清楚我做错了什么?
我的控制台出现以下错误。
错误:错误域=com.amazonaws.AWSS3TransferUtilityErrorDomain Code=1“(null)”UserInfo={Server=AmazonS3, Transfer-Encoding=Identity, Connection=close, Content-Type =application/xml,日期=2016 年 12 月 13 日星期二 05:58:32 GMT,x-amz-request-id=2A76DF0FE33476C5,x-amz-id-2=QWZgOETbWQfddlqKmm0w3Z9HFGM2x1DWnrFjukiajTsIXfbSt9W0orTkoZeNXH/bI1xfc3mxI4Q=,x-amz- bucket-region=us-west-1}
我的代码如下:
let myIdentityPoolId = "us-west-2:dca2beb4-etcetcetc...."
let credentialsProvider:AWSCognitoCredentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.usWest2, identityPoolId: myIdentityPoolId)
let configuration = AWSServiceConfiguration(region: AWSRegionType.usWest2, credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
然后我调用我在下面制作的功能上传我的图片
func uploadImage(filename:String){
print("AWS Upload Image Attempt...")
//defining bucket and upload file name
let S3BucketName: String = "distribution-tech-mobile"
let filepath = "\(AppDelegate.appDelegate.applicationDocumentsDirectory())/\(filename)"
let imageURL = URL(fileURLWithPath: filepath)
let S3UploadKeyName = filename //TODO: Change this later
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest?.bucket = S3BucketName
uploadRequest?.key = filename
uploadRequest?.contentType = "image/jpeg"
uploadRequest?.body = imageURL
uploadRequest?.serverSideEncryption = AWSS3ServerSideEncryption.awsKms
uploadRequest?.uploadProgress = { (bytesSent, totalBytesSent, totalBytesExpectedToSend) -> Void in
DispatchQueue.main.async(execute: {
self.amountUploaded = totalBytesSent // To show the updating data status in label.
self.fileSize = totalBytesExpectedToSend
print("\(totalBytesSent)/\(totalBytesExpectedToSend)")
})
}
self.uploadCompletionHandler = { (task, error) -> Void in
DispatchQueue.main.async(execute: {
if ((error) != nil){
print("Failed with error")
print("Error: \(error!)");
}
else{
print("Sucess")
}
})
}
let transferUtility = AWSS3TransferUtility.default()
let expression = AWSS3TransferUtilityUploadExpression()
transferUtility.uploadFile(imageURL, bucket: S3BucketName, key: S3UploadKeyName, contentType: "image/jpeg", expression: expression, completionHander: uploadCompletionHandler).continue({ (task) -> AnyObject! in
if let error = task.error {
print("Error: \(error.localizedDescription)")
}
if let exception = task.exception {
print("Exception: \(exception.description)")
}
if let _ = task.result {
print("Upload Starting!")
}
return nil;
})
}
我收到控制台消息 "Upload Starting",然后是一条消息 "Failed with error"(来自我的完成处理程序),然后是我假设来自 Amazon 的错误。
关于我做错了什么的想法?
好的,我找到了答案,但现在我遇到了一个不同的问题,我将 post 在另一个关于显示上传进度的问题中。
答案是我的存储桶是在错误的区域创建的。我在俄勒冈州创建了我的凭据,即 Us-West-2,我第一次偶然在北加州创建了存储桶。这显然造成了错误。
在查看了诸如此类的不同主题 (
我的控制台出现以下错误。
错误:错误域=com.amazonaws.AWSS3TransferUtilityErrorDomain Code=1“(null)”UserInfo={Server=AmazonS3, Transfer-Encoding=Identity, Connection=close, Content-Type =application/xml,日期=2016 年 12 月 13 日星期二 05:58:32 GMT,x-amz-request-id=2A76DF0FE33476C5,x-amz-id-2=QWZgOETbWQfddlqKmm0w3Z9HFGM2x1DWnrFjukiajTsIXfbSt9W0orTkoZeNXH/bI1xfc3mxI4Q=,x-amz- bucket-region=us-west-1}
我的代码如下:
let myIdentityPoolId = "us-west-2:dca2beb4-etcetcetc...."
let credentialsProvider:AWSCognitoCredentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.usWest2, identityPoolId: myIdentityPoolId)
let configuration = AWSServiceConfiguration(region: AWSRegionType.usWest2, credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
然后我调用我在下面制作的功能上传我的图片
func uploadImage(filename:String){
print("AWS Upload Image Attempt...")
//defining bucket and upload file name
let S3BucketName: String = "distribution-tech-mobile"
let filepath = "\(AppDelegate.appDelegate.applicationDocumentsDirectory())/\(filename)"
let imageURL = URL(fileURLWithPath: filepath)
let S3UploadKeyName = filename //TODO: Change this later
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest?.bucket = S3BucketName
uploadRequest?.key = filename
uploadRequest?.contentType = "image/jpeg"
uploadRequest?.body = imageURL
uploadRequest?.serverSideEncryption = AWSS3ServerSideEncryption.awsKms
uploadRequest?.uploadProgress = { (bytesSent, totalBytesSent, totalBytesExpectedToSend) -> Void in
DispatchQueue.main.async(execute: {
self.amountUploaded = totalBytesSent // To show the updating data status in label.
self.fileSize = totalBytesExpectedToSend
print("\(totalBytesSent)/\(totalBytesExpectedToSend)")
})
}
self.uploadCompletionHandler = { (task, error) -> Void in
DispatchQueue.main.async(execute: {
if ((error) != nil){
print("Failed with error")
print("Error: \(error!)");
}
else{
print("Sucess")
}
})
}
let transferUtility = AWSS3TransferUtility.default()
let expression = AWSS3TransferUtilityUploadExpression()
transferUtility.uploadFile(imageURL, bucket: S3BucketName, key: S3UploadKeyName, contentType: "image/jpeg", expression: expression, completionHander: uploadCompletionHandler).continue({ (task) -> AnyObject! in
if let error = task.error {
print("Error: \(error.localizedDescription)")
}
if let exception = task.exception {
print("Exception: \(exception.description)")
}
if let _ = task.result {
print("Upload Starting!")
}
return nil;
})
}
我收到控制台消息 "Upload Starting",然后是一条消息 "Failed with error"(来自我的完成处理程序),然后是我假设来自 Amazon 的错误。
关于我做错了什么的想法?
好的,我找到了答案,但现在我遇到了一个不同的问题,我将 post 在另一个关于显示上传进度的问题中。
答案是我的存储桶是在错误的区域创建的。我在俄勒冈州创建了我的凭据,即 Us-West-2,我第一次偶然在北加州创建了存储桶。这显然造成了错误。