mongodb - 通过 stitch aws 服务将图像上传到 s3 失败

标签 mongodb amazon-s3 quasar-framework mongodb-stitch

抱歉,我是菜鸟,但我正在构建一个使用 mongodb stitch 作为后端的类星体前端。

我正在尝试使用 stitch javascript sdks 和 AwsRequest.Builder 上传图像。

Quasar 给了我一个带有 base64 编码数据的图像对象。

我从 base64 字符串(显示“data:image/jpeg;base64,”的部分)中删除 header 字符串,然后将其转换为二进制并将其上传到 aws s3 存储桶。

我可以很好地上传数据,当我再次下载它时,我得到了我上传的确切字节,因此从 stitch 到 aws S3 的往返似乎有效。

只是,我上传的图片无法在S3中打开,下载后也无法打开。

困难似乎在于将 base64 字符串转换为二进制和/或为 stitch 选择合适的上传参数。

这是我的代码:

      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)
        })

在上面的代码片段中,我尝试按照 Haley 下面的建议使用 BSON.Binary.fromBase64 转换为二进制文件,但出现以下错误:

boot_stitch__WEBPACK_IMPORTED_MODULE_3__["BSON"].Binary.fromBase64 is not a function.

我也尝试过其他方法将 base64 字符串转换为二进制,例如 vanilla atob() 函数和 BUFFER npm 模块,但没有任何乐趣。

我一定是在某个地方做了一些愚蠢的事情,但我找不到出路。

最佳答案

我有一个类似的问题,通过从 base64 数据创建缓冲区解决了它,然后使用 new BSON.Binary(new Uint8Array(fileBuffer), 0) 创建 BSON 二进制对象。

使用 OP 看起来像这样:

var fileSrc = file.__img.src  // valid base64 encoded image with header string
var fileData = fileSrc.substr(fileSrc.indexOf(',') + 1) // stripping out header string
var fileBuffer = new Buffer(fileData, 'base64');
var body = new BSON.Binary(new Uint8Array(fileBuffer), 0)

关于mongodb - 通过 stitch aws 服务将图像上传到 s3 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55435493/

相关文章:

vue.js - 调试 Vue 运行时错误的技巧

mongodb将重复文档分组到一个数组

mongodb - 如何通过引用其先前状态来更新文档?

amazon-web-services - AWS S3 - 能够从本地上传文件但不能从部署(拒绝访问)

node.js - NodeJS - 从可读流中查看数据事件,而无需从可写流中相应暂停

vue.js - 类星体 : Is there any way to get the value typed into a QSelect with use-input before it gets removed on blur?

vue.js - 在 Quasar CLI 中使用 lang ="pug"

mongodb - 将 Mongo shell 脚本转换为 SpringData/Java

ruby-on-rails - MongoDB ruby 日期

r - 如何使用 IAM 角色身份验证从 EC2 上的 RStudio 访问 S3 数据?