简体   繁体   English

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

[英]Uploading images to s3 through stitch aws service fails

Sorry I am a noob, but I am building a quasar frontend using mongodb stitch as backend.抱歉,我是菜鸟,但我正在使用 mongodb 针迹作为后端构建类星体前端。

I am trying to upload an image using the stitch javascript sdks and the AwsRequest.Builder.我正在尝试使用缝合 javascript sdks 和 AwsRequest.Builder 上传图像。

Quasar gives me an image object with base64 encoded data. Quasar 给了我一个带有 base64 编码数据的图像对象。

I remove the header string from the base64 string (the part that says "data:image/jpeg;base64,"), I convert it to Binary and upload it to the aws s3 bucket.我从 base64 字符串(表示“data:image/jpeg;base64,”的部分)中删除标题字符串,将其转换为二进制并将其上传到 aws s3 存储桶。

I can get the data to upload just fine and when I download it again I get the exact bytes that I have uploaded, so the roundtrip through stitch to aws S3 and back seems to work.我可以让数据上传得很好,当我再次下载它时,我得到了我上传的确切字节,所以通过针迹到 aws S3 并返回的往返似乎工作。

Only, the image I upload can neither be opened in S3 nor cannot be opened once downloaded.只是,我上传的图片在S3中无法打开,下载后也无法打开。

The difficulties seem to be in the conversion to binary of the base64 string and/or in the choice of the proper upload parameters for stitch.困难似乎在于将 base64 字符串转换为二进制文件和/或选择合适的上传参数进行拼接。

Here is my code:这是我的代码:

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

In the snippet above I try to use BSON.Binary.fromBase64 for the conversion to binary as per Haley's suggestion below, but I get following error:在上面的代码段中,我尝试使用 BSON.Binary.fromBase64 按照下面 Haley 的建议转换为二进制文件,但出现以下错误:

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

I have also tried other ways to convert the base64 string to binary, like the vanilla atob() function and the BUFFER npm module, but with no joy.我还尝试了其他方法将 base64 字符串转换为二进制,例如 vanilla atob() 函数和 BUFFER npm 模块,但没有任何乐趣。

I must be doing something stupid somewhere but I cannot find my way out.我一定是在某个地方做了一些愚蠢的事情,但我找不到出路。

I had a similar issue, solved it by creating a buffer from the base64 data and then used new BSON.Binary(new Uint8Array(fileBuffer), 0) to create the BSON Binary Object.我有一个类似的问题,通过从 base64 数据创建一个缓冲区来解决它,然后使用new BSON.Binary(new Uint8Array(fileBuffer), 0)创建 BSON 二进制对象。

Using the OP it would look something like this:使用 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)

You should be able to convert the base64 image to BSON.Binary and then upload the actual image that way (i have some of the values hard-coded, but you can replace those):您应该能够将 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),
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM