import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3' import { Upload } from '@aws-sdk/lib-storage' import { PassThrough, Readable } from 'stream' import { WritableStreamBuffer } from 'stream-buffers' import * as path from 'path' async function uploadDoc(title, content) { const s3 = new S3Client({ region: 'oss-cn-hangzhou', endpoint: `https://oss-cn-hangzhou.aliyuncs.com`, credentials: { accessKeyId: 'PXzJyah5rZfWHIIH', secretAccessKey: 'e1MS6j0wypXJrw8CM0hObZu8qKbfah' } }) const stream = new PassThrough() const key = `doc/${title}_${Date.now()}.docx` const upload = new Upload({ client: s3, params: { ACL: 'public-read', Bucket: 'nebuai', Key: key, Body: stream } }) const { execa } = await (eval('import("execa")') as Promise) const p = execa('pandoc', ['-f', 'markdown', '-t', 'docx']) Readable.from(content).pipe(p.stdin) const err = new WritableStreamBuffer() p.pipeStdout(stream) p.pipeStderr(err) try { await p } catch (error) { throw new Error(err.getContents().toString()) } await upload.done() return `https://nebuai.oss-cn-hangzhou.aliyuncs.com/${key}` } uploadDoc('test', '# test').then((res) => { console.log(res) })