我正在尝试在S3中上传图片并将您的URL保存在数据库中,但是错误和图片中的状态码不超过s3才200。...
后端代码:
const multer = require('multer')
const multerS3 = require('multer-s3')
const aws = require('aws-sdk')
const s3 = new aws.S3()
aws.config.update({
secretAccessKey: "fdsfsfsdf",
accessKeyId: "sdfsdfdsfs",
region: 'us-east-1'
})
const upload = multer({
storage: multerS3({
dirname: '/',
s3: s3,
bucket: 'images-budget',
acl: 'public-read',
key: function (req, file, cb) {
cb(null, file.originalname)
}
})
})
module.exports = upload
路由后端代码:
const upload = require('../../resources/uploadFile')
router.post('/image-upload', upload.array('upl'), function (req, res, next) {
res.send("How I get URL image in S3?");
});
角度代码:
this.productForm = this.formBuilder.group({
photo: ''
})
registerProduct(valueForm) {
this.productService.saveImage(valueForm).subscribe(res => {
});
}
HTML代码:
<form [formGroup]="productForm" enctype="multipart/form-data" novalidate>
<div class="col-12 col-sm-4">
<label for="inputSuccess">Photo</label>
<div class="custom-file">
<input type="file" class="form-control custom-file-input" id="validatedCustomFile" formControlName="photo">
<label class="custom-file-label" for="validatedCustomFile">Choose a picture...</label>
</div>
</div>
<button type="button" class="btn btn-success" (click)="registerProduct(productForm.value)"
[disabled]="!productForm.valid">Save</button>
</form>
然后,输出此错误:
core.js:15724 ERROR
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:3000/budgets/image-upload", ok: false, …}
error: {error: SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse (<anonymous>) at XMLHtt…, text: "Uploaded!"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for http://localhost:3000/budgets/image-upload"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://localhost:3000/budgets/image-upload"
__proto__: HttpResponseBase
我现在还需要如何保存图像的URL,以保存在数据库中。
谢谢大家。