本文介绍了将视频上传到Swift中的Amazon S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我曾尝试在Swift中将视频文件上传到Amazon S3,但失败了.这是我的代码.我希望你能指导我.
I have tried video file upload to Amazon S3 in Swift but I failed. Here is my code. I hope you can guide me.
@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库文件.
and I have added pod library file on existing project.
推荐答案
传递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中的Amazon S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!