Skip to content

Commit

Permalink
testing streaming directly from bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
enapupe committed Nov 5, 2023
1 parent eddb78b commit d10af5a
Show file tree
Hide file tree
Showing 3 changed files with 422 additions and 11 deletions.
29 changes: 21 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ const fetch = (...args) =>
import("node-fetch").then(({ default: fetch }) => fetch(...args));
const express = require("express");
const sharp = require("sharp");
const { Storage, ApiError } = require("@google-cloud/storage");

const storage = new Storage();
const app = express();

const PORT = 8080;
const HOST = "0.0.0.0";
const BASE_STORAGE_IMAGE_URL = "https://storage.googleapis.com/";
Expand All @@ -24,7 +27,7 @@ app.get("/healthy", (req, res) => {

app.get("*", async (req, res) => {
try {
const { searchParams, pathname, href } = new URL(
const { searchParams, pathname } = new URL(
`${BASE_STORAGE_IMAGE_URL}${BUCKET}${req.url}`,
);

Expand All @@ -39,19 +42,29 @@ app.get("*", async (req, res) => {
const height = Number(searchParams.get("h")) || undefined;
const format = getFormat(webp, avif);

const { data, status } = await getImage(href);
if (status > 399) {
return res
.status(415)
.send("upstream server did not respond with a valid status code");
}
const fileBucketName = pathname.replace(BUCKET, "").slice(2);

res
.set("Cache-Control", "public, max-age=15552000")
.set("Vary", "Accept")
.type(`image/${format}`);

sharp(data)
const pipeline = sharp();

storage
.bucket(BUCKET)
.file(fileBucketName)
.createReadStream()
.on("error", function (e) {
if (e instanceof ApiError) {
if (e.message?.includes("No such object"))
return res.status(404).end();
}
return res.status(500).send(JSON.stringify(e));
})
.pipe(pipeline);

pipeline
.rotate()
.resize({ width, height })
.toFormat(format, { effort: 3, quality, progressive: true })
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "openbeta.io",
"license": "MIT",
"dependencies": {
"@google-cloud/storage": "^7.5.0",
"express": "^4.18.2",
"node-fetch": "^3.3.2",
"sharp": "^0.32.6"
Expand Down
Loading

0 comments on commit d10af5a

Please sign in to comment.