Diaz Kautsar
Issue with Adding Object to S3 from Deno Deploy (Works Locally)
here’s the code I’m using:
import { AWS_ACCES_KEY, AWS_ENDPOINT, AWS_REGION, AWS_SECRET_KEY } from "../constants/index.ts";
import {
PutObjectCommand,
PutObjectCommandOutput,
S3Client
} from "@aws-sdk/client-s3"
class S3ClientLibs {
AWS_ACCES_KEY: string;
AWS_SECRET_KEY: string;
AWS_ENDPOINT: string;
AWS_REGION: string;
s3Instance: S3Client | null;
constructor() {
this.AWS_ACCES_KEY = Deno.env.get(AWS_ACCES_KEY) as string;
this.AWS_SECRET_KEY = Deno.env.get(AWS_SECRET_KEY) as string;
this.AWS_ENDPOINT = Deno.env.get(AWS_ENDPOINT) as string;
this.AWS_REGION = Deno.env.get(AWS_REGION) as string;
this.s3Instance = null;
}
createInstance(): S3Client {
if (!this.s3Instance) {
this.s3Instance = new S3Client({
credentials: {
accessKeyId: this.AWS_ACCES_KEY,
secretAccessKey: this.AWS_SECRET_KEY,
},
region: this.AWS_REGION,
endpoint: this.AWS_ENDPOINT,
});
}
return this.s3Instance;
}
async addObject(
Bucket: string,
Key: string,
Body: any,
instance?: S3Client,
): Promise<PutObjectCommandOutput> {
try {
const s3 = instance ? instance : await this.createInstance();
const command = new PutObjectCommand({
Bucket,
Key,
Body,
});
const response = await s3.send(command);
return response;
} catch (error) {
throw error
}
}
}
const s3 = new S3ClientLibs()
s3.createInstance()
export { s3 }
import { AWS_ACCES_KEY, AWS_ENDPOINT, AWS_REGION, AWS_SECRET_KEY } from "../constants/index.ts";
import {
PutObjectCommand,
PutObjectCommandOutput,
S3Client
} from "@aws-sdk/client-s3"
class S3ClientLibs {
AWS_ACCES_KEY: string;
AWS_SECRET_KEY: string;
AWS_ENDPOINT: string;
AWS_REGION: string;
s3Instance: S3Client | null;
constructor() {
this.AWS_ACCES_KEY = Deno.env.get(AWS_ACCES_KEY) as string;
this.AWS_SECRET_KEY = Deno.env.get(AWS_SECRET_KEY) as string;
this.AWS_ENDPOINT = Deno.env.get(AWS_ENDPOINT) as string;
this.AWS_REGION = Deno.env.get(AWS_REGION) as string;
this.s3Instance = null;
}
createInstance(): S3Client {
if (!this.s3Instance) {
this.s3Instance = new S3Client({
credentials: {
accessKeyId: this.AWS_ACCES_KEY,
secretAccessKey: this.AWS_SECRET_KEY,
},
region: this.AWS_REGION,
endpoint: this.AWS_ENDPOINT,
});
}
return this.s3Instance;
}
async addObject(
Bucket: string,
Key: string,
Body: any,
instance?: S3Client,
): Promise<PutObjectCommandOutput> {
try {
const s3 = instance ? instance : await this.createInstance();
const command = new PutObjectCommand({
Bucket,
Key,
Body,
});
const response = await s3.send(command);
return response;
} catch (error) {
throw error
}
}
}
const s3 = new S3ClientLibs()
s3.createInstance()
export { s3 }
3 replies