Skip to content

Commit

Permalink
fix(s3-static-assets): when initializing s3 client, pass in bucket re…
Browse files Browse the repository at this point in the history
…gion to ensure correct s3 endpoints (#1262)
  • Loading branch information
dphang committed Jun 18, 2021
1 parent 5edf06e commit 83805bb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/libs/s3-static-assets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import getPublicAssetCacheControl, {

type UploadStaticAssetsOptions = {
bucketName: string;
bucketRegion: string;
basePath: string;
nextConfigDir: string;
nextStaticDir?: string;
Expand Down Expand Up @@ -151,6 +152,7 @@ const uploadStaticAssetsFromBuild = async (
): Promise<AWS.S3.ManagedUpload.SendData[]> => {
const {
bucketName,
bucketRegion,
credentials,
basePath,
publicDirectoryCache,
Expand All @@ -163,6 +165,7 @@ const uploadStaticAssetsFromBuild = async (
});
const s3 = await S3ClientFactory({
bucketName,
bucketRegion,
credentials: credentials
});

Expand All @@ -179,6 +182,7 @@ const uploadStaticAssetsFromBuild = async (

type DeleteOldStaticAssetsOptions = {
bucketName: string;
bucketRegion: string;
basePath: string;
credentials: Credentials;
};
Expand All @@ -191,12 +195,13 @@ type DeleteOldStaticAssetsOptions = {
const deleteOldStaticAssets = async (
options: DeleteOldStaticAssetsOptions
): Promise<void> => {
const { bucketName, basePath } = options;
const { bucketName, bucketRegion, basePath } = options;

const normalizedBasePathPrefix = basePath ? basePath.slice(1) + "/" : "";

const s3 = await S3ClientFactory({
bucketName,
bucketRegion,
credentials: options.credentials
});

Expand Down
10 changes: 8 additions & 2 deletions packages/libs/s3-static-assets/src/lib/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ObjectList } from "aws-sdk/clients/s3";

type S3ClientFactoryOptions = {
bucketName: string;
bucketRegion: string;
credentials: Credentials;
};

Expand Down Expand Up @@ -49,9 +50,10 @@ export type Credentials = {

export default async ({
bucketName,
bucketRegion,
credentials
}: S3ClientFactoryOptions): Promise<S3Client> => {
let s3 = new AWS.S3({ ...credentials });
let s3 = new AWS.S3({ ...credentials, region: bucketRegion });

try {
const { Status } = await s3
Expand All @@ -61,7 +63,11 @@ export default async ({
.promise();

if (Status === "Enabled") {
s3 = new AWS.S3({ ...credentials, useAccelerateEndpoint: true });
s3 = new AWS.S3({
...credentials,
region: bucketRegion,
useAccelerateEndpoint: true
});
}
} catch (err) {
console.warn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,14 @@ class NextjsComponent extends Component {
// After deployment, only the new and previous build ID's assets are present. We still need previous build assets as it takes time to propagate the Lambda.
await deleteOldStaticAssets({
bucketName: bucketOutputs.name,
bucketRegion: bucketRegion,
basePath: routesManifest.basePath,
credentials: this.context.credentials.aws
});

await uploadStaticAssetsFromBuild({
bucketName: bucketOutputs.name,
bucketRegion: bucketRegion,
basePath: routesManifest.basePath,
nextConfigDir: nextConfigPath,
nextStaticDir: nextStaticPath,
Expand Down

0 comments on commit 83805bb

Please sign in to comment.