question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Preprocessing External Images from S3

See original GitHub issue

@lannonbr, I’m hoping you can help. I have a question regarding the documentation you wrote for ‘Preprocessing External Images#17385 , it’s quite a deviation from the example that you provided so ill provide some context.

I am building an e-commerce store, which contains products (obv), each of the product pages is created programmatically from gatsby-node. I have images for each of these products in an S3 bucket. The data for each product contains image1, which is the name (key) of the file in the S3 bucket.

const productQuery = `
query ($nextToken: String, $region: AWSAppSync_Region!, $productType: String!) {
    appSync {
        productsForCategory(
        region: $region,
        productTypeCategorySlugSubCategorySlugRatingsTotalSavingsVsRrpValue: {
            beginsWith: {
            productType: $productType
            }
        },
        nextToken: $nextToken
        ) {
        items {
            globalIdColor
            productType
            region
            categorySlug
            subCategorySlug
            image1
        }
        nextToken
        }
    }
}`;

I wish to attach the image as a File node to this product item in the GraphQL layer.

Currently, I request my images in what feels like a really wrong way. I gain access to my images using the gatsby-source-s3-image plugin. This allows me to access the images through allImageSharp. But then I need to match each product to an image. On each product page, I have an image component which is passed the ‘image1’ as props.filename.

export const Image = props => {

  return (
    <StaticQuery
      query={graphql`
      query {
        images: allImageSharp {
          edges {
            node {
                square: fluid(maxWidth: 300, quality: 100) {
                  ...GatsbyImageSharpFluid
                  originalName
                }
                thumbnailSizes: fluid(maxWidth: 256, quality: 100) {
                  ...GatsbyImageSharpFluid
                  originalName
                }
                largeSizes: fluid(maxWidth: 400, quality: 100) {
                  ...GatsbyImageSharpFluid
                  originalName
                }
            }
          }
        }
      } 
    `}
      render={data => {
        const image = data && data.images && data.images.edges && data.images.edges.find(n => {
          if (n.node.square.originalName && props.filename) {
            const key = n.node.square.originalName.toLowerCase();
            const filename = props.filename.toLowerCase();

            return key.includes(filename);
          }

          return false;
        });

        if (!image) {
          return (
            <ImageUnavailable />
          );
        }

        if (image) {
          return <Img  fluid={image.node.largeSizes} />;
        }
        if (props.thumbnail && image) {
          return <Img  fluid={image.node.thumbnailSizes} />;
        }
        if (props.blogPost && image) {
          return <Img  fluid={image.node.square} />;
        }
      }}
    />
  )
};

This only actually works 50% of the time even if the product key is in the S3 bucket, maybe something to do with this misuse of static query or that there is thousands of images. I’m hoping that if I can attach the image as a File node inside gatsby-node, then I can delete this ‘find and includes’ way of getting my product image.

So my question, how do I attach the image as a File node for each product item from a graphql request rather than on the creation of a MarkdownRemark type?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
LpmRavencommented, Apr 7, 2020

Great, thanks @Js-Brecht! Got that all working, I can’t thank you enough.

0reactions
rwchampincommented, Jul 12, 2020

Hey guys!

Ive been stuck on this for WEEEEKSSSSS. Thank you SO incredibly much.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Preprocessing External Images - Gatsby
This guide will show you how to use the createRemoteFileNode process and get the same benefits of gatsby-transformer-sharp with externally sourced images.
Read more >
How to boost your performance with serverless architectures
To do that, click on “SNS” in the list of triggers, select “image-preprocessing” from the SNS topic list, and click “add.” Finally we...
Read more >
Writing Images to s3fs.S3FileSystem after preprocessing image
S3FileSystem after preprocessing image ... I can retrieve an image from the s3 bucket as defined above and preprocess them using
Read more >
Analyzing images stored in an Amazon S3 bucket
Amazon Rekognition Image can analyze images that are stored in an Amazon S3 bucket or images that are supplied as image bytes.
Read more >
Using AWS S3 to Store Static Assets and File Uploads
In a pass-through upload, a file uploads to your app, which in turn uploads it to S3. This method enables you to perform...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found