swift - 在 Swift 中将视频上传到 Amazon S3

标签 swift amazon-web-services 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 information 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
  })

}

关于swift - 在 Swift 中将视频上传到 Amazon S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34770099/

相关文章:

ios - iOS 7 和 iOS8 中的 Alert View 与 UIAlertController

amazon-web-services - 如何在 CloudFormation 模板中引用现有安全组

Heroku 使用 AWS 从 example.com 重定向到 www.example.com 并保留 HTTPS

ios - TableView didSelectRowAt IndexPath 没有被调用

ios - 逐渐提高 SpriteKit 中滚动背景的速度

python - 使用 SAM 通过自定义 python 函数构建 AWS Lambda 层

php - 提供的 token 格式错误或无效

file-upload - 我可以做些什么来加速 S3 上传/更新?

python - 上传亚马逊s3 python,boto3后获取文件url

swift - 二元运算符 '==' 不能应用于类型为 'UIImage?' 和 'String' 的操作数 - Swift 4