从第三方api,我得到了这种类型的数据

%PDF-1.7 %���� 2093 0 obj <> endobj 2240 0 obj <>/Filter/FlateDecode/ID[(\204\240\33\17\372\324FC\221_B\326\226\7\374\206) (\204\240\33\17\372\324FC\221_B\326\226\7\374\206)]/Index[2093 148]/Info 2092 0 R/Length 164/Prev 1150275/Root 2094 0 R/Size 2241/Type/XRef/W[1 3 1]>> stream x�cbd```b`` �� ���d4�6`�D2��`�L.��������`R���LZ�E������S�`"�V!،�`v���dbgc����0�`?ƃH��`�<0�DJ�lg|2�q��?u�^�Qr������w�|&�g&��ŕ��� endstream endobj startxref 0 %%EOF 2094 0 obj <>/Metadata 1312 0 R/Pages 1566 0 R/StructTreeRoot 1569 0 R/Type/Catalog/ViewerPreferences 2140 0 R>> endobj 2238 0 obj <> stream

这是一个pdf文件,我想上传的s3,这里是代码

const response = await axios.get(url, options);
const file = { originalname: `${docName}.pdf`, buffer: response.data };
await this.s3Service.upload(file, `${S3_BASE_PATH.DOCS}/ENQ_${enquiryDocDto.enquiryId}`);

和S3业务上的上传方法

async upload(file, basePath: string): Promise<string> {

    const fileName = this._getRandomFileName(file)
    const bucketFileKey = `${basePath}/${fileName}`;

    const params: AWS.S3.PutObjectRequest = {
      Bucket: this.bucketName,
      Key: bucketFileKey,
      Body: file.buffer
    };

    this.logger.log(`Uploading ${bucketFileKey}`);

    await this.s3.putObject(params).promise();
    this.logger.log(`Uploaded ${bucketFileKey}`);
    return bucketFileKey;
  }

从api,我得到这个pdf数据的工作在postman,和相同的数据,它返回和保存响应从postman它创建一个有效的pdf.

但通过代码,它没有正确上传

我甚至试着创建这样的缓冲区

const buffer = Buffer.from(response.data, "utf-8")

我试过"binary"种,但没有成功.

当我直接从我的nodejs上传时,

所以我试着把它转换成Buffer,但转换成Uint8Array

UPdate1

const options = {
        headers: { accept: "*/*", authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/octet-stream' }
      };

我试着删除内容类型,接受以及,更改接受到申请人/pdf以及

甚至try 了这个

fs.writeFileSync("A.pdf", Buffer.from(response.data));
      fs.writeFileSync("B.pdf", Buffer.from(response.data, "binary"));
      fs.writeFileSync("C.pdf", Buffer.from(response.data, "utf-8"));
      fs.writeFileSync("D.pdf", response.data);

仍然是同一个问题

推荐答案

try 将responseType: 'arraybuffer'添加到AXIOS选项:

const response = await axios.get(url, {responseType: 'arraybuffer', ...options});

// file is now a buffer
const file = { originalname: `${docName}.pdf`, buffer: response.data };

Node.js相关问答推荐

编辑Mongoose中的对象嵌套数组

如何在RavenDB中执行JS索引?

在对象的嵌套数组中使用$lookup,创建多个记录作为响应,mongodb

通过 Node js 中的 TBA 执行 netsuite REST upsert 操作出现 401 错误

为什么 mongoose 没有常规数组方法

找不到 vue-template-compiler@2.6.14 的匹配版本 | Shopware 6.5 更新后的 node 问题

如果我在 tsx 文件中使用了use client,ssr 会如何发生?

无法使用 Express 设置会话 cookie 的过期日期

运行本地移动自动化测试时,在onPrepare钩子中,ERROR @wdio/cli:utils: A service failed in the 'onPrepare'

无法关闭 node.js 中的mongoose 连接

Zod 模式中的self 数组

我怎样才能让`git`失败而不是要求提供凭据

NodeJS 后端发布请求将数据作为NULL值发布到 SQL Server 表

在 `DataFrame` 上使用用户定义的可链接函数抽象出 Polars 表达式

使用服务帐户将 Firebase 应用程序部署到 Heroku(使用 dotenv 的环境变量)

用于排除多个文件的 node.js glob 模式

在 Node.js 与 Cron 作业(job)中设置间隔?

Node.js:socket.io 关闭客户端连接

在单独的模块中定义 Mongoose 模型

大型项目的 NodeJS vs Play 框架