ionic-framework - 使用 Ionic 2 通过预签名 url 上传到 s3 时出现 SignatureDoesNotMatch 错误

标签 ionic-framework amazon-s3 ionic2 cordova-plugins put

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

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 的代码。
var params = {Bucket: s3_bucket, Key: filename, Expires: 900000};
var url = { 
    'url' : s3.getSignedUrl('putObject', params)
};

我明白了,SignatureDoesNotMatch错误。消息说,The request signature we calculated does not match the signature you provided. Check your key and signing method.我不确定我在这里做错了什么 - 我查看了其他一些 SO 和 Ionic 问题,并尝试了他们推荐的无济于事。关于我和做错了什么的任何想法?

最佳答案

您的上传 PUT请求将有 Content-Type: video/quicktime标题。

Content-Type header 存在于请求中(不是响应),它的值是 Signature V2 规范请求中的一个非可选组件......这意味着您必须将它传递给生成签名的代码。
var params = {Bucket: s3_bucket, Key: filename, Expires: 900000};还需要将此字符串(在本例中为 video/quicktime )作为 ContentType: ... 传递给它对于 PUT请求(但不适用于 GET 请求,因为这描述了您正在发送的内容,而 GET 请求通常不发送实际内容。

SDK documentation似乎没有特别提到这一点,但 S3 绝对需要它。

关于ionic-framework - 使用 Ionic 2 通过预签名 url 上传到 s3 时出现 SignatureDoesNotMatch 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41078440/

相关文章:

angularjs - 如何在 Ionic 应用程序中显示 Twitter 时间线?

python - 从 S3 下载大量文件

angular - 使用 ngModelChange 去抖 Angular 2

ionic-framework - Ionic 4 更改图像填充颜色

angular - 如何在 Ionic 3 中获取 ionic 复选框的多选值

html - 如何使用 Ionic 实现捏合/捏合?

node.js - 使用 AWS SDK Node.js 的自定义 S3 服务器的自签名证书错误

python - 将像对象这样的文件保存到 s3 我收到错误 : Unicode-objects must be encoded before hashing

css - 将 CSS 规则应用于父选择器和更具体的选择器,而无需重复该行

ios - Cordova:无效数据, block 必须是字符串或缓冲区,而不是对象