S3 Fix
This commit is contained in:
@@ -94,14 +94,4 @@ export const SERVICES: Record<
|
||||
};
|
||||
|
||||
export const BASE_URI = process.env.NODE_ENV == "production" ? "https://online-energieausweis.org" : "http://localhost:3000";
|
||||
export const LEX_OFFICE_API_KEY = process.env.NODE_ENV == "production" ? "iwQLCU_ZAq6bVV7hmR8RO8MiC8Q" : "znjmkmbA3Hbx9dC7wdKp7TnOf1pcRl_tCUwEBZys7bj-QRPG"
|
||||
|
||||
export const s3Client = new S3Client({
|
||||
region: "eu-central-1",
|
||||
endpoint: "https://s3-eu-central-1.ionoscloud.com",
|
||||
credentials: {
|
||||
accessKeyId: "EEAAAAHYzkBh2aRNC4OEVfqCRCviXoIZ3wm9UieAVCeLbWnJrQAAAAECGy6_AAAAAAIbLr9zLHhcZE7kGJngOPTFoODh",
|
||||
secretAccessKey: "zlRI0jK+A8CIxir0QPdXNFUV+L9XjFTGyBvdUT1dvgY4FNlsOJgNJ+5xW5oShzmy",
|
||||
},
|
||||
forcePathStyle: true,
|
||||
});
|
||||
export const LEX_OFFICE_API_KEY = process.env.NODE_ENV == "production" ? "iwQLCU_ZAq6bVV7hmR8RO8MiC8Q" : "znjmkmbA3Hbx9dC7wdKp7TnOf1pcRl_tCUwEBZys7bj-QRPG"
|
||||
44
src/lib/s3.ts
Normal file
44
src/lib/s3.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
||||
import * as fs from "fs";
|
||||
|
||||
export const s3Client = new S3Client({
|
||||
region: "eu-central-1",
|
||||
endpoint: "https://s3-eu-central-1.ionoscloud.com",
|
||||
credentials: {
|
||||
accessKeyId:
|
||||
"EEAAAAHYzkBh2aRNC4OEVfqCRCviXoIZ3wm9UieAVCeLbWnJrQAAAAECGy6_AAAAAAIbLr9zLHhcZE7kGJngOPTFoODh",
|
||||
secretAccessKey:
|
||||
"zlRI0jK+A8CIxir0QPdXNFUV+L9XjFTGyBvdUT1dvgY4FNlsOJgNJ+5xW5oShzmy",
|
||||
},
|
||||
forcePathStyle: true,
|
||||
});
|
||||
|
||||
export async function getS3File(
|
||||
bucket: string,
|
||||
key: string
|
||||
): Promise<Buffer | null> {
|
||||
try {
|
||||
let command = new GetObjectCommand({ Bucket: bucket, Key: key });
|
||||
let response = await s3Client.send(command);
|
||||
|
||||
const body = response.Body;
|
||||
|
||||
if (!body) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let buffer = await streamToBuffer(body as unknown as fs.ReadStream);
|
||||
return buffer;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function streamToBuffer(stream: fs.ReadStream): Promise<Buffer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const chunks: any[] = [];
|
||||
stream.on("data", (chunk) => chunks.push(chunk));
|
||||
stream.on("error", reject);
|
||||
stream.on("end", () => resolve(Buffer.concat(chunks)));
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user