Promise not working in JS worker

Hi I have am using the S3 client in a Cloudflare worker
import {GetObjectCommand, ListObjectsV2Command, ListObjectsV2CommandOutput, S3Client} from "@aws-sdk/client-s3";

// Create client
const S3: S3Client = new S3Client({
region: "auto",
endpoint: `https://${accountid}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: env.AWS_ACCESS_KEY_ID,
secretAccessKey: env.AWS_SECRET_ACCESS_KEY,
},
});
import {GetObjectCommand, ListObjectsV2Command, ListObjectsV2CommandOutput, S3Client} from "@aws-sdk/client-s3";

// Create client
const S3: S3Client = new S3Client({
region: "auto",
endpoint: `https://${accountid}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: env.AWS_ACCESS_KEY_ID,
secretAccessKey: env.AWS_SECRET_ACCESS_KEY,
},
});
If I use await syntax I can log the results from this call in a Worker
const item: ListObjectsV2CommandOutput = await S3.send(new ListObjectsV2Command({Bucket: 'pics'}));
item.Contents.map((item) => {
console.log(item.Key);})
const item: ListObjectsV2CommandOutput = await S3.send(new ListObjectsV2Command({Bucket: 'pics'}));
item.Contents.map((item) => {
console.log(item.Key);})
If I try the same thing using .then() syntax I do not get any logs
S3.send(
new ListObjectsV2Command({Bucket: 'pics'})
).then((item) => {
console.log(item.Contents[0].Key)});
S3.send(
new ListObjectsV2Command({Bucket: 'pics'})
).then((item) => {
console.log(item.Contents[0].Key)});
Can anyone please explain why?
5 Replies
Sam
Sam8mo ago
Why are you using a s3 client package, instead of the Wrangler bindings?
Chaika
Chaika8mo ago
If the worker returns a response, it ends, and all dangling/left promises are cancelled but yea, why not r2 binding?
Barbara88
Barbara88OP8mo ago
Thanks heaps for that! that makes sense I would like to transition to R2 binding but the documentation on how to get signed urls from the R2 bindings was not as clear as the S3 client doco
Barbara88
Barbara88OP8mo ago
Thanks! managed to get it working using the R2 bindings, i had a small bug when i was trying to do this earlier
Want results from more Discord servers?
Add your server