我正在尝试使用角度客户端将文件上传到Amazon S3。我已经使用NodeJs应用服务器生成了Presigned URL。在将文件上传到预签名URL时,但是从客户端上传文件失败,被获取(取消)。
我尝试上传以下格式的文件:buffer,base64,formData和raw File
如果我尝试将邮递员上传到二进制格式的生成的URL,则此方法有效
使用NodeJ生成预签名URL
const s3 = new AWS.S3({
accessKeyId: AWS_ACCESS_KEY,
secretAccessKey: AWS_SECRET_ACCESS_KEY,
region: 'eu-central-1',
});
const signedUrlExpireSeconds = 60 * 5;
const presignedS3Url = s3.getSignedUrl('putObject', {
Bucket: process.env.bucket,
Key: './test3Public.pdf',
Expires: signedUrlExpireSeconds,
ACL: 'public-read',
ContentType: 'application/pdf',
});
的HTML
<input
type="file"
(change)="onFileSelected($event)"
accept="application/pdf, .docx"
/>
Component.ts
onFileSelected(event: any) {
this.fileToUpload = <File>event.target.files[0];
this.fileUpload
.generatePresignedURL(this.fileToUpload.name)
.pipe(first())
.subscribe(
(data) => {
this.fileUpload
.uploadfileAWSS3(this.fileToUpload, data.presignedS3Url)
.pipe(first())
.subscribe(
(data) => {
console.log('uploaded', data);
},
(error) => {
console.log('error', error);
}
);
},
(error) => {
console.log('error', error);
}
);
}
我发送文件的格式:
Angular 11服务
uploadfileAWSS3(file, fileuploadurl) {
const req = new HttpRequest('PUT', fileuploadurl, file);
return this.http.request(req);
}
您能帮我取消上传的问题在哪里吗?
HTML:
<input type="file" (change)="onFileSelected($event)" accept="application/pdf, .docx"
/>
角度代码:
onFileSelected(event) {
const fileToUpload = <File>event.target.files[0];
const bucketParms = {
Bucket: "sample-temp-bucket",
Key: fileToUpload.name,
ContentType: "application/pdf",
ACL: "public-read",
Expires: 3600,
};
s3.getSignedUrl("putObject", bucketParms, (error, url) => {
if (error) console.log("error", error);
if (url) {
this.http.put(url, fileToUpload).subscribe((response) => {
console.log("response receved is ", response);
});
}
});
}
课程:
[
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"PUT",
"POST"
],
"AllowedOrigins": [
"http://localhost:4200"
],
"ExposeHeaders": []
}
]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句