-
Notifications
You must be signed in to change notification settings - Fork 0
Storing Files in AWS S3
Writing to S3 from Parse Server is as simple as configuring and using the S3 files adapter in your main Node/Express script. Here's what it should look like:
...
var S3Adapter = require('parse-server').S3Adapter;
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
appId: process.env.APP_ID || 'myAppId',
masterKey: process.env.MASTER_KEY || '',
...
filesAdapter: new S3Adapter(
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"BUCKET_NAME",
{directAccess: true}
),
...
});
Don't forget to change AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and BUCKET_NAME to their correct value.
-
Log into your AWS account or create a new one
-
Head to the S3 service and choose Create Bucket
-
Fill out a unique Bucket Name (also it should not contain any period '.' for directAccess to work) and click Create (all other defaults are OK)
-
Now head to the Identity and Access Management (IAM) service
-
Click the Users tab, then Create New User
-
Fill out at least one user name and make sure Generate an access key for each user is selected before clicking Create
-
Make sure to Download Credentials on the next screen
-
Now select the Policies tab, then Create Policy
-
Select Create Your Own Policy, fill out a Policy Name
-
Copy the following config in Policy Document, changing BUCKET_NAME for the name of the bucket you created earlier. (note: this is a little more permissive than we need, but it works for now)
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:*" ], "Resource": [ "arn:aws:s3:::BUCKET_NAME/*" ] } ] }
-
Make sure to Validate Policy first, then click Create Policy
-
Go back to the Users tab and select the user you created earlier
-
In Permissions, select Attach Policy and find the policy we just created to attach it
new S3Adapter(accessKey, secretKey, bucket, options)
Required:
- accessKey: the AWS access key for a user that has the required permissions
- secretKey: the AWS secret key for the user
- bucket: the name of your S3 bucket. Needs to be globally unique in all of S3
options
is a Javascript object (map) that can contain:
- region: the AWS region to connect to. Default: 'us-east-1'
- bucketPrefix: create all the files with the specified prefix added to the filename. Can be used to put all the files for an app in a folder with 'folder/'. Default: ''
- directAccess: whether reads are going directly to S3 or proxied through your Parse Server. Default: false