Is anyone using `"@aws-sdk/client-s3"` ?

Is anyone using "@aws-sdk/client-s3" ? I currently do:
const s3Client = new S3Client({
region: "auto",
endpoint: `https://${process.env.CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: process.env.CLOUDFLARE_R2_BUCKET_ACCESS_KEY_ID,
secretAccessKey: process.env.CLOUDFLARE_R2_BUCKET_SECRET_KEY_ID,
},
})
const s3Client = new S3Client({
region: "auto",
endpoint: `https://${process.env.CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: process.env.CLOUDFLARE_R2_BUCKET_ACCESS_KEY_ID,
secretAccessKey: process.env.CLOUDFLARE_R2_BUCKET_SECRET_KEY_ID,
},
})
Any my workers throws a 500 with: Cannot convert object to primitive value
38 Replies
harshil1712
harshil17125mo ago
I have a similar code for my pages function, and it is working fine.
const S3 = new S3Client({
region: "auto",
endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: ACCESS_KEY_ID,
secretAccessKey: SECRET_ACCESS_KEY,
},
});
const S3 = new S3Client({
region: "auto",
endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: ACCESS_KEY_ID,
secretAccessKey: SECRET_ACCESS_KEY,
},
});
Are you getting this error when running any operation?
cosbgn
cosbgnOP5mo ago
Yes this is just at the beginning of my code, so before any op I also double checked all env vars and they are logged correctly You import it like this right: import { S3Client } from "@aws-sdk/client-s3";
harshil1712
harshil17125mo ago
Yes. This is the GitHub repo for your reference: https://github.com/harshil1712/nextjs-r2-demo/blob/main/lib/r2.ts
GitHub
nextjs-r2-demo/lib/r2.ts at main · harshil1712/nextjs-r2-demo
Contribute to harshil1712/nextjs-r2-demo development by creating an account on GitHub.
cosbgn
cosbgnOP5mo ago
Maybe I need nodejs_compat it's the only difference I see Nope same error. So weird! tried also by pinning v3.600
Hard@Work
Hard@Work5mo ago
Are all of your bindings strings? That's my first guess, that for some reason, it is being given an object that it tries(and fails) to cast into a string
cosbgn
cosbgnOP5mo ago
I don't use bindings
Hard@Work
Hard@Work5mo ago
For process.env?
cosbgn
cosbgnOP5mo ago
Those are env variables
cosbgn
cosbgnOP5mo ago
Set here
No description
Hard@Work
Hard@Work5mo ago
Hm... running it locally, does it throw the same error?
cosbgn
cosbgnOP5mo ago
No locally it works perfectly also, when I do:
await log(`${process.env.CLOUDFLARE_ACCOUNT_ID.substring(0,5)}-${process.env.CLOUDFLARE_R2_BUCKET_ACCESS_KEY_ID.substring(0,5)}-${process.env.CLOUDFLARE_R2_BUCKET_SECRET_KEY_ID.substring(0,5)}`)
await log(`${process.env.CLOUDFLARE_ACCOUNT_ID.substring(0,5)}-${process.env.CLOUDFLARE_R2_BUCKET_ACCESS_KEY_ID.substring(0,5)}-${process.env.CLOUDFLARE_R2_BUCKET_SECRET_KEY_ID.substring(0,5)}`)
I get the strings as expected, also in prod
Hard@Work
Hard@Work5mo ago
Does the error show which exact line it is failing on?
cosbgn
cosbgnOP5mo ago
Yes but it's minified so it's messed up. But I tried with console.log(1) and console.log(2) just before and after new S3Client({ and log(2) doesn't show up so it's somewhere in new S3Client()
Hard@Work
Hard@Work5mo ago
Yeah, I'm trying to figure out whether it is failing on the endpoint, or the credentials Let me try spinning a repro, and seeing if I can replicate your error
cosbgn
cosbgnOP5mo ago
No description
Hard@Work
Hard@Work5mo ago
Wait, it is Nuxt... Not Next Ugh, rebuilding
cosbgn
cosbgnOP5mo ago
nuxt deserves more love such a nice tool But yeah the whole app is bundled in there, so it's hard to reproduce
Hard@Work
Hard@Work5mo ago
Can you disable minification?
cosbgn
cosbgnOP5mo ago
on cl dashboard?
Hard@Work
Hard@Work5mo ago
No, in Nuxt
cosbgn
cosbgnOP5mo ago
I don't think, I'm googling it, but I think server code it's always minified I've also added toString() but doesn't help:
endpoint: `https://${process.env.CLOUDFLARE_ACCOUNT_ID.toString()}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: process.env.CLOUDFLARE_R2_BUCKET_ACCESS_KEY_ID.toString(),
secretAccessKey: process.env.CLOUDFLARE_R2_BUCKET_SECRET_KEY_ID.toString(),
},
endpoint: `https://${process.env.CLOUDFLARE_ACCOUNT_ID.toString()}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: process.env.CLOUDFLARE_R2_BUCKET_ACCESS_KEY_ID.toString(),
secretAccessKey: process.env.CLOUDFLARE_R2_BUCKET_SECRET_KEY_ID.toString(),
},
I'm pretty sure creds are correct (also form the logs screenshotted) from the logs, I think it happens here: export const defaultUserAgent = ({ serviceId, clientVersion }: DefaultUserAgentOptions): Provider<UserAgent> => {
cosbgn
cosbgnOP5mo ago
So some of this in workers is an object and not a string:
[`os/${platform()}`, release()]
["md/nodejs", `${versions.node}`]
[`exec-env/${env.AWS_EXECUTION_ENV}`]
[`app/${appId}`]
[`os/${platform()}`, release()]
["md/nodejs", `${versions.node}`]
[`exec-env/${env.AWS_EXECUTION_ENV}`]
[`app/${appId}`]
I just don't get why it happens to me and not everyone else
cosbgn
cosbgnOP5mo ago
No description
cosbgn
cosbgnOP5mo ago
I'm getting the same
Hard@Work
Hard@Work5mo ago
Yeah, so it isn't just a you issue...
cosbgn
cosbgnOP5mo ago
yes but S3Client is fairly used in workers and I can't find a single mention of this maybe is nuxt doing some polyfill to fix node
Hard@Work
Hard@Work5mo ago
I'm guessing maybe it is a Nuxt + S3 issue Let me see if I can get a bit closer
cosbgn
cosbgnOP5mo ago
🙏
Hard@Work
Hard@Work5mo ago
Found it!
cosbgn
cosbgnOP5mo ago
OMG
Hard@Work
Hard@Work5mo ago
It appears that Nuxt is using unenv to smooth over differences in runtimes And one of the things it does on build is replace imports from os with a proxy that doesn't appear to be working correctly Looking at the unminified code isn't very enlightening, but yeah
cosbgn
cosbgnOP5mo ago
Any solution?
Hard@Work
Hard@Work5mo ago
Disable unenv somehow? Not entirely sure, probably something you want to flag either to the Nuxt or unenv team
cosbgn
cosbgnOP5mo ago
Thanks a ton for the investigation super helpful.
Hard@Work
Hard@Work5mo ago
Sorry I couldn't do more there. Tbh, I don't do much(if anything) in Nuxt, so I might not be the best person for this kind of thing
cosbgn
cosbgnOP5mo ago
It's great I now know where to go Thanks a ton
Want results from more Discord servers?
Add your server