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

问题描述 投票:3回答:1

对不起,我是一个菜鸟,但我正在使用mongodb stitch作为后端建立一个类星体前端。

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

Quasar为我提供了base64编码数据的图像对象。

我从base64字符串(表示“data:image / jpeg; base64,”)的部分中删除了标题字符串,我将其转换为Binary并将其上传到aws s3存储桶。

我可以将数据上传得很好,当我再次下载时,我得到了我上传的确切字节,所以通过针对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)
        })

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

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个回答
0
投票

您应该能够将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),
})
© www.soinside.com 2019 - 2024. All rights reserved.