File: /var/www/javago-nodeserver-hotfixes/src/helper/presigned.url.js
import { config } from "../config/config.js";
import { HttpRequest } from "@smithy/protocol-http";
import { S3RequestPresigner } from "@aws-sdk/s3-request-presigner";
import { Hash } from "@smithy/hash-node";
import { formatUrl } from "@aws-sdk/util-format-url";
const credentials = {
accessKeyId: config.AWS_SES_ACCESS_KEYID,
secretAccessKey: config.AWS_SES_SECRET_ACCESS_KEY
}
const presigned = new S3RequestPresigner({
credentials,
region: config.AWS_SES_REGION,
sha256: Hash.bind(null, "sha256"), // In Node.js
//sha256: Sha256 // In browsers
});
// Create a GET request from S3 url.
export const getPreSignedUrl = async (file_url) => {
try {
if (file_url == '' || file_url == null) {
return ''
}
const url = await presigned.presign(new HttpRequest(file_url));
return formatUrl(url)
} catch (error) {
throw new Error(error);
}
}