Created
July 9, 2019 09:07
-
-
Save giusecapo/d2d0bd2ef115e4684506af52a3aa0766 to your computer and use it in GitHub Desktop.
Create custom PFFile with Amazon AWS Cloudfront
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express(); | |
var Parse = require("parse/node"); | |
var bodyParser = require('body-parser'); | |
const pug = require('pug'); | |
var multer = require('multer'); | |
var upload = multer({ dest: __dirname + '/tmp/' }); | |
// Init Parse | |
Parse.serverURL = 'YOURSERVERURL' | |
Parse.initialize("YOURAPPID", "YOURAPPJSKEY", "YOURAPPMASTERKEY") | |
app.use(express.static(__dirname + '/public')); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ | |
extended: true | |
})); | |
app.get('/', function(req, res) { | |
res.send(pug.renderFile('templates/index.pug')); | |
}) | |
app.get('/uploadFile', function(req, res) { | |
uploadPFFile(res) | |
}) | |
var port = process.env.PORT || 8282; | |
app.listen(port, function() { | |
console.log('Example app listening on port 8282!'); | |
}); | |
// MARK: - Uplaod a file (image) on S3 with s3-uploader and create PFObject with PFFile | |
// INSTRUCTION | |
// You must create an object having: | |
// "__type":"File", | |
// "name":"nameofthefile.extension", | |
// "url":"amazoncloudfronturl/nameofthefile.extension" | |
function createPFFile(name, awsKey){ | |
var file = Object() | |
file.__type = "File" | |
file.name = name | |
file.url = "yourcloudfronturl" + awsKey | |
return file | |
} | |
var AWS = require('aws-sdk'); | |
var Upload = require('s3-uploader'); | |
// This function upload a file on AWS and save the PFObject | |
function uploadPFFile(res){ | |
var fileURL = __dirname + '/public/img/example.jpeg' | |
var client = new Upload('awsBucketName', { | |
aws: { | |
path: 'images/', | |
region: 'eu-west-2', | |
acl: 'public-read', | |
signatureVersion: 'v4', | |
accessKeyId:"accessKeyId", | |
secretAccessKey:"secretAccessKeys" | |
}, | |
cleanup: { | |
versions: true, | |
original: true | |
}, | |
versions: [{ | |
maxHeight: 500, | |
format: 'jpg', | |
suffix: '-normal', | |
quality: 70, | |
awsImageExpires: 31536000, | |
awsImageMaxAge: 31536000 | |
},{ | |
maxHeight: 320, | |
format: 'jpg', | |
suffix: '-thumb', | |
quality: 80, | |
awsImageExpires: 31536000, | |
awsImageMaxAge: 31536000 | |
}] | |
}); | |
client.upload(fileURL, {}, function(err, versions, meta) { | |
if (err) { throw err; } | |
else { | |
let awsKey = versions[0].key | |
let file = createPFFile("myfile", awsKey) | |
let images = [file] | |
var parseObject = new YourCustomParseClass(); | |
// .. Any other property | |
parseObject.set("images", images); | |
parseObject.save(null, { | |
success: function(service) { | |
res.send("success") | |
}, | |
error: function(service, error) { | |
res.send(error) | |
} | |
}); | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I created this script to quickly create custom PFFiles using AWS Cloudfront.
Since Parse.com has been shut down, now we have to setup our custom Parse Server (anyway I use Heroku for my projects, it's amazing).
I loved PFFiles on Parse.com, because they provide the simplest and best way (for me) to cache images and files, using the iOS and Android Parse SDK (setting the CachePolicy with zero effort was super).
Now, with this lil' trick, I'm able to use PFFiles with Amazon Cloudfront: this helps me to achieve the best UX possible!