1)用于生成s3 URL的代码
const file = req.files ? req.files.filename || null : null
const extension = path.extname(file.name)
const client = new S3Client()
const params = {
Bucket: S3_BUCKET, // env variable
Key: `tmp/photos/image_file.png`,
ContentDisposition: 'inline',
ContentType: file.mimetype,
Body: file.data
}
const command = new PutObjectCommand(params)
const presignedS3Url = await getSignedUrl(client, command, { expiresIn: 3600 })
我可以使用此CURL请求上载图像
curl --location --request PUT $presignedS3Url \
--header 'Content-Disposition: inline' \
--header 'Content-Type: image/png' \
--data-binary '@/Users/name/Downloads/image-file.png'
无法使用axios上传
const imageFile = fs.readFile('/Users/filepath/image-file.png')
const bufferString = Buffer.from(imageFile, 'base64')
await axios.put(presignedS3Url, {
data: bufferString
}, {
headers: {
'Content-Type': file.mimetype,
'Content-Disposition': 'inline'
}
})
如何使用Axios或XHR上传图像?
2条答案
按热度按时间tmb3ates1#
希望这对你有帮助
4ngedf3f2#
在生成预签名URL时使用此
若要使用XMLHttpRequest进行上载