Created
January 31, 2018 21:12
-
-
Save sebabouche/aba54dfcf0938c9e07f22876f3ff818a to your computer and use it in GitHub Desktop.
aws and filepreviews.io
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
import aws from "aws-sdk" | |
import shortid from "shortid" | |
import FilePreviews from "filepreviews" | |
import { | |
REACT_APP_S3_ACCESS_KEY_ID, | |
REACT_APP_S3_SECRET_KEY, | |
REACT_APP_S3_REGION, | |
REACT_APP_S3_BUCKET_NAME, | |
REACT_APP_FILEPREVIEWS_API_KEY, | |
REACT_APP_FILEPREVIEWS_API_SECRET, | |
} from "../../../../../config/constants" | |
aws.config.update({ | |
region: REACT_APP_S3_REGION, | |
accessKeyId: REACT_APP_S3_ACCESS_KEY_ID, | |
secretAccessKey: REACT_APP_S3_SECRET_KEY, | |
}) | |
const previews = new FilePreviews({ | |
debug: true, | |
apiKey: REACT_APP_FILEPREVIEWS_API_KEY, | |
apiSecret: REACT_APP_FILEPREVIEWS_API_SECRET, | |
}) | |
const awsUploader = (file, userId) => { | |
// console.log("file", file) | |
const extension = file.name.split('.').pop(); | |
// console.log("extension", extension) | |
const fileName = `${userId}-${shortid.generate()}.${extension}` | |
const previewName = `${userId}-${shortid.generate()}.preview.${extension}` | |
const s3 = new aws.S3({ params: { Bucket: REACT_APP_S3_BUCKET_NAME } }) | |
s3.upload({ | |
Key: fileName, | |
Body: file, | |
ACL: "public-read", | |
}, (err, data) => { | |
if (err) { | |
console.log("error", err) | |
} | |
if (data) { | |
console.log("Upload successful:", data) | |
previews.generate(data.Location, (generateError, generateResult) => { | |
if (generateError) { | |
console.log(generateError) | |
} | |
console.log(generateResult.id) | |
console.log(generateResult.status) | |
previews.retrieve(generateResult.id, (retrieveError, retrieveResult) => { | |
console.log(retrieveResult) | |
}) | |
}) | |
} | |
}) | |
} | |
export default awsUploader |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment