我有一个用NodeJS编写的无服务器(AWS)应用程序,带有公开的GraphQL端点,并将图像上传到S3 bucket。
GraphQL lambda的定义如下
graphql:
handler: src/functions/graphql/graphql.handler
name: ${self:provider.stackName}-graphql
events:
- http:
method: POST
path: /graphql
GraphQL lambda基本上就是这样
const server = new ApolloServer({
typeDefs,
resolvers,
});
export const handler = server.createHandler();
我有一个
upload
突变定义
type Mutation {
uploadPhoto(file: Upload!, location: String): String!
}
分解器是这样的
public async uploadPhoto(
parent: any,
params: { file: any; location: string }
): Promise<string> {
const file = await params.file;
const { createReadStream, filename, mimetype } = file;
const { ext } = parse(filename);
const uploadParams: S3.Types.PutObjectRequest = {
Bucket: process.env.PHOTO_BUCKET,
Key: `${params.location}/${randomNumber}${ext}`,
ContentType: mimetype,
Body: createReadStream(),
ContentEncoding: "base64",
ACL: "public-read",
};
try {
console.log("upload params", uploadParams);
const sendData = await this.s3bucket.upload(uploadParams).promise();
console.log(sendData);
return sendData.Location;
} catch (error) {
console.log("error uploading file", error);
return error.message;
}
}
还可以让API网关接受我安装的二进制数据
custom:
apigwBinary:
types:
- image/jpeg
- image/jpg
- image/png
plugins:
- serverless-apigw-binary
尽管如此,在这一切之后,我可以上传
.txt
例如文件,但图像看起来像这样
https://cutt.ly/cg7zA6O
就像一个空文件。内容类型正常,文件大小也正常,但图像已损坏。
我猜API网关可能有问题,但我不是100%确定