Docx Documentless Serverless S3 Putobject方法时出错 [英] Docx Document Serverless S3 Error while Putobject Method

查看:102
本文介绍了Docx Documentless Serverless S3 Putobject方法时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我长期处于困境中.我正在尝试使用无服务器框架将docx文件上传到S3 bucket

I am stuck in a situation for a long time. I am trying to upload a docx file to S3 bucket , using Serverless framework

我成功地将docx文档上载到S3存储桶.但是,当我从S3中打开文档时发生了什么.该文档包含转换为base64字符串的缓冲区数据.即大约23页的大字符串.

I was successful in uploading the docx document to S3 bucket.But what happened is, when I opened the document from S3. The document contained a buffer data converted to base64 string. i.e the big string about 23 pages.

docx已损坏.

我的代码

exports.putFile = async(event) =>{
  try {
    var bitmap = fs.readFileSync('./test.docx');
    let bufferValue = new Buffer(bitmap).toString('base64');
    const valueFromBucket = await S3.putObject(
        { Body: bufferValue, 
          Bucket: '********', 
          Key: '*****.docx',
          ContentType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
          ContentDisposition: 'attachment',
          ContentEncoding: 'base64'
          }).promise();
    return{
      statusCode: 200,
      body: JSON.stringify(valueFromBucket)
    }
  }catch(err){
    return {
      statusCode: err.statusCode || 400,
      body: err.message || JSON.stringify(err.message)
    }
  }
}

Serverless.yml

Serverless.yml

service: node11

custom:
  bucket: ******

provider:
  name: aws
  runtime: provided # set to provided
  stage: dev
  region: *****
  iamRoleStatements:
    - Effect: Allow
      Action:
        - s3:*
        - ses:SendEmail
        - ses:SendRawEmail
      Resource: "*"

functions:
  hello:
    handler: handler.*****
    memorySize: 1024
    events:
      - http:
          path: /
          method: get
    layers: # add layer
      - arn:aws:lambda:us-east-1:553035198032:layer:nodejs11:3
  putFile:
    handler: handler.*****
    description: put Object using S3 service.
    memorySize: 1024
    timeout: 5
    events:
      - http:
          path: putfile
          method: post
          integration: lambda
          cors: true
    layers: # add layer
      - arn:aws:lambda:us-east-1:553035198032:layer:nodejs11:3

我认为代码几乎可以正常工作,但是此问题包含在上载文档中的base 64字符串就是这个问题.

I think the code is almost working , but this issue of the base 64 string which is contained in the uploaded document is the issue.

感谢任何帮助,在此先感谢

Any kind of help appreciated, Thanks in advance

推荐答案

上传之前,不必将二进制缓冲区转换为字符串. 放下.toString('base64'),其他所有内容都应该起作用.

It should not be necessary to convert the binary buffer to string before uploading. Drop the .toString('base64'), everything else ought to work.

这篇关于Docx Documentless Serverless S3 Putobject方法时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆