使用Amazon Educate Starter帐户将图像上传到S3 [英] Upload image to S3 with Amazon Educate Starter Account

查看:89
本文介绍了使用Amazon Educate Starter帐户将图像上传到S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想将图像上传到S3,但是我使用的是AWS Educate帐户,并且自4个小时以来我一直在尝试完成此操作,并告诉ZERO哪些功能无法正常工作.

I just want to upload an image to S3, but I am using AWS Educate Account and I'm trying since 4 hours to get this done and have ZERO ideas what isn't working correctly.

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

So I've set up everything on the AWS console and the bucket is public to EVERYONE + Region US West N.Virginia like it should be with 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)
            }
        }

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

Can anyone tell me what I do wrong?

我收到此错误消息:

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

The operation couldn’t be completed. (com.amazonaws.AWSS3TransferUtilityErrorDomain error 2.)

accessKey + secretKey 我是从"Welcome to AWS Educate Starter Account"仪表板上的Account Details + AWS CLI中获得的,显然,存储桶名称与控制台中的名称相同

accessKey + secretKey I got from Account Details + AWS CLI on the 'Welcome to AWS Educate Starter Account' Dashboard and obviously the bucket name is the same name like in my console

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
            }
        }
    }

推荐答案

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

I have uploaded an image to S3 bucket with the following code: First, save the image to a temporary directory.

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获取图像:

For getting image from Path following code I have used:

 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

Moreover: Right now I don't think you can directly upload image from memory. I searched for it. Following are links for same: https://github.com/aws-amplify/aws-sdk-ios/issues/42

如何上传UIImageAWS iOS SDK v2升级到S3

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

Let me know if you find any error.

这篇关于使用Amazon Educate Starter帐户将图像上传到S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆