微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

视频上传到Swift中的Amazon S3

我已尝试在 Swift中将视频文件上传到Amazon S3,但我失败了.这是我的代码.我希望你能引导我.

@IBAction func uploadVideoBtnClicked(sender: AnyObject)
{
    //-- Add Amazon Video upload code

    var uploadRequest: awss3transfermanagerUploadRequest = awss3transfermanagerUploadRequest()

    uploadRequest.bucket = "appFile"
    uploadRequest.key = "foldername/test.mov"

    //Move video file to the application folder so it can be read

    var savedVideoURLToBeUsed =  NSUserDefaults.standardUserDefaults().objectForKey("ThisIsTheVideoIWantToUse") as! String
    print("Video saved in Store: \(savedVideoURLToBeUsed)")

    var url: NSURL = self.videoPath

    uploadRequest.body = url

    print("URL: \(url)")

    let transferManager: awss3transfermanager = awss3transfermanager.defaultS3TransferManager()
    transferManager.upload(uploadRequest).continueWithExecutor(AWSExecutor.mainThreadExecutor(),withBlock: { (AWSTask) -> AnyObject! in

        //Handle errors
        if AWSTask.error != nil {

            println("Error in uploading the video: \(AWSTask.error)")

            // Retrive @R_816_4045@ion important for later downloading
        } else {
            println("Video upload successful..")
            var uploadResult: AnyObject! = AWSTask.result
            println("Upload result: \(uploadResult)")


        }
        return nil

    })

}

我在现有项目中添加了pod库文件.

解决方法

在uploadVideo中传递视频的URL

func uploadVideo(fileUrl : URL){

  let newKey = "video/1.mov"

  let uploadRequest = awss3transfermanagerUploadRequest()
  uploadRequest?.body = fileUrl as URL
  uploadRequest?.key = newKey
  uploadRequest?.bucket = "YourBucketName"
  uploadRequest?.acl = AWSS3ObjectCannedACL.publicRead 
  uploadRequest?.contentType = "movie/mov"

  uploadRequest?.uploadProgress = { (bytesSent,totalBytesSent,totalBytesExpectedToSend) -> Void in
      dispatchQueue.main.async(execute: {
          let amountUploaded = totalBytesSent // To show the updating data status in label.
          print(amountUploaded)
      })
  }

  let transferManager = awss3transfermanager.default()
  transferManager.upload(uploadRequest!).continueWith(executor: AWSExecutor.mainThread(),block: { (task) in
      if task.error != nil {
          print(task.error.debugDescription)
      } else {
          // Do something with your result.
          print("done")
      }
      return nil
  })

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐