我正在使用 nestjs 并且我想上传到 amazon s3,但是每个请求都失败并且错误是:POST requires exactly one file upload per request这是我的代码实现:const formData = new FormData();formData.append('policy', clientPayload.policy);formData.append('key', clientPayload.key);formData.append('x-amz-signature', clientPayload['x-amz-signature']);formData.append('x-amz-algorithm', clientPayload['x-amz-algorithm']);formData.append('x-amz-date', clientPayload['x-amz-date']);formData.append('x-amz-credential', clientPayload['x-amz-credential']);formData.append('success_action_status', '201');formData.append('success_action_redirect', '');formData.append('file', file.path);try { const data = await this.http .post(AppConfig.awsServices.bucketUrl, formData, { headers: { 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW', }, }) .toPromise();这是回应:data: '<?xml version="1.0" encoding="UTF-8"?>\n' + '<Error><Code>InvalidArgument</Code><Message>POST requires exactly one file upload per request.</Message><ArgumentName>file</ArgumentName><ArgumentValue>0</ArgumentValue><RequestId>D602243726B03B53</RequestId><HostId>qjLwo8jak8yb3iWOXp4fbqAw391MW7d/3/9r8AzqA20hAvYd1Kgj0PJIAEv+v9sMckixT9WtUoA=</HostId></Error>'这里还有图片附件:这是我试图从文档中实现的请求:https://dev.vdocipher.com/api/docs/book/upload/file.html
1 回答
萧十郎
TA贡献1815条经验 获得超13个赞
看起来你实际上并没有上传文件,你只是传递了文件的路径。将其更改为:
const formData = new FormData();
// your .append() calls here
// instead of formData.append('file', file.path);
form.append('file', fs.createReadStream('path-to-file'));
也不要手动指定内容类型标头,httpService 应该处理这个问题:
const data = await this.http
.post(AppConfig.awsServices.bucketUrl, formData, { headers: { ...formData.getHeaders() })
.toPromise();
添加回答
举报
0/150
提交
取消