2016-02-06 101 views

回答

8

做很多研究,我已经得到了这个工作后,

import AWSS3 
import AWSCore 

上传:

我假定你已经实现UIImagePickerControllerDelegate了。

第1步:

  • 举行URL创建变量:

    var imageURL = NSURL() 
    
  • 创建上传完成处理的obj:

    var uploadCompletionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock? 
    

步骤2:从imagePickerController(_:didFinishPickingMediaWithInfo:)获取图像URL:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]){ 

    //getting details of image 
    let uploadFileURL = info[UIImagePickerControllerReferenceURL] as! NSURL 

    let imageName = uploadFileURL.lastPathComponent 
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! as String 

    // getting local path 
    let localPath = (documentDirectory as NSString).stringByAppendingPathComponent(imageName!) 


    //getting actual image 
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage 
    let data = UIImagePNGRepresentation(image) 
    data!.writeToFile(localPath, atomically: true) 

    let imageData = NSData(contentsOfFile: localPath)! 
    imageURL = NSURL(fileURLWithPath: localPath) 

    picker.dismissViewControllerAnimated(true, completion: nil) 
} 

步骤3::呼叫后imageURL集此uploadImage方法在此委托方法的imageURL

  • 设定值将图片上传到您的存储区:

    func uploadImage(){ 
    
        //defining bucket and upload file name 
        let S3BucketName: String = "bucketName" 
        let S3UploadKeyName: String = "public/testImage.jpg" 
    
    
        let expression = AWSS3TransferUtilityUploadExpression() 
        expression.uploadProgress = {(task: AWSS3TransferUtilityTask, bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) in 
         dispatch_async(dispatch_get_main_queue(), { 
          let progress = Float(totalBytesSent)/Float(totalBytesExpectedToSend) 
          print("Progress is: \(progress)") 
         }) 
        } 
    
        self.uploadCompletionHandler = { (task, error) -> Void in 
         dispatch_async(dispatch_get_main_queue(), { 
          if ((error) != nil){ 
           print("Failed with error") 
           print("Error: \(error!)"); 
          } 
          else{ 
           print("Sucess") 
          } 
         }) 
        } 
    
        let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility() 
    
        transferUtility.uploadFile(imageURL, bucket: S3BucketName, key: S3UploadKeyName, contentType: "image/jpeg", expression: expression, completionHander: uploadCompletionHandler).continueWithBlock { (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; 
        } 
    } 
    

    下载:

    func downloadImage(){ 
    
        var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock? 
    
        let S3BucketName: String = "bucketName" 
        let S3DownloadKeyName: String = "public/testImage.jpg" 
    
        let expression = AWSS3TransferUtilityDownloadExpression() 
        expression.downloadProgress = {(task: AWSS3TransferUtilityTask, bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) in 
         dispatch_async(dispatch_get_main_queue(), { 
          let progress = Float(totalBytesSent)/Float(totalBytesExpectedToSend) 
          print("Progress is: \(progress)") 
         }) 
        } 
    
        completionHandler = { (task, location, data, error) -> Void in 
         dispatch_async(dispatch_get_main_queue(), { 
          if ((error) != nil){ 
           print("Failed with error") 
           print("Error: \(error!)") 
          } 
          else{ 
           //Set your image 
           var downloadedImage = UIImage(data: data!) 
          } 
         }) 
        } 
    
        let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility() 
    
        transferUtility.downloadToURL(nil, bucket: S3BucketName, key: S3DownloadKeyName, expression: expression, completionHander: completionHandler).continueWithBlock { (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("Download Starting!") 
         } 
         return nil; 
        } 
    
    } 
    

    Ref. for upload image

    Ref. for download image

    非常感谢jzorz

+0

你是怎么设法让它工作的?我遵循相同的教程,但它不适合我。吨的东西打印在我的控制台,我不知道从哪里开始... – jo3birdtalk

+0

其实,我从github下载了他们的项目,并更改存储桶和钥匙,但未能上传..... – jo3birdtalk

+0

你有没有尝试我的答案,经过测试,我已经上传了吗? –

0

如果你想要的是下载的图片,这是一个更简洁正确的方法:

func downloadImage(bucketName: String, fileName: String, completion: (image: UIImage?, error: NSError?) -> Void) { 
    let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility() 

    transferUtility.downloadDataFromBucket(bucketName, key: fileName, expression: nil) { (task, url, data, error) in 
     var resultImage: UIImage? 

     if let data = data { 
      resultImage = UIImage(data: data) 
     } 

     completion(image: resultImage, error: error) 
    } 
}