О загрузке фотографии в AWS

Я занимаюсь разработкой программного обеспечения на AWS DynamoDB. Я получил некоторые UIImages. Где их загрузить? S3?

Я пытаюсь использовать S3, но там написано, что моя фотография должна быть ресурсом в комплекте. Но то, что я получил, это UIImage.

Как я могу конвертировать UIImagge в NSBUNDLE?

1 ответ

Решение

Вам необходимо настроить и выполнить запрос на загрузку:

// Configure the authentication
import AWSS3

// configure S3
let S3BucketName = "<#bucket#>"

// configure authentication with Cognito
let CognitoPoolID = "<#cognito-pool-id#>"
let Region = AWSRegionType.<#region#>
let credentialsProvider = AWSCognitoCredentialsProvider(regionType:Region,
    identityPoolId:CognitoPoolID)
let configuration = AWSServiceConfiguration(region:Region, credentialsProvider:credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

// Point to the image you want to upload
let ext = "png"
let imageURL = NSBundle.mainBundle().URLForResource("<#image-name#>", withExtension: ext)!

// Setup the upload request
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest.body = imageURL
uploadRequest.key = NSProcessInfo.processInfo().globallyUniqueString + "." + ext
uploadRequest.bucket = S3BucketName
uploadRequest.contentType = "image/" + ext

// Push the image
let transferManager = AWSS3TransferManager.defaultS3TransferManager()
transferManager.upload(uploadRequest).continueWithBlock { (task) -> AnyObject! in
    if let error = task.error {
        print("Upload failed (\(error))")
    }
    if let exception = task.exception {
        print("Upload failed (\(exception))")
    }
    if task.result != nil {
        let s3URL = NSURL(string: "http://s3.amazonaws.com/\(S3BucketName)/\(uploadRequest.key!)")!
        print("Uploaded to:\n\(s3URL)")
    }
    else {
        print("Unexpected empty result.")
    }
    return nil
}

Теперь вы можете создать экземпляр UIImage, выполнив: let image = UIImage(data: NSData(contentsOfURL: s3URL)!)

Другие вопросы по тегам