iOS - 管道文件到 HTTP 请求

iOS - pipe file to HTTP request

我正在尝试使用预签名请求将大型文件(如 内存不适合 )上传到 S3。我终于让它与 curl

一起工作
curl -v -T video.mp4 "http://<myBucket>.s3.amazonaws.com/video.mp4?AWSAccessKeyId=<myAccessKey>&Expires=1492187347&Signature=vpcUnvGALlVXju31Qk2nXNmBTgc%3D"

我现在正在尝试通过我的应用程序执行此操作。我首先尝试使用 AF(我不想使用):

let videoPath = Bundle.main.path(forResource: "media", ofType: "mov")!
let videoUrl = URL(fileURLWithPath: videoPath)
let presignedUrl = "http://<myBucket>.s3.amazonaws.com/video.mp4?AWSAccessKeyId=<myAccessKey>&Expires=1492187347&Signature=vpcUnvGALlVXju31Qk2nXNmBTgc%3D"
request = Alamofire.upload(videoUrl, to: presignedUrl, method: .put, headers: [:])
request.responseString(completionHandler: { response in
    print(response)
})
request.resume()

打印成功的响应实际上是一个错误:

The request signature we calculated does not match the signature you provided. Check your key and signing method.

我在一些地方读到 headers 可能存在问题,无论如何我不想在这里使用 AF。

Swift 中的什么相当于 curl -T filePath url

签署请求时,传递内容类型。在节点中:

var AWS = require('aws-sdk');

AWS.config.update({
  accessKeyId: accessKeyId,
  secretAccessKey: secretAccessKey,
  region: 'us-east-1',
});

var s3 = new AWS.S3();

var params = {
  Bucket: bucketName,
  Key: 'path/my-video.mp4',  
  Expires: 1800000,
  ContentType: 'video/mp4',
};
var signedUrl = s3.getSignedUrl('putObject', params)
// returns something like:
// https://<- my bucket ->.s3.amazonaws.com/path/my-video.mp4?AWSAccessKeyId=<- my access key ->&Content-Type=video%2Fmp4&Expires=1492203291&Signature=HeRBUObYQiQ6FIH%2Fg%2FOI0qv57VY%3D

现在 swift:

let request = NSMutableURLRequest(url: presignedUrl)
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
request.httpMethod = "PUT"
request.setValue("video/mp4", forHTTPHeaderField: "Content-Type")
request.setValue("<- my bucket ->.s3.amazonaws.com", forHTTPHeaderField: "host")
let uploadTask = session.uploadTask(with: request as URLRequest, fromFile: videoUrl) { data, response, error in
  ...
}
uploadTask.resume()