Last active
September 9, 2024 12:51
-
-
Save ValchanOficial/f190f72f951809c2927a546411489130 to your computer and use it in GitHub Desktop.
[AWS SDK V3][S3 getSignedUrl][ERROR][Test][Sinon] TypeError: Descriptor for property getSignedUrl is non-configurable and non-writable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Solution: | |
sinon.stub(presigner.prototype, 'presign').returns(Promise.resolve(new HttpRequest(parseUrl(Key)))); | |
----------------------- | |
Tests: | |
import assert from 'assert'; | |
import sinon from 'sinon'; | |
import { GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3"; | |
import {HttpRequest} from '@aws-sdk/protocol-http'; | |
import {parseUrl} from '@aws-sdk/url-parser'; | |
import { putObject, getPresignedDownloadUrl, getPresignedUploadUrl } from '../src/aws/s3'; | |
import { S3RequestPresigner as presigner} from '@aws-sdk/s3-request-presigner'; | |
import { s3Client } from '../src/aws/clients'; | |
describe('putObject', () => { | |
const expectedBody = 'body'; | |
const Bucket = 'bucket.test'; | |
const Key = 'https://fake/path/to/test.txt'; | |
process.env.AWS_REGION = 'us-bar-1'; | |
process.env.AWS_ACCESS_KEY_ID = 'foo'; | |
process.env.AWS_SECRET_ACCESS_KEY = 'bar'; | |
sinon.stub(presigner.prototype, 'presign').resolves(new HttpRequest(parseUrl(Key))); | |
const putStub = sinon.stub(s3Client, 'send').callsFake((command: any) => { | |
if (command instanceof PutObjectCommand || command instanceof GetObjectCommand) { | |
return Promise.resolve(command); | |
} | |
}); | |
it('should upload object to s3', async () => { | |
await putObject({ Bucket, Key, Body: expectedBody, Metadata: { 'key': 'test' } }); | |
assert.equal(putStub.calledOnce, true); | |
assert.equal(putStub.firstCall.firstArg.input.Body, expectedBody) | |
assert.equal(putStub.firstCall.firstArg.input.Metadata.key, 'test') | |
}) | |
it('should get presigned download url', async () => { | |
const url = await getPresignedDownloadUrl(Bucket, Key); | |
assert.ok(url); | |
assert.notEqual(url, null); | |
assert.notEqual(url.length, 0); | |
assert.equal(url, Key); | |
}) | |
it('should get presigned upload url', async () => { | |
const url = await getPresignedUploadUrl(Bucket, Key); | |
assert.ok(url); | |
assert.notEqual(url, null); | |
assert.notEqual(url.length, 0); | |
assert.equal(url, Key); | |
}) | |
}) | |
-------------------------------------------------------------------- | |
Code: | |
import { GetObjectCommand, PutObjectCommand, PutObjectCommandInput, PutObjectCommandOutput } from "@aws-sdk/client-s3"; | |
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'; | |
import { s3Client } from "./clients"; | |
const putObject = async (input: PutObjectCommandInput): Promise<PutObjectCommandOutput> => { | |
const command = new PutObjectCommand(input); | |
return await s3Client.send(command); | |
} | |
const getPresignedDownloadUrl = async (Bucket: string, Key: string): Promise<string> => { | |
const command = new GetObjectCommand({ Bucket, Key }); | |
return await getSignedUrl(s3Client, command, { expiresIn: 3600 }); | |
} | |
const getPresignedUploadUrl = async (Bucket: string, Key: string): Promise<string> => { | |
const command = new PutObjectCommand({ Bucket, Key }); | |
return await getSignedUrl(s3Client, command, { expiresIn: 3600 }); | |
} | |
export { putObject, getPresignedDownloadUrl, getPresignedUploadUrl }; | |
-------------------------------------------- | |
S3 Client: | |
import { S3Client } from "@aws-sdk/client-s3"; | |
const s3Client = new S3Client(); | |
export { | |
s3Client, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment