我目前正在尝试使用 AWS IOS SDK 将视频文件上传到 AWSS3。这是我的过程:
// 我接受来自 UIIMagepicker 的视频数据 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
var videoPath = "VideoPath"
if let videoURL = info[UIImagePickerController.InfoKey.mediaURL] {
print("Video URL: \(videoURL)")
uploadVideo(resource: "RandomName", type: "mov", url: videoURL as! URL)
thumbnailImageView.image =
}
//然后我调用这个上传视频的函数来制作视频的AWSS#transferutilityuploadtask。
let bucketName = “somebucketName"
var completionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock?
func uploadVideo(resource: String, type: String , url: URL){ //1
let key = "Tester.mov"
//let resource = Bundle.main.path(forResource: resource, ofType: type)!
let url = url
print(url)
// let url = url
let expression = AWSS3TransferUtilityUploadExpression()
expression.progressBlock = { (task: AWSS3TransferUtilityTask,progress: Progress) -> Void in
print("Progress: \(progress.fractionCompleted * 100 ) %")//2
print("Task state: \(task.progress)")
if progress.isFinished{ //3
print("100%...Upload Finished...")
print("finished Uploading \(url)")
DispatchQueue.main.async {
var alert = UIAlertController(title: "Congratulations!",
message: "Successfully Uploaded Video",
preferredStyle: .alert)
self.present(alert, animated: true, completion:nil)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: {
(action) in
}))
}
//do any task here.
}
}
expression.setValue("public-read-write", forRequestHeader: "x-amz-acl") //4
expression.setValue("public-read-write", forRequestParameter: "x-amz-acl")
completionHandler = { (task:AWSS3TransferUtilityUploadTask, error:NSError?) -> Void in
if(error != nil){
print("Failure uploading file")
}else{
print("Success uploading file!\(print(task.response))")
}
} as? AWSS3TransferUtilityUploadCompletionHandlerBlock
//5
AWSS3TransferUtility.default().uploadFile(url, bucket: bucketName, key: String(key), contentType: "video", expression: expression, completionHandler: self.completionHandler).continueWith(block: { (task:AWSTask) -> AnyObject? in
if(task.error != nil){
print("Error uploading file: \(String(describing: task.error?.localizedDescription))")
}
if(task.result != nil){
print("Starting upload...")
}
return nil
})
}
我还在我的应用程序委托中初始化了 AWSS3 传输连接。
这段代码在我的模拟器中完美运行。它从本地文件路径 url 获取文件并将其上传到我的 AWS Bucket。但是,当我在设备上运行它时,本地路径似乎无法访问视频数据。程序在“Starting upload..”处停止并显示 0 作为进度
我注意到从设备和 sumulator 创建的 url 是不同的。不知道如何接受两者,但由于某种原因,设备 url 没有提供正确的访问权限。
模拟器 VideoURL file:///Users/dominiq/Library/Developer/CoreSimulator/Devices/66E08504-7B2B-4DC3-A473-F35E7BB575F6/data/Containers/Data/PluginKitPlugin/9E664198-094E-458F-A1C0-C0E0E3F9A454/tmp/trim .2F6719E1-B2AC-4DF0-BA59-319A85B770DF.MOV
IOS设备视频网址:
file:///private/var/mobile/Containers/Data/PluginKitPlugin/B62449E7-5EE3-4EAB-A5AC-F7C867928959/tmp/trim.C18D77B0-6247-40BB-A175-A1B058627F9D.MOV