Skip to content

Commit 2ebb418

Browse files
authored
Merge pull request #242 from commandeer/resize-image
sst image resize
2 parents fe79464 + 842ea8e commit 2ebb418

File tree

52 files changed

+1253619
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1253619
-0
lines changed
6 KB
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# These variables are only available in your SST code.
2+
# To apply them to your Lambda functions, checkout this doc - https://docs.serverless-stack.com/environment-variables#environment-variables-in-lambda-functions
3+
4+
MY_ENV_VAR=i-am-an-environment-variable
6 KB
Binary file not shown.
10 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"use strict";
2+
var __create = Object.create;
3+
var __defProp = Object.defineProperty;
4+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5+
var __getOwnPropNames = Object.getOwnPropertyNames;
6+
var __getProtoOf = Object.getPrototypeOf;
7+
var __hasOwnProp = Object.prototype.hasOwnProperty;
8+
var __export = (target, all) => {
9+
for (var name in all)
10+
__defProp(target, name, { get: all[name], enumerable: true });
11+
};
12+
var __copyProps = (to, from, except, desc) => {
13+
if (from && typeof from === "object" || typeof from === "function") {
14+
for (let key of __getOwnPropNames(from))
15+
if (!__hasOwnProp.call(to, key) && key !== except)
16+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17+
}
18+
return to;
19+
};
20+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
21+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22+
23+
// backend/functions/resize.ts
24+
var resize_exports = {};
25+
__export(resize_exports, {
26+
handler: () => handler
27+
});
28+
module.exports = __toCommonJS(resize_exports);
29+
var import_aws_sdk = __toESM(require("aws-sdk"), 1);
30+
var import_sharp = __toESM(require("sharp"), 1);
31+
var import_stream = __toESM(require("stream"), 1);
32+
var width = 800;
33+
var prefixPhotos = "photos";
34+
var prefixOutput = "resized-photos";
35+
var S3 = new import_aws_sdk.default.S3();
36+
function readStreamFromS3(bucket, key) {
37+
console.log(`readStreamFromS3: ${bucket}, ${key}`);
38+
return S3.getObject({
39+
Bucket: bucket,
40+
Key: key
41+
}).createReadStream();
42+
}
43+
function writeStreamToS3(bucket, key) {
44+
console.log(`writeStreamToS3: ${bucket}, ${key}`);
45+
const pass = new import_stream.default.PassThrough();
46+
return {
47+
writeStream: pass,
48+
upload: S3.upload({
49+
Key: key,
50+
Bucket: bucket,
51+
Body: pass
52+
}).promise()
53+
};
54+
}
55+
function streamToSharp(width2) {
56+
return (0, import_sharp.default)().resize(width2);
57+
}
58+
var handler = async (event) => {
59+
const s3Record = event.Records[0].s3;
60+
const key = s3Record.object.key;
61+
const bucket = s3Record.bucket.name;
62+
console.log(`key: ${key}`);
63+
console.log(`bucket: ${bucket}`);
64+
if (!key.startsWith(prefixPhotos)) {
65+
console.log("exiting, this is not a /photos object");
66+
return false;
67+
}
68+
const readStream = readStreamFromS3(bucket, key);
69+
const resizeStream = streamToSharp(width);
70+
let newKey = `${prefixOutput}/${key.substring(key.lastIndexOf("/") + 1)}`;
71+
console.log(`newKey: ${newKey}`);
72+
const { writeStream, upload } = writeStreamToS3(bucket, newKey);
73+
readStream.pipe(resizeStream).pipe(writeStream);
74+
await upload;
75+
return true;
76+
};
77+
// Annotate the CommonJS export names for ESM import in node:
78+
0 && (module.exports = {
79+
handler
80+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"type":"commonjs"}

0 commit comments

Comments
 (0)