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

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

问题描述

我正在尝试将视频上传到 s3 并有一个预签名的 PUT url.以下是执行此操作的代码.

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 请求将有一个 Content-Type: video/quicktime 标头.

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

Content-Type 标头出现在请求(不是响应)中时,它的值是 Signature V2 规范请求中的非可选组件...这意味着您必须通过生成签名的代码.

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)传递给它作为 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 通过预签名 url 上传到 s3 时出现 SignatureDoesNotMatch 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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