Diaz Kautsar
Diaz Kautsar3d ago

Issue with Adding Object to S3 from Deno Deploy (Works Locally)

Hello everyone, I'm facing an issue with uploading objects to my S3 server using the AWS SDK in Deno Deploy. The same code works perfectly fine when running on my local system, but fails after being deployed to Deno Deploy. Additional Information: 1. Locally, everything works as expected, and I can upload objects to the S3 server without any issues. 2. The issue arises only after the deployment to Deno Deploy. 3. I’ve verified the environment variables (AWS_ACCESS_KEY, AWS_SECRET_KEY, AWS_ENDPOINT, AWS_REGION) and they are correctly set in Deno Deploy. Questions: 1. Is there something specific about Deno Deploy that could be causing this issue with the AWS SDK? 2. Are there known limitations or configurations needed for S3 uploads in Deno Deploy? 3. Has anyone encountered a similar issue or have suggestions on how to resolve this? Any help or guidance would be greatly appreciated. Thank you!
2 Replies
Diaz Kautsar
Diaz KautsarOP3d ago
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 }
raunioroo
raunioroo3d ago
Hi! I'm not too familiar with deploy, or aws-sdk. But I have had success using https://deno.land/x/s3@0.5.0 as a simple, lightweight library for S3. IIRC aws-sdk was at least unnecessarily huge. Can't remember if that was the reason I ditched the official aws-sdk, or was it not working with deno for some reason. Either way, the mentioned library has served me well.