Giter VIP home page Giter VIP logo

react-amplify-appsync-files-s3's Introduction

File uploads and downloads with React, AWS Amplify, AWS AppSync, and Amazon S3

This is an example project showing how to upload and download files and images using AWS Amplify, AWS AppSync, and Amazon S3

How to upload files and images with GraphQL

This project goes along with the GraphQL Tutorial - How to Manage Image & File Uploads & Downloads with AWS AppSync & AWS Amplify on Dev.to.

The question

How do I upload images and files using GraphQL with AWS AppSync?

The solution

There are a few parts to this solution:

  • You must first upload the image to a storage solution (in this example, Amazon S3)
  • After you have finished uploading the image, you will need to store a reference to this image in a database using a GraphQL mutation.
  • When you want to view a public image (public bucket), you need to:
    • Query the image URL from your database using GraphQL
  • When you want to view a private image (non-public bucket), you need to do two things:
    • First, query the image reference from your database using GraphQL
    • Get a signed URL for the image from S3

In this example, I show how to:

  1. Store images using GraphQL, AppSync, and S3 (both using public and private access)
  2. Fetch a list of images and render them in a React application
  3. Fetch a single image and render it in a React application

To deploy this app

Amplify CLI

  1. Clone the project and change into the directory
git clone https://github.com/dabit3/react-amplify-appsync-files-s3.git

cd react-amplify-appsync-s3
  1. Install the dependencies
npm install

# or

yarn
  1. Initialize and deploy the amplify project
amplify init

amplify push
  1. Run the app
npm start
  1. Change the bucket policy in your S3 bucket for files in the images folder to be public (in order for the Product images to be publicly viewable):
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::<YOUR_BUCKET_NAME>/public/images/*"
        }
    ]
}
  1. Render either the private (Users) or public (Products) example in src/App.js:
import React from 'react'
import Products from './Products'
import Users from './Users'

function App() {
  return (
    <Users />
    // or <Products />
  )
}

export default App;

Amplify Console

Click the button to deploy this application to the Amplify console.

amplifybutton

Then change the bucket policy in your S3 bucket for files in the images folder to be public (in order for the Product images to be publicly viewable):

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::<YOUR_BUCKET_NAME>/public/images/*"
        }
    ]
}

react-amplify-appsync-files-s3's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-amplify-appsync-files-s3's Issues

upload does not seem to work

Hi Nadar,
Firstly thank you very much for this tutorial.
Have looked everywhere for a working description of how to upload to AWS
I am new to AWS and trying to learn how to develop the upload process with Auth both public pictures and ones behind Auth.

I have followed your steps to build this App to learn how to upload files to AWS.
This GitHub was branched off of another page
https://dev.to/dabit3/graphql-tutorial-how-to-manage-image-file-uploads-downloads-with-aws-appsync-aws-amplify-hga
The react pages differ markedly to this one.
Seems this GitHub has a folder in it (amplify backend) that appears to corrupt the install and needs a needs a Node update from 8 to 10

I have built both using the steps laid out on both pages and although the dev.to one would not work for me, this one works for Public pictures hoping I can get some clarification to get the auth working for protected file folders so they display

Resource is not in the state stackUpdateComplete

Hello,

I tried to follow the tutorial, cloned the project, but then got an error about nodejs8 being deprecated or something.

Then I tried second time and changed nodejs8 to nodejs12.x in amplify/backend/auth/gqlimages05383c67/gqlimages05383c67-cloudformation-template.yml

But I got another error, which I guess somehow connected with creating Cognito User Pool. This is full console log:

Vladislavs-Air:test-amplify-upload vladislavsorokin$ amplify push


Current Environment: testupload

| Category | Resource name     | Operation | Provider plugin   |
| -------- | ----------------- | --------- | ----------------- |
| Auth     | gqlimages05383c67 | Create    | awscloudformation |
| Storage  | gqls3             | Create    | awscloudformation |
| Api      | gqlimages         | Create    | awscloudformation |
? Are you sure you want to continue? Yes

The following types do not have '@auth' enabled. Consider using @auth with @model
         - Product
         - User
Learn more about @auth here: https://aws-amplify.github.io/docs/cli-toolchain/graphql#auth 


GraphQL schema compiled successfully.

Edit your schema at /Users/vladislavsorokin/webprojects/test-amplify-upload/amplify/backend/api/gqlimages/schema.graphql or place .graphql files in a directory at /Users/vladislavsorokin/webprojects/test-amplify-upload/amplify/backend/api/gqlimages/schema
? Do you want to update code for your updated GraphQL API Yes
? Do you want to generate GraphQL statements (queries, mutations and subscription) based on your schema types? This will overwrite your current graphql q
ueries, mutations and subscriptions Yes
⠏ Updating resources in the cloud. This may take a few minutes...

UPDATE_ROLLBACK_IN_PROGRESS gqlimages-testupload-20200108194100 AWS::CloudFormation::Stack Wed Jan 08 2020 19:42:31 GMT+0000 (Western European Standard Time) The following resource(s) failed to create: [authgqlimages05383c67, UpdateRolesWithIDPFunctionRole, storagegqls3, apigqlimages].                                                
CREATE_FAILED               authgqlimages05383c67               AWS::CloudFormation::Stack Wed Jan 08 2020 19:42:30 GMT+0000 (Western European Standard Time) Template format error: Unrecognized resource types: [AWS::Cognito::UserPool, AWS::Cognito::IdentityPoolRoleAttachment, AWS::Cognito::UserPoolClient, AWS::Cognito::IdentityPool]
CREATE_FAILED               storagegqls3                        AWS::CloudFormation::Stack Wed Jan 08 2020 19:42:30 GMT+0000 (Western European Standard Time) Resource creation cancelled                                                                                                                                                     
CREATE_FAILED               UpdateRolesWithIDPFunctionRole      AWS::IAM::Role             Wed Jan 08 2020 19:42:30 GMT+0000 (Western European Standard Time) Resource creation cancelled                                                                                                                                                     
CREATE_IN_PROGRESS          UpdateRolesWithIDPFunctionRole      AWS::IAM::Role             Wed Jan 08 2020 19:42:30 GMT+0000 (Western European Standard Time) Resource creation Initiated                                                                                                                                                     
CREATE_IN_PROGRESS          storagegqls3                        AWS::CloudFormation::Stack Wed Jan 08 2020 19:42:30 GMT+0000 (Western European Standard Time) Resource creation Initiated                                                                                                                                                     
CREATE_FAILED               apigqlimages                        AWS::CloudFormation::Stack Wed Jan 08 2020 19:42:30 GMT+0000 (Western European Standard Time) Template format error: Unrecognized resource types: [AWS::AppSync::GraphQLApi, AWS::AppSync::GraphQLSchema, AWS::AppSync::ApiKey]                                               
CREATE_IN_PROGRESS          authgqlimages05383c67               AWS::CloudFormation::Stack Wed Jan 08 2020 19:42:29 GMT+0000 (Western European Standard Time)                                                                                                                                                                                 
CREATE_IN_PROGRESS          UpdateRolesWithIDPFunctionRole      AWS::IAM::Role             Wed Jan 08 2020 19:42:29 GMT+0000 (Western European Standard Time)                                                                                                                                                                                 
CREATE_IN_PROGRESS          apigqlimages                        AWS::CloudFormation::Stack Wed Jan 08 2020 19:42:29 GMT+0000 (Western European Standard Time)                                                                                                                                                                                 
CREATE_IN_PROGRESS          storagegqls3                        AWS::CloudFormation::Stack Wed Jan 08 2020 19:42:29 GMT+0000 (Western European Standard Time)                                                                                                                                                                                 
UPDATE_IN_PROGRESS          gqlimages-testupload-20200108194100 AWS::CloudFormation::Stack Wed Jan 08 2020 19:42:26 GMT+0000 (Western European Standard Time) User Initiated                                                                                                                                                                  
⠼ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS gqlimages-testupload-20200108194100-storagegqls3-TXL83Y38CGQ0 AWS::CloudFormation::Stack Wed Jan 08 2020 19:42:30 GMT+0000 (Western European Standard Time) User Initiated
⠇ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS S3Bucket AWS::S3::Bucket Wed Jan 08 2020 19:42:33 GMT+0000 (Western European Standard Time) 
⠋ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS S3Bucket AWS::S3::Bucket Wed Jan 08 2020 19:42:34 GMT+0000 (Western European Standard Time) Resource creation Initiated
⠦ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS S3AuthUploadPolicy AWS::IAM::Policy Wed Jan 08 2020 19:42:57 GMT+0000 (Western European Standard Time) 
CREATE_COMPLETE    S3Bucket           AWS::S3::Bucket  Wed Jan 08 2020 19:42:55 GMT+0000 (Western European Standard Time) 
⠙ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS S3AuthReadPolicy      AWS::IAM::Policy Wed Jan 08 2020 19:42:57 GMT+0000 (Western European Standard Time) 
CREATE_IN_PROGRESS S3GuestPublicPolicy   AWS::IAM::Policy Wed Jan 08 2020 19:42:57 GMT+0000 (Western European Standard Time) 
CREATE_IN_PROGRESS S3AuthPublicPolicy    AWS::IAM::Policy Wed Jan 08 2020 19:42:57 GMT+0000 (Western European Standard Time) 
CREATE_IN_PROGRESS S3AuthPrivatePolicy   AWS::IAM::Policy Wed Jan 08 2020 19:42:57 GMT+0000 (Western European Standard Time) 
CREATE_IN_PROGRESS S3AuthProtectedPolicy AWS::IAM::Policy Wed Jan 08 2020 19:42:57 GMT+0000 (Western European Standard Time) 
CREATE_IN_PROGRESS S3GuestReadPolicy     AWS::IAM::Policy Wed Jan 08 2020 19:42:57 GMT+0000 (Western European Standard Time) 
⠼ Updating resources in the cloud. This may take a few minutes...

DELETE_COMPLETE                              UpdateRolesWithIDPFunctionRole      AWS::IAM::Role             Wed Jan 08 2020 19:43:00 GMT+0000 (Western European Standard Time) 
DELETE_IN_PROGRESS                           storagegqls3                        AWS::CloudFormation::Stack Wed Jan 08 2020 19:42:58 GMT+0000 (Western European Standard Time) 
DELETE_COMPLETE                              authgqlimages05383c67               AWS::CloudFormation::Stack Wed Jan 08 2020 19:42:58 GMT+0000 (Western European Standard Time) 
DELETE_IN_PROGRESS                           UpdateRolesWithIDPFunctionRole      AWS::IAM::Role             Wed Jan 08 2020 19:42:58 GMT+0000 (Western European Standard Time) 
DELETE_COMPLETE                              apigqlimages                        AWS::CloudFormation::Stack Wed Jan 08 2020 19:42:58 GMT+0000 (Western European Standard Time) 
UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS gqlimages-testupload-20200108194100 AWS::CloudFormation::Stack Wed Jan 08 2020 19:42:58 GMT+0000 (Western European Standard Time) 
⠏ Updating resources in the cloud. This may take a few minutes...

CREATE_FAILED      S3AuthReadPolicy                                              AWS::IAM::Policy           Wed Jan 08 2020 19:42:59 GMT+0000 (Western European Standard Time) Resource creation cancelled
CREATE_FAILED      S3AuthPrivatePolicy                                           AWS::IAM::Policy           Wed Jan 08 2020 19:42:59 GMT+0000 (Western European Standard Time) Resource creation cancelled
CREATE_FAILED      S3GuestPublicPolicy                                           AWS::IAM::Policy           Wed Jan 08 2020 19:42:59 GMT+0000 (Western European Standard Time) Resource creation cancelled
CREATE_FAILED      S3AuthProtectedPolicy                                         AWS::IAM::Policy           Wed Jan 08 2020 19:42:59 GMT+0000 (Western European Standard Time) Resource creation cancelled
CREATE_FAILED      S3AuthPublicPolicy                                            AWS::IAM::Policy           Wed Jan 08 2020 19:42:59 GMT+0000 (Western European Standard Time) Resource creation cancelled
CREATE_FAILED      S3GuestReadPolicy                                             AWS::IAM::Policy           Wed Jan 08 2020 19:42:59 GMT+0000 (Western European Standard Time) Resource creation cancelled
CREATE_FAILED      S3AuthUploadPolicy                                            AWS::IAM::Policy           Wed Jan 08 2020 19:42:59 GMT+0000 (Western European Standard Time) Resource creation cancelled
DELETE_IN_PROGRESS gqlimages-testupload-20200108194100-storagegqls3-TXL83Y38CGQ0 AWS::CloudFormation::Stack Wed Jan 08 2020 19:42:59 GMT+0000 (Western European Standard Time) User Initiated             
CREATE_IN_PROGRESS S3AuthPublicPolicy                                            AWS::IAM::Policy           Wed Jan 08 2020 19:42:58 GMT+0000 (Western European Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS S3AuthProtectedPolicy                                         AWS::IAM::Policy           Wed Jan 08 2020 19:42:58 GMT+0000 (Western European Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS S3GuestReadPolicy                                             AWS::IAM::Policy           Wed Jan 08 2020 19:42:58 GMT+0000 (Western European Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS S3AuthReadPolicy                                              AWS::IAM::Policy           Wed Jan 08 2020 19:42:58 GMT+0000 (Western European Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS S3GuestPublicPolicy                                           AWS::IAM::Policy           Wed Jan 08 2020 19:42:58 GMT+0000 (Western European Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS S3AuthUploadPolicy                                            AWS::IAM::Policy           Wed Jan 08 2020 19:42:58 GMT+0000 (Western European Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS S3AuthPrivatePolicy                                           AWS::IAM::Policy           Wed Jan 08 2020 19:42:58 GMT+0000 (Western European Standard Time) Resource creation Initiated
⠦ Updating resources in the cloud. This may take a few minutes...

DELETE_COMPLETE    gqlimages-testupload-20200108194100-storagegqls3-TXL83Y38CGQ0 AWS::CloudFormation::Stack Wed Jan 08 2020 19:43:29 GMT+0000 (Western European Standard Time) 
DELETE_SKIPPED     S3Bucket                                                      AWS::S3::Bucket            Wed Jan 08 2020 19:43:29 GMT+0000 (Western European Standard Time) 
DELETE_COMPLETE    S3AuthUploadPolicy                                            AWS::IAM::Policy           Wed Jan 08 2020 19:43:28 GMT+0000 (Western European Standard Time) 
DELETE_COMPLETE    S3GuestReadPolicy                                             AWS::IAM::Policy           Wed Jan 08 2020 19:43:28 GMT+0000 (Western European Standard Time) 
DELETE_COMPLETE    S3AuthPublicPolicy                                            AWS::IAM::Policy           Wed Jan 08 2020 19:43:28 GMT+0000 (Western European Standard Time) 
DELETE_COMPLETE    S3AuthPrivatePolicy                                           AWS::IAM::Policy           Wed Jan 08 2020 19:43:28 GMT+0000 (Western European Standard Time) 
DELETE_COMPLETE    S3GuestPublicPolicy                                           AWS::IAM::Policy           Wed Jan 08 2020 19:43:28 GMT+0000 (Western European Standard Time) 
DELETE_COMPLETE    S3AuthProtectedPolicy                                         AWS::IAM::Policy           Wed Jan 08 2020 19:43:28 GMT+0000 (Western European Standard Time) 
DELETE_COMPLETE    S3AuthReadPolicy                                              AWS::IAM::Policy           Wed Jan 08 2020 19:43:28 GMT+0000 (Western European Standard Time) 
DELETE_IN_PROGRESS S3GuestReadPolicy                                             AWS::IAM::Policy           Wed Jan 08 2020 19:43:27 GMT+0000 (Western European Standard Time) 
DELETE_IN_PROGRESS S3AuthPrivatePolicy                                           AWS::IAM::Policy           Wed Jan 08 2020 19:43:27 GMT+0000 (Western European Standard Time) 
DELETE_IN_PROGRESS S3AuthPublicPolicy                                            AWS::IAM::Policy           Wed Jan 08 2020 19:43:27 GMT+0000 (Western European Standard Time) 
DELETE_IN_PROGRESS S3AuthUploadPolicy                                            AWS::IAM::Policy           Wed Jan 08 2020 19:43:27 GMT+0000 (Western European Standard Time) 
DELETE_IN_PROGRESS S3AuthProtectedPolicy                                         AWS::IAM::Policy           Wed Jan 08 2020 19:43:27 GMT+0000 (Western European Standard Time) 
DELETE_IN_PROGRESS S3AuthReadPolicy                                              AWS::IAM::Policy           Wed Jan 08 2020 19:43:27 GMT+0000 (Western European Standard Time) 
DELETE_IN_PROGRESS S3GuestPublicPolicy                                           AWS::IAM::Policy           Wed Jan 08 2020 19:43:27 GMT+0000 (Western European Standard Time) 
⠋ Updating resources in the cloud. This may take a few minutes...

UPDATE_ROLLBACK_COMPLETE gqlimages-testupload-20200108194100 AWS::CloudFormation::Stack Wed Jan 08 2020 19:43:33 GMT+0000 (Western European Standard Time) 
DELETE_COMPLETE          storagegqls3                        AWS::CloudFormation::Stack Wed Jan 08 2020 19:43:32 GMT+0000 (Western European Standard Time) 
⠸ Updating resources in the cloud. This may take a few minutes...Error updating cloudformation stack
⠼ Updating resources in the cloud. This may take a few minutes...

Following resources failed

Resource Name: gqlimages-20200108194100-authRole-idp (AWS::IAM::Role)
Event Type: create
Reason: Resource creation cancelled


Resource Name: gqlim-S3Au-1U8A75MRMTEY3 (AWS::IAM::Policy)
Event Type: create
Reason: Resource creation cancelled


Resource Name: gqlim-S3Au-1PU7JLM4XTGPI (AWS::IAM::Policy)
Event Type: create
Reason: Resource creation cancelled


Resource Name: gqlim-S3Gu-SL82M8PBEHCR (AWS::IAM::Policy)
Event Type: create
Reason: Resource creation cancelled


Resource Name: gqlim-S3Au-198WSG4PEMDRS (AWS::IAM::Policy)
Event Type: create
Reason: Resource creation cancelled


Resource Name: gqlim-S3Au-1WN48G68XIR5O (AWS::IAM::Policy)
Event Type: create
Reason: Resource creation cancelled


Resource Name: gqlim-S3Gu-1K21QOCZXRJIP (AWS::IAM::Policy)
Event Type: create
Reason: Resource creation cancelled


Resource Name: gqlim-S3Au-NCHJEQ4ZWX9S (AWS::IAM::Policy)
Event Type: create
Reason: Resource creation cancelled


✖ An error occurred when pushing the resources to the cloud

Resource is not in the state stackUpdateComplete

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.