【发布时间】:2020-03-31 23:41:36
【问题描述】:
对不起,我是一个菜鸟,但我正在使用 mongodb 缝合作为后端构建一个类星体前端。
我正在尝试使用拼接 javascript sdks 和 AwsRequest.Builder 上传图像。
Quasar 给了我一个带有 base64 编码数据的图像对象。
我从 base64 字符串(显示“data:image/jpeg;base64”的部分)中删除标题字符串,将其转换为二进制并将其上传到 aws s3 存储桶。
我可以很好地上传数据,当我再次下载它时,我会得到我上传的确切字节,所以通过缝合到 aws S3 和返回的往返似乎工作。
只是,我上传的图片既不能在S3中打开,也不能下载后打开。
困难似乎在于将 base64 字符串转换为二进制和/或选择合适的拼接上传参数。
这是我的代码:
var fileSrc = file.__img.src // valid base64 encoded image with header string
var fileData = fileSrc.substr(fileSrc.indexOf(',') + 1) // stripping out header string
var body = BSON.Binary.fromBase64(fileData, 0) // here I get the BSON error
const args = {
ACL: 'public-read',
Bucket: 'elever-erp-document-store',
ContentType: file.type,
ContentEncoding: 'x-www-form-urlencoded', // not sure about the need to specify encoding for binary file
Key: file.name,
Body: body
}
const request = new AwsRequest.Builder()
.withService('s3')
.withRegion('eu-west-1')
.withAction('PutObject')
.withArgs(args)
aws.execute(request.build())
.then(result => {
alert('OK ' + result)
return file
}).catch(err => {
alert('error ' + err)
})
在上面的 sn-p 中,我尝试按照 Haley 的建议使用 BSON.Binary.fromBase64 转换为二进制,但出现以下错误:
boot_stitch__WEBPACK_IMPORTED_MODULE_3__["BSON"].Binary.fromBase64 is not a function.
我还尝试了其他将 base64 字符串转换为二进制的方法,例如 vanilla atob() 函数和 BUFFER npm 模块,但没有任何乐趣。
我一定是在某个地方做了一些愚蠢的事情,但我找不到出路。
【问题讨论】:
标签: mongodb amazon-s3 quasar-framework mongodb-stitch