You can upload files to AWS S3 using AWS AppSync by following these steps:
In the AWS AppSync console, choose the Data sources tab.
Choose the Add data source button.
In the Add data source screen, choose Amazon S3 for the Data source type.
Enter a Data source name. This is the name that you'll use to refer to this data source in your AWS AppSync API.
For the IAM role, choose the role that you created in Step 1: Create an IAM Role for AppSync.
Choose the check mark icon to create the data source.
In the left navigation panel, choose the Schema tab.
Choose the Edit button.
In the schema editor, enter the following type definition:
type S3Object @aws_api_key_required {
bucket: String!
key: String!
region: String
localUri: String
}
Choose the Save button.
In the left navigation panel, choose the Queries tab.
Choose the New query button.
In the New query screen, do the following:
a. For Operation name, enter GetS3Object.
b. Choose the S3Object type from the Type dropdown list.
c. Enter the following for the Query:
query GetS3Object($bucket: String!, $key: String!) {
getS3Object(bucket: $bucket, key: $key) {
bucket
key
region
localUri
}
}
d. Choose the check mark icon to save the query.
In the Queries tab, choose the GetS3Object query that you just created.
In the Query editor, enter the following values for the Query variables:
{
"bucket": "my-bucket",
"key": "my-file.txt"
}
Choose the play icon to execute the query.
The results of the query are displayed in the Result pane.
To upload a file to AWS S3 using AWS AppSync, you can use the mutation. In the AWS AppSync console, choose the Mutations tab.
Choose the New mutation button.
In the New mutation screen, do the following:
a. For Operation name, enter PutS3Object.
b. Choose the S3Object type from the Type dropdown list.
c. Enter the following for the Query:
mutation PutS3Object($bucket: String!, $key: String!, $region: String!, $localUri: String!) {
putS3Object(bucket: $bucket, key: $key, region: $region, localUri: $localUri) {
bucket
key
region
localUri
}
}
d. Choose the check mark icon to save the mutation.
In the Mutations tab, choose the PutS3Object mutation that you just created.
In the Mutation editor, enter the following values for the Mutation variables:
{
"bucket": "my-bucket",
"key": "my-file.txt",
"region": "us-east-1",
"localUri": "/local/path/to/my-file.txt"
}
Choose the play icon to execute the mutation.
The results of the mutation are displayed in the Result pane.