使用Amazon Educate Starter帐户将图像上传到S3

时间:2019-05-24 09:16:57

标签: ios swift amazon-s3

我只想将图像上传到S3,但是我正在使用AWS Educate帐户,并且自4个小时以来我一直在尝试完成此操作,并让ZERO知道不能正常工作的情况。

因此,我已经在AWS控制台上进行了所有设置,并且存储桶对每个人+美国西弗吉尼亚北部地区都是公共的,就像它应该与AWS Educate Accounts一样。

这是我的代码:

        let accessKey = "accessKey"
        let secretKey = "secretKey"
        let credentialsProvider = AWSStaticCredentialsProvider(accessKey: accessKey, secretKey: secretKey)
        let configuration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: credentialsProvider)
        AWSServiceManager.default().defaultServiceConfiguration = configuration

        let transferUtility = AWSS3TransferUtility.default()
        let bucketname = "bucketname"
        let expression = AWSS3TransferUtilityUploadExpression()

        transferUtility.uploadData(jpegData, bucket: bucketname, key: "myTransferUtilityUpload.jpg", contentType: "image/jpg", expression: expression) { (task, error) in
            if let error = error {
                print(error.localizedDescription)
            }

            if let response = task.response {
                print(response)
            }
        }

谁能告诉我我做错了什么?

我收到此错误消息:

  

该操作无法完成。   (com.amazonaws.AWSS3TransferUtilityErrorDomain错误2。)

accessKey + secretKey 我从“欢迎使用AWS Educate Starter Account”仪表板上的Account Details + AWS CLI中获得,显然存储桶名称与控制台中的名称相同

编辑:

if let jpegData = image.jpegData(compressionQuality: 0.7) {

        let fileManager = FileManager.default
        if let documentDirectory = fileManager.urls(for: .documentDirectory,
                                                    in: .userDomainMask).first {
            var sourceFolderURL =  documentDirectory.appendingPathComponent("image.jpg")

            do {
                try jpegData.write(to: sourceFolderURL)
            } catch {
                print(error.localizedDescription)
                completionHandler(nil)
                return
            }

            let accessKey = "-"
            let secretKey = "-"
            let bucketname = "-"
            let credentialsProvider = AWSStaticCredentialsProvider(accessKey: accessKey, secretKey: secretKey)
            let configuration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: credentialsProvider)
            AWSServiceManager.default().defaultServiceConfiguration = configuration


            let uploadRequest = AWSS3TransferManagerUploadRequest()!
            uploadRequest.body = sourceFolderURL
            uploadRequest.key = "image.jpg"
            uploadRequest.bucket = bucketname
            uploadRequest.contentType = "image/jpeg"
            uploadRequest.acl = .publicReadWrite

            let transferManager = AWSS3TransferManager.default()

            transferManager.upload(uploadRequest).continueWith { [weak self] (task) -> Any? in
                if let error = task.error {
                    print("Upload failed with error: (\(error.localizedDescription))")
                    completionHandler(nil)
                    return nil
                }

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

2 个答案:

答案 0 :(得分:1)

我已使用以下代码将图像上传到S3存储桶: 首先,将图像保存到临时目录。

func uploadImageToS3(imageName:String, completion:@escaping (Bool) -> Void) {

    let accessKey = "accessKey"
    let secretKey = "secretKey"

    let credentialsProvider = AWSStaticCredentialsProvider(accessKey: accessKey, secretKey: secretKey)
    let configuration = AWSServiceConfiguration(region:AWSRegionType.APSoutheast2, credentialsProvider:credentialsProvider)

    AWSServiceManager.default().defaultServiceConfiguration = configuration

    let S3BucketName = "bucketname"

    let uploadRequest = AWSS3TransferManagerUploadRequest()!
    uploadRequest.body = self.getImageURLWithName(imageName:imageName)
    uploadRequest.key = "\(imageName)"
    uploadRequest.bucket = S3BucketName
    uploadRequest.contentType = "image/jpeg"

    //You can specify private or public here
    uploadRequest.acl = .private

    let transferManager = AWSS3TransferManager.default()

    transferManager.upload(uploadRequest).continueWith { [weak self] (task) -> Any? in
        ProgressIndicatorHelper.hideGlobalHUD()
        if let error = task.error {
            print("Upload failed with error: (\(error.localizedDescription))")
            completion(false)
        }

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

        return nil
    }
}

我使用以下代码从Path获取图像:

 func getImageURLWithName(imageName:String) -> URL {
        var sourceImageURL: URL!
        let fileManager = FileManager.default
        if let documentDirectory = fileManager.urls(for: .documentDirectory,
                                                    in: .userDomainMask).first {
            var sourceFolderURL:URL!

            sourceFolderURL =  documentDirectory.appendingPathComponent("YourTempFolder")

            sourceImageURL = sourceFolderURL.appendingPathComponent(imageName)

        }
        return sourceImageURL
    }

希望这会有所帮助。

此外:目前,我不认为您可以直接从内存中上传图像。 我搜寻了。以下是相同的链接: https://github.com/aws-amplify/aws-sdk-ios/issues/42

How to upload a UIImage to S3 with AWS iOS SDK v2

如果您发现任何错误,请告诉我。

答案 1 :(得分:0)

也许是政策问题? 尝试将此策略附加到您的存储桶(存储桶策略)

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:*"
            ],
            "Resource": [
                "arn:aws:s3:::mybucketname/*"
            ]
        }
   ]
}