【问题标题】:Upload Image to Amazon S3 Using Swift3使用 Swift3 将图像上传到 Amazon S3
【发布时间】:2017-04-28 03:13:40
【问题描述】:

在查看了诸如 (Swift - AWS S3 Upload Image from Photo Library and download it) 和 (Upload image AWS S3 bucket in swift) 之类的不同线程后,我非常接近上传图片,但不知道我做错了什么?

我在控制台中收到以下错误。

错误:错误域=com.amazonaws.AWSS3TransferUtilityErrorDomain Code=1 "(null)" UserInfo={Server=AmazonS3, Transfer-Encoding=Identity, Connection=close, Content-Type=application/xml, Date =格林威治标准时间 2016 年 12 月 13 日星期二 05:58:32,x-amz-request-id=2A76DF0FE33476C5,x-amz-id-2=QWZgOETbWQfddlqKmm0w3Z9HFGM2x1DWnrFjukiajTsIXfbSt9W0orTkoZeNXH/bI1xfc3mxI4Q=-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;
        })

    }

我收到控制台消息“上传开始”,然后收到消息“失败并出现错误”(来自我的完成处理程序),然后是我假设来自 Amazon 的错误。

对我做错了什么有什么想法吗?

【问题讨论】:

    标签: swift amazon-s3 swift3 image-uploading ios10


    【解决方案1】:

    好的,我找到了答案,但我现在有一个不同的问题,我将在另一个关于显示上传进度的问题中发布。

    答案是我的存储桶是在不正确的区域创建的。我在俄勒冈州创建了我的凭据,即 Us-West-2,我第一次意外地在北加州创建了存储桶。这显然造成了错误。

    【讨论】:

    • 请给我一些时间,我会在我的项目中查找我的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 2011-04-16
    • 2021-06-10
    • 2015-09-05
    相关资源
    最近更新 更多