Last active
March 30, 2018 16:51
-
-
Save brianberlin/a32196bd930e73f6bb4dcb551d649fdb to your computer and use it in GitHub Desktop.
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
require('dotenv').config() | |
const AWS = require('aws-sdk') | |
const fs = require('fs') | |
const async = require('async') | |
const Bucket = process.env.AWS_BUCKET | |
const s3 = new AWS.S3({ | |
accessKeyId: process.env.ACCESS_KEY_ID, | |
secretAccessKey: process.env.SECRET_ACCESS_KEY | |
}) | |
async function updateObject({ Key }) { | |
if (Key === 'uploads/') return | |
if (Key.indexOf('pdf') !== Key.length - 3) return | |
const objectData = await s3.headObject({ | |
Key, | |
Bucket | |
}).promise() | |
if (objectData.ContentType === 'application/pdf') return | |
const { Body } = await s3.getObject({ | |
Key, | |
Bucket | |
}).promise() | |
// backup in case | |
fs.writeFileSync(`${__dirname}/${Key}`, Body) | |
return s3.putObject({ | |
Bucket, | |
Key, | |
ContentType: 'application/pdf', | |
Body: Body | |
}).promise() | |
} | |
async function go(Marker) { | |
const objects = await s3.listObjects({ | |
Bucket, | |
Marker, | |
Prefix: 'uploads/' | |
}).promise() | |
let x = 0 | |
const process = (object, cb) => { | |
console.log(x++, object.Key) | |
updateObject(object).then(() => cb()).catch(cb) | |
} | |
async.eachLimit(objects.Contents, 10, process, err => { | |
if (err) throw err | |
if (objects.Contents.length === 1000) { | |
go(objects.Contents[objects.Contents.length - 1].Key) | |
} else { | |
console.log('done') | |
process.exit() | |
} | |
}) | |
} | |
go() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment