我正在尝试将视频上传到 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/