I have a multi-part upload, using AWS JS SDK v3, that takes a long time. After one hour the credentials expire. I need to find a way to refresh the credentials to keep the upload going. I can ask my backend for new credentials, I just need to understand where to inject them.
The response I get is:
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>
ExpiredToken
</Code>
<Message>The provided token has expired.</Message>
<Token-0>REDACTED</Token-0>
<RequestId>REDACTED</RequestId>
<HostId>REDACTED</HostId>
</Error>
The code I'm using looks like this:
import { S3Client } from "@aws-sdk/client-s3";
import { Upload as AwsUpload, Progress } from "@aws-sdk/lib-storage";
import type { AwsCredentialIdentity } from "@aws-sdk/types/dist-types/identity/AwsCredentialIdentity";
// ...
const { accessKeyId, secretAccessKey, sessionToken } = session.uploadCredentials;
const selectedFile: File = //...
const region: string = // ...
const key: string = // ...
const multipartUpload = new AwsUpload({
client: new S3Client({
maxAttempts: 10,
region,
credentials: async (): Promise<AwsCredentialIdentity> => {
return {
accessKeyId,
secretAccessKey,
sessionToken,
};
},
}),
params: { Bucket: session.bucket, Key: key, Body: selectedFile },
});
// Irrelevant, but for completeness:
multipartUpload.on("httpUploadProgress", handleUploadProgress);
multipartUpload.done().then(handleUploadResponse).catch(handleUploadError);
I can't figure out how to configure the SDK to ask me for new credentials, when necessary. Can it be done?