Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@tus/s3-store: Method for reading the file is missing #498

Closed
wants to merge 2 commits into from

Conversation

andriykhc
Copy link

This PR addresses an issue where an uploaded URL file produces a not found response

@andriykhc andriykhc closed this Oct 19, 2023
@mitjap
Copy link
Collaborator

mitjap commented Oct 19, 2023

Your example is not valid GetObject command for latest @aws-sdk/client-s3 library.

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/classes/getobjectcommand.html

This is what works for me. But this is asynchronous function which is not fully supported by the library.

async read(id: string) {
  const data = await this.client.getObject({
    Bucket: this.bucket,
    Key: id,
  })
  return data.Body as Readable;
}     

Another alternative is to synchronously return a Readable stream and pass data to it when object's stream is available.

read(id: string) {
  const pass = new stream.PassThrough();
  
  (async () => {
    try {
      const data = await this.client.getObject({
        Bucket: this.bucket,
        Key: id,
      })

      try {
        if (data.Body) {
          streamProm.pipeline(data.Body as Readable, pass);
        } else {
          pass.write(null);
        }
      } catch (err) {
        // ignore errors
      }
    } catch (err) {
      pass.destroy(err);
    }
  })()

  return pass;
}

@andriykhc
Copy link
Author

Sorry, I used this library on a project that still uses aws-sdk v2 and I forgot that this library already uses aws-sdk/client-s3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants