Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function contructOnlineAccessUrls(files, distEndpoint) {
const urls = [];
const bucketsString = await aws.s3().getObject({
Bucket: process.env.bucket,
Key: `${process.env.stackName}/workflows/buckets.json`
}).promise();
const bucketsObject = JSON.parse(bucketsString.Body);
// URLs are for public and protected files
const bucketKeys = Object.keys(bucketsObject);
files.forEach((file) => {
const urlObj = {};
const bucketkey = bucketKeys.find((bucketKey) =>
file.bucket === bucketsObject[bucketKey].name);
if (bucketsObject[bucketkey].type === 'protected') {
const extension = urljoin(bucketsObject[bucketkey].name, `${file.filepath}`);
urlObj.URL = urljoin(distEndpoint, extension);
urlObj.URLDescription = 'File to download';
async enableBucketVersioning(bucket) {
// check that the bucket has versioning enabled
const versioning = await aws.s3().getBucketVersioning({ Bucket: bucket }).promise();
// if not enabled, make it enabled
if (versioning.Status !== 'Enabled') {
aws.s3().putBucketVersioning({
Bucket: bucket,
VersioningConfiguration: { Status: 'Enabled' }
}).promise();
}
}
async enableBucketVersioning(bucket) {
// check that the bucket has versioning enabled
const versioning = await aws.s3().getBucketVersioning({ Bucket: bucket }).promise();
// if not enabled, make it enabled
if (versioning.Status !== 'Enabled') {
aws.s3().putBucketVersioning({
Bucket: bucket,
VersioningConfiguration: { Status: 'Enabled' }
}).promise();
}
}
reportStartTime,
reportEndTime
} = params;
const distributionReport = await generateDistributionReport({
reportStartTime,
reportEndTime
});
const { reportsBucket, reportsPrefix } = bucketsPrefixes();
const reportKey = await determineReportKey(DISTRIBUTION_REPORT, reportStartTime, reportsPrefix);
const s3Uri = aws.buildS3Uri(reportsBucket, reportKey);
log.info(`Uploading report to ${s3Uri}`);
return aws.s3().putObject({
Bucket: reportsBucket,
Key: reportKey,
Body: distributionReport
}).promise()
.then(() => ({ reportType: DISTRIBUTION_REPORT, file: s3Uri }));
}
// Export to support testing
async function getDistributionEventsFromS3Object(params) {
const {
Bucket,
Key
} = params;
const logLines = await aws.s3().getObject({ Bucket, Key }).promise()
.then((response) => response.Body.toString().split('\n'));
const distributionEvents = logLines
.filter(DistributionEvent.isDistributionEvent)
.map((logLine) => new DistributionEvent(logLine));
log.info(`Found ${distributionEvents.length} distribution events in s3://${Bucket}/${Key}`);
return distributionEvents;
}
async function uploadReportToS3(filename, reportBucket, reportKey) {
await aws.s3().putObject({
Bucket: reportBucket,
Key: reportKey,
Body: fs.createReadStream(filename)
}).promise();
fs.unlinkSync(filename);
const s3Uri = aws.buildS3Uri(reportBucket, reportKey);
log.info(`uploaded ${s3Uri}`);
return s3Uri;
}
async function postS3Object(destination, options) {
await aws.promiseS3Upload(
{ Bucket: destination.bucket, Key: destination.key, Body: destination.body }
);
if (options) {
const s3 = aws.s3();
await s3.deleteObject(options).promise();
}
}