尝试使用Alamofire上传短视频文件。 api是一个API网关端点,指向一个lambda函数,它处理视频然后将其存储在S3中。我只需要弄清楚为什么我无法正确地从发送到API的mac中获取视频。
func uploadVideo() {
let buildUrl = URL(fileURLWithPath: pathToVideoFile)
Alamofire.upload(buildUrl, to: apiUrl).responseJSON { response in
debugPrint(response)
}
}
答案 0 :(得分:0)
我能够找到我正在尝试的默认上传过程的问题。我的视频文件大于AWS API Gateway支持的最大有效负载大小10MB,因此我可以通过请求AWS S3的预签名URL然后将视频直接发布到S3来创建解决方案
func getPresignedUrl() {
let uploadUrl = URL(string: awsLambdaUrl)
Alamofire.request(uploadUrl!, method: .put, parameters: ["filename": filename ], encoding: JSONEncoding.default).responseJSON { response in
if let result = response.result.value {
let JSON = result as! NSDictionary
self.postToS3(JSON["url"] as! String)
}
}
}
func postToS3(_ url: String) {
let buildUrl = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0], isDirectory: true).appendingPathComponent("\(filename).mp4")
Alamofire.upload(buildUrl, to: url, method: .put, headers: ["Content-Type": "video/mp4", "x-amz-acl": "public-read"]).responseString { response in
if response.result.isSuccess {
return self.deleteVideo()
}
}
}
处理预签名网址的AWS Lambda函数是:
const aws = require('aws-sdk');
const s3 = new aws.S3();
exports.handler = (event, context, callback) => {
var params = {
Bucket: 'YOUR_BUCKET_NAME',
Key: `${event.body.filename}.mp4`,
ACL: 'public-read',
Expires: 120,
ContentType: 'video/mp4'
};
s3.getSignedUrl('putObject', params, function (err, url) {
if (err) {
callback(err);
} else {
callback(null, { url });
}
});
};