I'd like to hear about how you all are dealing wit...
# orm-help
s
I'd like to hear about how you all are dealing with file upload with Prisma/GQL in general. @medelman
s
me too. in another project, I'm using AWS Amplify's "Auth" class to authorize a client-side user to use s3.upload
n
m
So, I typically use AWS S3 to store files. Rather than actively handling uploads from the client (and then to S3), I generate AWS Pre-Signed URLs in response to create[File] mutations. These data are then returned to the client for client-side posting.
👍 3
Something along the lines of:
Copy code
import AWS from "aws-sdk";

const s3 = new AWS.S3({
  accessKeyId: process.env.AWS_ACCESS_KEY_ID || "YOUR KEY HERE",
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || "YOUR KEY HERE"
});

const Bucket = process.env.BUCKET || "YOUR BUCKET HERE";

const getSignedLinkFromS3 = async ({ filename, owner }) => {
  const Key = `uploads/${owner}/${filename}`;
  const uploadUri = await s3.getSignedUrl("putObject", { Key, Bucket });
  return {
    uploadUri,
    bucket: Bucket,
    key: Key
  };
};

export const createMedia = async (parent, args, ctx, info) => {
  const { data } = args;
  const mediaDataWithS3Data = await getSignedLinkFromS3(data);
  try {
    return await ctx.db.mutation.createMedia(
      {
        data: {
          ...data,
          ...mediaDataWithS3Data,
          owner: { connect: { id: data.owner } }
        }
      },
      info
    );
  } catch (error) {
    console.log(error);
    return error;
  }
};
s
That seems pretty straight-forward, thanks. Seems like a snippet like that should be in Awesome-Prisma (it might be already) or somewhere in the documentation. @nilan
n
thanks @Sam Jackson! could you share it in the Forum or on Github, then I'll add it right away to the list, @medelman 🙂
m
Will do. I have an existing repo that I will clean up for public consumption
👏 1
😍 1
t
I'm writing a question on forum on uploads topic now lol)
m
Here's the repo; will further cook tomorrow (i.e., add delete, update, etc.) https://github.com/medelman17/prisma-yoga-s3-uploads
👏 1
s
thanks! BTW, if you want to cut down on the size of any code that'll be bundled (via webpack, etc), you can just do
import S3 from 'aws-sdk/clients/s3'
to just import the SDK for that service
👍 2
m
Wanted to keep it pseudo-beginner friendly 🙂
Not for y'all here, but for those to come.
That said, I will certainly take comments on my webpack config because I am a webpack novice and always eager to learn