-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support transformation of S3 getSignedUrl (#517)
- Loading branch information
Showing
30 changed files
with
466 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"aws-sdk-js-codemod": minor | ||
--- | ||
|
||
Support transformation of S3 getSignedUrl |
8 changes: 8 additions & 0 deletions
8
src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/callback.input.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
const s3 = new AWS.S3(); | ||
const params = { Bucket: "bucket", Key: "key" }; | ||
|
||
s3.getSignedUrl("getObject", params, function (err, url) { | ||
console.log('The URL is', url); | ||
}); |
10 changes: 10 additions & 0 deletions
10
src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/callback.output.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
const s3 = new AWS.S3(); | ||
const params = { Bucket: "bucket", Key: "key" }; | ||
|
||
// S3 getSignedUrl with callbacks are not supported in AWS SDK for JavaScript (v3). | ||
// Please convert to 'client.getSignedUrl(apiName, options)', and re-run aws-sdk-js-codemod. | ||
s3.getSignedUrl("getObject", params, function (err, url) { | ||
console.log('The URL is', url); | ||
}); |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.identifier.input.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
const s3 = new AWS.S3(); | ||
const params = { Bucket: "bucket", Key: "key" }; | ||
|
||
url = s3.getSignedUrl("getObject", params); |
9 changes: 9 additions & 0 deletions
9
src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.identifier.output.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; | ||
import { GetObjectCommand, S3 } from "@aws-sdk/client-s3"; | ||
|
||
const s3 = new S3(); | ||
const params = { Bucket: "bucket", Key: "key" }; | ||
|
||
url = await getSignedUrl(s3, new GetObjectCommand(params), { | ||
expiresIn: "/* add value from 'Expires' from v2 call if present, else remove */" | ||
}); |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.import-equals.input.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import AWS = require("aws-sdk"); | ||
|
||
const s3 = new AWS.S3(); | ||
|
||
url = s3.getSignedUrl("getObject", { Bucket: "bucket", Key: "key" }); | ||
url = s3.getSignedUrl("getObject", { Bucket: "bucket", Key: "key", Expires: 60 }); |
22 changes: 22 additions & 0 deletions
22
src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.import-equals.output.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import AWS_s3_request_presigner = require("@aws-sdk/s3-request-presigner"); | ||
|
||
const { | ||
getSignedUrl | ||
} = AWS_s3_request_presigner; | ||
|
||
import AWS_S3 = require("@aws-sdk/client-s3"); | ||
|
||
const { | ||
GetObjectCommand, | ||
S3 | ||
} = AWS_S3; | ||
|
||
const s3 = new S3(); | ||
|
||
url = await getSignedUrl(s3, new GetObjectCommand({ Bucket: "bucket", Key: "key" })); | ||
url = await getSignedUrl(s3, new GetObjectCommand({ | ||
Bucket: "bucket", | ||
Key: "key" | ||
}), { | ||
expiresIn: 60 | ||
}); |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.input.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
const s3 = new AWS.S3(); | ||
|
||
url = s3.getSignedUrl("getObject", { Bucket: "bucket", Key: "key" }); | ||
url = s3.getSignedUrl("getObject", { Bucket: "bucket", Key: "key", Expires: 60 }); |
12 changes: 12 additions & 0 deletions
12
src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.output.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; | ||
import { GetObjectCommand, S3 } from "@aws-sdk/client-s3"; | ||
|
||
const s3 = new S3(); | ||
|
||
url = await getSignedUrl(s3, new GetObjectCommand({ Bucket: "bucket", Key: "key" })); | ||
url = await getSignedUrl(s3, new GetObjectCommand({ | ||
Bucket: "bucket", | ||
Key: "key" | ||
}), { | ||
expiresIn: 60 | ||
}); |
9 changes: 9 additions & 0 deletions
9
src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.promise.input.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
const s3 = new AWS.S3(); | ||
const params = { Bucket: "bucket", Key: "key" }; | ||
|
||
url = await s3.getSignedUrlPromise("getObject", params); | ||
|
||
url = await s3.getSignedUrlPromise("getObject", { Bucket: "bucket", Key: "key" }); | ||
url = await s3.getSignedUrlPromise("getObject", { Bucket: "bucket", Key: "key", Expires: 60 }); |
17 changes: 17 additions & 0 deletions
17
src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.promise.output.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; | ||
import { GetObjectCommand, S3 } from "@aws-sdk/client-s3"; | ||
|
||
const s3 = new S3(); | ||
const params = { Bucket: "bucket", Key: "key" }; | ||
|
||
url = await getSignedUrl(s3, new GetObjectCommand(params), { | ||
expiresIn: "/* add value from 'Expires' from v2 call if present, else remove */" | ||
}); | ||
|
||
url = await getSignedUrl(s3, new GetObjectCommand({ Bucket: "bucket", Key: "key" })); | ||
url = await getSignedUrl(s3, new GetObjectCommand({ | ||
Bucket: "bucket", | ||
Key: "key" | ||
}), { | ||
expiresIn: 60 | ||
}); |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.require.input.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const AWS = require("aws-sdk"); | ||
|
||
const s3 = new AWS.S3(); | ||
|
||
url = s3.getSignedUrl("getObject", { Bucket: "bucket", Key: "key" }); | ||
url = s3.getSignedUrl("getObject", { Bucket: "bucket", Key: "key", Expires: 60 }); |
17 changes: 17 additions & 0 deletions
17
src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.require.output.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const { | ||
getSignedUrl | ||
} = require("@aws-sdk/s3-request-presigner"), | ||
{ | ||
GetObjectCommand, | ||
S3 | ||
} = require("@aws-sdk/client-s3"); | ||
|
||
const s3 = new S3(); | ||
|
||
url = await getSignedUrl(s3, new GetObjectCommand({ Bucket: "bucket", Key: "key" })); | ||
url = await getSignedUrl(s3, new GetObjectCommand({ | ||
Bucket: "bucket", | ||
Key: "key" | ||
}), { | ||
expiresIn: 60 | ||
}); |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/putObject.input.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
const s3 = new AWS.S3(); | ||
|
||
url = s3.getSignedUrl("putObject", { Bucket: "bucket", Key: "key" }); | ||
url = s3.getSignedUrl("putObject", { Bucket: "bucket", Key: "key", Expires: 60 }); |
12 changes: 12 additions & 0 deletions
12
src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/putObject.output.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; | ||
import { PutObjectCommand, S3 } from "@aws-sdk/client-s3"; | ||
|
||
const s3 = new S3(); | ||
|
||
url = await getSignedUrl(s3, new PutObjectCommand({ Bucket: "bucket", Key: "key" })); | ||
url = await getSignedUrl(s3, new PutObjectCommand({ | ||
Bucket: "bucket", | ||
Key: "key" | ||
}), { | ||
expiresIn: 60 | ||
}); |
This file contains 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
This file contains 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
This file contains 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
16 changes: 16 additions & 0 deletions
16
src/transforms/v2-to-v3/apis/getClientApiCallExpression.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { CallExpression } from "jscodeshift"; | ||
|
||
import { ClientIdentifier } from "./getClientIdentifiers"; | ||
|
||
export const getClientApiCallExpression = ( | ||
clientId: ClientIdentifier, | ||
apiName: string | ||
// @ts-expect-error Property 'arguments' is missing in type | ||
): CallExpression => ({ | ||
type: "CallExpression", | ||
callee: { | ||
type: "MemberExpression", | ||
object: clientId, | ||
property: { type: "Identifier", name: apiName }, | ||
}, | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Returns the command name for a given API name | ||
// Example input `getObject` returns `GetObjectCommand` | ||
export const getCommandName = (apiName: string): string => | ||
`${apiName[0].toUpperCase()}${apiName.slice(1)}Command`; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Collection, JSCodeshift, Literal } from "jscodeshift"; | ||
|
||
import { getClientIdentifiers } from "./getClientIdentifiers"; | ||
|
||
export interface GetS3SignedUrlApiNameOptions { | ||
v2ClientName: string; | ||
v2ClientLocalName: string; | ||
v2GlobalName?: string; | ||
} | ||
|
||
export const getS3SignedUrlApiNames = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
options: GetS3SignedUrlApiNameOptions | ||
): string[] => { | ||
if (options.v2ClientName !== "S3") return []; | ||
|
||
const apiNames: Set<string> = new Set(); | ||
const clientIdentifiers = getClientIdentifiers(j, source, options); | ||
|
||
for (const clientId of clientIdentifiers) { | ||
for (const apiName of ["getSignedUrl", "getSignedUrlPromise"]) { | ||
source | ||
.find(j.CallExpression, { | ||
callee: { | ||
type: "MemberExpression", | ||
object: clientId, | ||
property: { type: "Identifier", name: apiName }, | ||
}, | ||
}) | ||
.forEach((callExpression) => { | ||
apiNames.add((callExpression.value.arguments[0] as Literal).value as string); | ||
}); | ||
} | ||
} | ||
|
||
return [...apiNames].sort(); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
export * from "./addNotSupportedComments"; | ||
export * from "./getClientWaiterStates"; | ||
export * from "./getV3ClientWaiterApiName"; | ||
export * from "./getS3SignedUrlApiNames"; | ||
export * from "./isS3GetSignedUrlApiUsed"; | ||
export * from "./isS3UploadApiUsed"; | ||
export * from "./removePromiseCalls"; | ||
export * from "./replaceS3GetSignedUrlApi"; | ||
export * from "./replaceS3UploadApi"; | ||
export * from "./replaceWaiterApi"; | ||
export * from "./getCommandName"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
|
||
import { getClientIdentifiers } from "./getClientIdentifiers"; | ||
|
||
export interface IsS3GetSignedUrlApiUsedOptions { | ||
v2ClientName: string; | ||
v2ClientLocalName: string; | ||
v2GlobalName?: string; | ||
} | ||
|
||
export const isS3GetSignedUrlApiUsed = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
options: IsS3GetSignedUrlApiUsedOptions | ||
) => { | ||
if (options.v2ClientName !== "S3") return false; | ||
|
||
const clientIdentifiers = getClientIdentifiers(j, source, options); | ||
|
||
for (const clientId of clientIdentifiers) { | ||
for (const apiName of ["getSignedUrl", "getSignedUrlPromise"]) { | ||
const s3GetSignedUrlCallExpressions = source.find(j.CallExpression, { | ||
callee: { | ||
type: "MemberExpression", | ||
object: clientId, | ||
property: { type: "Identifier", name: apiName }, | ||
}, | ||
}); | ||
if (s3GetSignedUrlCallExpressions.length) return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; |
Oops, something went wrong.