【问题标题】:Uploading images to s3 through stitch aws service fails通过stitch aws服务上传图片到s3失败
【发布时间】: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


    【解决方案1】:

    我遇到了类似的问题,通过从 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)
    

    【讨论】:

      【解决方案2】:

      您应该能够将 base64 图像转换为 BSON.Binary,然后以这种方式上传实际图像(我有一些硬编码的值,但您可以替换它们):

      context.services.get("<aws-svc-name>").s3("<your-region>").PutObject({
          Bucket: 'myBucket',
          Key: "hello.png",
          ContentType: "image/png",
          Body: BSON.Binary.fromBase64("iVBORw0KGgoAA... (rest of the base64 string)", 0),
      })
      

      【讨论】:

      • 谢谢海莉。我从您的帖子中了解到,我需要将 base64 转换为 BSON 对象。不幸的是,我拥有的 BSON 实现(node.js)似乎没有 Binary.fromBase64 函数。在接下来的几天里会更深入地研究它。再次感谢。
      • 所以,我尝试从 mongodb-stitch-browser-core 包中导入 BSON:import { stitchClient, RemoteMongoClient, AwsServiceClient, AwsRequest, BSON } from 'boot/stitch' but I still get TypeError: boot_stitch__WEBPACK_IMPORTED_MODULE_3__["BSON"].Binary.fromBase64 不是函数。`
      • 啊,你能告诉我你是如何传递参数的吗?我给你的答案是,如果你使用的是 Stitch 函数。
      • 感谢 Haley 的跟进,如有任何混淆,我们深表歉意。为了清楚起见,我已经编辑了上面的问题,并有可能粘贴更多代码。
      • 试试:new BSON.Binary(fileData, 0x00)
      猜你喜欢
      • 1970-01-01
      • 2017-04-12
      • 2019-07-20
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      • 2018-12-31
      相关资源
      最近更新 更多