Created
June 21, 2020 03:57
-
-
Save gaboelnuevo/859f59183699788fae025df18ad2d18b to your computer and use it in GitHub Desktop.
s3 utils
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
// dependencias | |
const _ = require("lodash"); | |
const AWS = require('aws-sdk'); | |
const AmazonS3URI = require("amazon-s3-uri"); | |
const request = require("request"); | |
const accessparams = { | |
accessKeyId: "", | |
secretAccessKey: "", | |
}; | |
const s3 = new AWS.S3(accessparams); | |
const fs = require('fs'); | |
const promiseConcurrency = require("promise-concurrency"); | |
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
// leer todos los archivos de un bucket | |
const s3 = new AWS.S3(accessparams); | |
var params = { | |
Bucket: NEW_BUCKET, | |
Delimiter: '/', | |
Prefix: '' | |
}; | |
const objects = await new Promise((_resolve, _reject) => { | |
var allKeys = []; | |
function listAllKeys(marker, cb) { | |
s3.listObjects({ ...params, Marker: marker }, function(err, data) { | |
if (err) return cb(err); | |
allKeys.push(data.Contents.map((o) => o.Key)); | |
if (data.IsTruncated) | |
listAllKeys(data.NextMarker, cb); | |
else | |
cb(); | |
}); | |
} | |
listAllKeys(null, (err) => { | |
if (err) _reject(err); | |
_resolve(_.flatten(allKeys)); | |
}); | |
}); |
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
// copiar desde url publica | |
const put_from_url = (url, bucket, key, callback, force = false) => { | |
request({ | |
url: url, | |
encoding: null | |
}, function(err, res, body) { | |
if (err) | |
return callback(err, res); | |
s3.putObject({ | |
Bucket: bucket, | |
Key: key, | |
ContentType: res.headers['content-type'], | |
ContentLength: res.headers['content-length'], | |
Body: body // buffer | |
}, (err) => { | |
setTimeout(() => callback(err), 200); | |
}); | |
}); | |
} |
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
// generar url publica | |
const getSignedUrl = (url) => { | |
try { | |
const { region, bucket, key } = AmazonS3URI(url); | |
const signedUrl = s3.getSignedUrl('getObject', { | |
Bucket: bucket, | |
Key: key, | |
Expires: 60 * 25 // time in seconds: e.g. 60 * 5 = 5 mins | |
}); | |
return signedUrl; | |
} catch (_err) { | |
// console.log(_err) | |
} | |
return url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment