尝试使用 Alamofire 上传短视频文件。该 api 是一个 API 网关端点,它指向一个处理视频然后将其存储在 S3 中的 lambda 函数。我只需要弄清楚为什么我无法将 Mac 上的视频正确发送到 API。
func uploadVideo() {
let buildUrl = URL(fileURLWithPath: pathToVideoFile)
Alamofire.upload(buildUrl, to: apiUrl).responseJSON { response in
debugPrint(response)
}
}
最佳答案
我能够找到我尝试的默认上传过程的问题。我的视频文件大于 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()
}
}
}
处理预签名 URL 的 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 });
}
});
};
关于swift - Alamofire OSX 上传视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46062224/