parse-server-example icon indicating copy to clipboard operation
parse-server-example copied to clipboard

Uploading files to S3 with FileAdapter

Open ZacharyKhan opened this issue 10 years ago • 17 comments

Im trying to store files to parse the way I have been via the iOS client, but I've set the backend up to store files in S3 (or at least I think I have).

I am running my Parse-Server on AWS ElasticBeanstalk with mLab as my DB.

I receive the following error when trying to upload a photo: [Error]: Could not store file. (Code: 130, Version: 1.12.0)

Here's a photo of my ElasticBeanstalk Configuration properties: screen shot 2016-03-20 at 6 05 54 pm

and my index.js is set up using the FileAdapter guide on GitHub

ZacharyKhan avatar Mar 21 '16 01:03 ZacharyKhan

@ZacharyKhan in your index.js are you adding the s3 file properties versus the elasticbeanstalk config properties. Since you are using express?

Should look like this:

var S3Adapter = require('parse-server').S3Adapter;

var api = new ParseServer({ databaseURI: databaseUri || 'mongodb://localhost:27017/dev', appId: process.env.APP_ID || 'APPLICATION_ID', masterKey: process.env.MASTER_KEY || 'MASTER_KEY', ... filesAdapter: new S3Adapter( "S3_ACCESS_KEY", "S3_SECRET_KEY", "S3_BUCKET", {directAccess: true} ), ... });

Once you do that. restart your app servers

https://github.com/ParsePlatform/parse-server/wiki/Configuring-File-Adapters

jdzorz avatar Mar 23 '16 15:03 jdzorz

My index.js is set up just like you have above, but replaced with my keys.

When trying to store data to mongoDB and the file to S3, I run into the same error [Error]: Could not store file. (Code: 130, Version: 1.12.0), and the entire post fails to save on MongoDB/S3. If I simply remove the adapter from index.js, files are properly stored within the MongoDB.

Here is my index.js: screen shot 2016-03-23 at 6 27 39 pm

I believe the keys are properly entered, but Parse-Server can't seem to store the data in S3 :disappointed:

ZacharyKhan avatar Mar 24 '16 01:03 ZacharyKhan

@ZacharyKhan can you go to \lib\routers\FilesRouter.js and in line 108 where you see this:

next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'Could not store file.')); change to: next(new _node.Parse.Error(_node.Parse.Error.FILE_SAVE_ERROR, 'Could not store file.' + error));

this should give you any S3 errors. that might be occurring on that end. You'll have to restart EB app server once you make the change.

jdzorz avatar Mar 24 '16 14:03 jdzorz

Where exactly might I find the \lib\routers\FilesRouter.js? There is no lib folder in my Parse-Server.

ZacharyKhan avatar Apr 04 '16 22:04 ZacharyKhan

@ZacharyKhan do you not see it under /var/app/current/node_modules/parse-server/lib ?

jdzorz avatar Apr 06 '16 14:04 jdzorz

I am running the Express version if that makes a difference? I dont see it

ZacharyKhan avatar Apr 07 '16 07:04 ZacharyKhan

@ZacharyKhan are you viewing the files directly from your ec2 server on elastic beanstalk? You'll have to ssh into it. Once you connect then you'll see the files under /var/app/current/node_modules/parse-server

jdzorz avatar Apr 08 '16 14:04 jdzorz

It still has problems. For me issue was S3 storage in Frankfurt. Frankfurt doesn't support old signature (It request v4 signature). Seems like S3Adapter doesn't support that;

"The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256"

xor22h avatar Apr 26 '16 09:04 xor22h

Did you make de policy on s3 ? https://github.com/ParsePlatform/parse-server/wiki/Configuring-File-Adapters#set-up-your-bucket-and-permissions

jeremypiednoel avatar May 20 '16 13:05 jeremypiednoel

Same fix as xor22h : changing the bucket's region solved the issue. Frankfurt that was automatically selected for me doesn't support the old signature. I had to create a bucket in US Standard region to get the adapter working.

jayjac avatar May 21 '16 21:05 jayjac

Dublin bucket worked well for me

jeremypiednoel avatar May 21 '16 21:05 jeremypiednoel

Anyone able to figure this issue yet? I am still get the code 130 error, and I understand its got something to do with the content-type of the file. I am manually setting it though (to 'text/csv' as the file is a csv file in my case) and still getting the following response:

Could not store file.InvalidParameterType: Expected params.ContentType to be a string

abbasdawood avatar Jun 14 '16 05:06 abbasdawood

Solved this by changing my S3 Adapter require statement from

var S3Adapter = require('parse-server-s3-adapter')

to

var S3Adapter = require('parse-server').S3Adapter

abbasdawood avatar Jun 14 '16 08:06 abbasdawood

Changing the region worked like magic!! Thank you. (I changed to Oregon)

kalyandechiraju avatar Jul 14 '16 19:07 kalyandechiraju

How do you get access to the index.js in the elasticbeanstalk? I just set up the environment variable in the dashboard following the configuration Here I upload the the file as normal using this

let post = PFObject(className: "Post")
        post["description"] = "hello world"
        do{
            post["media"] = try PFFile(data: Data(contentsOf: url))
        }catch let error as NSError{
            print(error.localizedDescription)
        }
        post.saveInBackground { (success, error) in
            if success{
                print("video saved")
            }else{
                print(error?.localizedDescription)
            }
        }
 


But the media file is saved to my mLab like this, and my S3 bucket is currently empty

{
    "_id": "Cx24LadasKH8",
    "media": "f5f1071836544asf2420632722d569c9f_file.bin",
    "description": "hello world",
    "_created_at": {
        "$date": "2017-01-26T01:52:45.141Z"
    },
    "_updated_at": {
        "$date": "2017-01-26T01:52:45.141Z"
    }
}

Can I save file to S3 just using Parse SDK, or do I have to install Amazon iOS SDK and use S3TransferManager? Can anyone point me to the right direction. I'm new to Parse and Amazon Webservice

kesongxie avatar Jan 26 '17 02:01 kesongxie

@kesongxie to connect to your elastic beanstalk you have to ssh into the ec2 instance. From there you can go to the index.js. You'll have to setup an s3 bucket with the permissions then try changing the configuration for the parse server to connect to it and save there.

jdzorz avatar Jan 26 '17 13:01 jdzorz

@jdzorz Thanks. I figured it out! What I did is download the original parse-server-example.zip and modified and re-uploaded to the dashboard(I have to go inside the folder and zip all the items together instead zip the global folder). My S3 bucket along with other configurations were set up properly by following the S3Adapter article and the article for setting up parse beanstalk from Amazon. Now I can use the pretty much the same code with PFFile and saveInBackground to save my file to the S3 and created a reference field in my mLab as well.

kesongxie avatar Jan 26 '17 20:01 kesongxie