Search code examples
vue.jsweb-workeraws-sdk-js

AWS SDK V3 S3Client in web worker throws ReferenceError: window is not defined


In a Vue JS app I am using a web worker to generate signed URLs. This code worked with no problem using @aws-sdk/client-s3 version 3.0.0. However, later versions fail the getSignedUrl() call with the error "ReferenceError: window is not defined".

(worker.js)

import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';

async function generateUrls(s3config, bucket, filename) {

  let getParams = {
    Bucket: bucket,
    Key: filename
  };
  let url;

  const clientS3 = new S3Client(s3config);

  const getCmd = new GetObjectCommand(getParams);
  try {
    url = await getSignedUrl(clientS3, getCmd);
  } catch (err) {
    console.log('Error getting signed URL ', err);
  }

  return url;
}

I know web workers run in a different context and don't have access to the main thread's window. I have traced into the AWS library client-s3 after 3.0.0 and seen that (for some reason) it assumes the existence of window.

Is there some trick to using the S3 client in a web worker or is this a bug?


Solution

  • it worked for me on AWS dynamodb by putting this somewhere on the code

    if(typeof window != "object")
        self.window = self;
    

    v3 is working now on cloudflare.