Skip to content

Instantly share code, notes, and snippets.

@stemmlerjs
Created February 13, 2020 16:50
Show Gist options
  • Select an option

  • Save stemmlerjs/0cba4417f6f40c91943559f90fb0ad5a to your computer and use it in GitHub Desktop.

Select an option

Save stemmlerjs/0cba4417f6f40c91943559f90fb0ad5a to your computer and use it in GitHub Desktop.
import stream from "stream";
type S3UploadStream = {
writeStream: stream.PassThrough;
promise: Promise<AWS.S3.ManagedUpload.SendData>;
};
export class AWSS3Uploader implements ApolloServerFileUploads.IUploader {
private s3: AWS.S3;
public config: S3UploadConfig;
private createUploadStream(key: string): S3UploadStream {
const pass = new stream.PassThrough();
return {
writeStream: pass,
promise: this.s3
.upload({
Bucket: this.config.destinationBucketName,
Key: key,
Body: pass
})
.promise()
};
}
async singleFileUploadResolver(
parent,
{ file }: { file: Promise<ApolloServerFileUploads.File> }
): Promise<ApolloServerFileUploads.UploadedFileResponse> {
const { stream, filename, mimetype, encoding } = await file;
// Create the destination file path
const filePath = this.createDestinationFilePath(
filename,
mimetype,
encoding
);
// Create an upload stream that goes to S3
const uploadStream = this.createUploadStream(filePath);
// Pipe the file data into the upload stream
// Get the link representing the uploaded file
// (optional) save it to our database
return { filename, mimetype, encoding, url: '' };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment