状态码501使用Alamofire将图像上传到s3

时间:2017-01-23 00:55:36

标签: ios amazon-s3 alamofire

我授予我的应用程序使用预先签名的URL上传到S3的权限。我的服务器成功地将预先签名的URL返回给应用程序,但是当我在我的Alamofire请求中使用url时,Amazon会发回一个HTTP状态代码为501的响应(服务器错误)。如何格式化我的Alamofire请求以正确上传到S3?

static func performUpload(image: UIImage, completionHandler: @escaping (_ success:Bool, _ getURL:String?, _ err: Error?) -> ()) {


    if let imageData = UIImageJPEGRepresentation(image, 0.1) { // 0.1 for high compression

        //placeholders for now
        let token = ""
        let params = ["Authorization": "Bearer \(token)"]

        //get signed url
        RESTEngine.sharedEngine.getSignedURL(params, success: { response in

            if let response = response, let postURL = response["postURL"], let getURL = response["getURL"] {

                //unwrap the response and put it into parameters
                let gURL = getURL as! String
                let pURL = postURL as! String

                Alamofire.upload(imageData, to: pURL).response { response in

                    if response.error != nil {
                        completionHandler(false, nil, response.error)
                    }
                    else {

                        print(response.response?.statusCode);
                        completionHandler(true, gURL, nil)
                    }


                }
            }
        }, failure: { error in
            print(error)
            completionHandler(false, nil, error)
        })
    }
}

用于创建无符号URL的My Node.js代码如下:

const aws = require('aws-sdk');
const User = require('../models/User.js');
const Item = require('../models/Item.js');
const RefreshToken = require('../models/RefreshToken.js');

var s3 = new aws.S3();

s3.config.update({
  accessKeyId: config.aws.accessKeyId,
  secretAccessKey: config.aws.secretAccessKey});

module.exports = () => {

let routes = {
'get': {

  // }]
  '/gsu': (req, res, next) => {
    var params = {
      Bucket: 'vdev',
      Key: uuid.v4(),
      Expires: 100, //number of seconds to perform the upload
      ContentType: 'image/jpeg', //must match the Content-Type header of the PUT request
    };

    console.log("Got the request!\n")
    s3.getSignedUrl('putObject', params, function(err, signedURL) {
      if (err) {

        //handle with boom later
        console.log("Sending an error.");
        res.error(err);
      }
      else {
        console.log(signedURL)
        res.json({postURL: signedURL, getURL: signedURL.split("?")[0]});
      }
    });
  }

}

XML响应正文:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>NotImplemented</Code>
    <Message>A header you provided implies functionality that is not implemented</Message>
    <Header>Transfer-Encoding</Header>
    <RequestId>732E0B7558831553</RequestId>
    <HostId>1hrPNv7zATIRsLj1O6s5PW47i4hLz5GBrLoeCuz4121pCwrsZxb8DidhX2Vk+X3mFhw/fO8o1UU=</HostId>
</Error>

0 个答案:

没有答案