使用Ionic 2通过预签名网址上传到s3时出现SignatureDoesNotMatch错误 [英] SignatureDoesNotMatch error when uploading to s3 via a pre signed url using Ionic 2

查看:178
本文介绍了使用Ionic 2通过预签名网址上传到s3时出现SignatureDoesNotMatch错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将视频上传到s3并拥有预先签名的PUT网址。以下是执行此操作的代码。

I am trying to upload a video to s3 and have a pre-signed PUT url. The following is the code to do so.

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {MediaCapture} from 'ionic-native';
import {Http} from '@angular/http';
import { Transfer } from 'ionic-native';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})

export class HomePage {

    public base64Image: string;

    constructor(private navController: NavController, public http: Http) {
        this.base64Image = "https://placehold.it/150x150";
    }

    public takeVideo() {
        MediaCapture.captureVideo({limit:2}).then(function(videoData){
            var link = "https://mysamplebucket.s3.amazonaws.com/non-tp/esx.mov?AWSAccessKeyId=TEMP_KEYY&Expires=1482290587&Signature=JUIHHI%2FcnLkqSVg%3D&x-amz-security-token=FQoDYXDGRfTXk6hma0Rxew6yraAX%2FlYGaQmYLwkvsuuB3%2F%2FtPvGDVs3dIQG0Ty3MeMjn0p%%26djt5xhAMk73pndJbZP0tCYYlvPvlUAyL8x7O%%2B3AwEa%%2B9b43yarIuPLCvujmKLTDyi%%3D%3Di";

            var options: any;

            options = {
             fileKey: 'file',
             fileName: 'esx.mov',
             httpMethod: 'PUT',
             chunkedMode: false,
             mimeType: 'video/quicktime',
             encodeURI: false,
             headers: {
                'Content-Type': 'video/quicktime'
              }
            };

            var ft = new Transfer();
            ft.upload(videoData[0].fullPath, link, options, false)
                .then((result: any) => {
                    this.success(result);
                }).catch((error: any) => {
                    this.failed(error);
                }); 

        }, function(err){
            alert(err);
        });
    }


}

这是生成预先签名的PUT URL的代码。

Here is the code that generates the pre-signed PUT url.

var params = {Bucket: s3_bucket, Key: filename, Expires: 900000};
var url = { 
    'url' : s3.getSignedUrl('putObject', params)
};

我得到, SignatureDoesNotMatch 错误。消息说,我们计算的请求签名与您提供的签名不匹配。检查你的密钥和签名方法。我不确定我在这里做错了什么 - 我看了一些其他的SO和Ionic问题并尝试了他们推荐的无济于事。关于我和做错的任何想法?

I get, SignatureDoesNotMatch error. The message says, The request signature we calculated does not match the signature you provided. Check your key and signing method. I am not sure what I am doing wrong here - I looked a few other SO and Ionic questions and tried what they recommended to no avail. Any ideas on what I and doing wrong?

推荐答案

您的上传 PUT 请求将有内容类型:video / quicktime 标题。

Your upload PUT request will have a Content-Type: video/quicktime header.

当请求中存在 Content-Type 标头(而非响应)时,其值为非Signature V2规范请求中的-optional组件...这意味着您必须将其传递给生成签名的代码。

When the Content-Type header is present in the request (not the response), its value is a non-optional component in the Signature V2 canonical request... which means you have to pass it to the code generating the signature.

var params = {Bucket:s3_bucket,Key:filename,Expires:900000}; 还需要传递给它的这个字符串( video / quicktime ,在这种情况下) as ContentType:... 用于 PUT 请求(但不包含 GET 请求,因为这描述了您发送的内容,而 GET 请求通常不会发送实际内容。

var params = {Bucket: s3_bucket, Key: filename, Expires: 900000}; also needs this string (video/quicktime, in this case) passed to it as ContentType: ... for a PUT request (but not for a GET request, since this describes the content you are sending, and GET requests customarily send no actual content.

SDK文档似乎没有特别提到这一点,但它绝对是S3所要求的。

The SDK documentation doesn't seem to specifically mention this, but it is most definitely required by S3.

这篇关于使用Ionic 2通过预签名网址上传到s3时出现SignatureDoesNotMatch错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆