Skip to content

Commit

Permalink
feat: tag only files that were not yet tagged
Browse files Browse the repository at this point in the history
  • Loading branch information
jaulz authored Jul 26, 2024
1 parent 2b9ee27 commit 55058a7
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/utils/s3-sync.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type {
CopyObjectOutput,
CopyObjectRequest,
HeadObjectOutput,
HeadObjectRequest,
ListObjectsV2Output,
ListObjectsV2Request,
PutObjectOutput,
Expand All @@ -22,7 +24,7 @@ import { getUtils } from "./logger";
const readdir = util.promisify(fs.readdir);
const stat = util.promisify(fs.stat);

type S3Objects = Record<string, S3Object>;
type S3Objects = Record<string, S3Object & HeadObjectOutput>;

/**
* Synchronize a local folder to a S3 bucket.
Expand Down Expand Up @@ -78,7 +80,7 @@ export async function s3Sync({
const targetKeys = filesToUpload.map((file) =>
targetPathPrefix !== undefined ? path.posix.join(targetPathPrefix, file) : file
);
const keysToDelete = findKeysToDelete(Object.keys(existingS3Objects), targetKeys);
const keysToDelete = findKeysToDelete(existingS3Objects, targetKeys);

Check failure on line 83 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Type

Argument of type 'S3Objects' is not assignable to parameter of type '{ Key: string; Metadata: { 'x-amz-tagging': string | undefined; }; }[]'.

Check failure on line 83 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Lint

Argument of type 'S3Objects' is not assignable to parameter of type '{ Key: string; Metadata: { 'x-amz-tagging': string | undefined; }; }[]'.

Check failure on line 83 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Unit tests (16, 2.36.0)

Argument of type 'S3Objects' is not assignable to parameter of type '{ Key: string; Metadata: { 'x-amz-tagging': string | undefined; }; }[]'.

Check failure on line 83 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Unit tests (16, latest)

Argument of type 'S3Objects' is not assignable to parameter of type '{ Key: string; Metadata: { 'x-amz-tagging': string | undefined; }; }[]'.
if (keysToDelete.length > 0) {
keysToDelete.map((key) => {
getUtils().log.verbose(`Deleting ${key}`);
Expand Down Expand Up @@ -124,21 +126,40 @@ async function s3ListAll(aws: AwsProvider, bucketName: string, pathPrefix?: stri
MaxKeys: 1000,
ContinuationToken: continuationToken,
});
(result.Contents ?? []).forEach((object) => {

for (const object of result.Contents ?? []) {
if (object.Key === undefined) {
return;
continue;
}
objects[object.Key] = object;
});

const objectDetails = await aws.request<HeadObjectRequest, HeadObjectOutput>("S3", "headObject", {
Bucket: bucketName,
Key: object.Key,
});

objects[object.Key] = {
...object,
...objectDetails,
};
}

continuationToken = result.NextContinuationToken;
} while (result.IsTruncated === true);

return objects;
}

function findKeysToDelete(existing: string[], target: string[]): string[] {
function findKeysToDelete(existing: { Key: string, Metadata: { 'x-amz-tagging': string | undefined } }[], target: string[]): string[] {
// Returns every key that shouldn't exist anymore
return existing.filter((key) => target.indexOf(key) === -1);

Check failure on line 154 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Type

Type '{ Key: string; Metadata: { 'x-amz-tagging': string | undefined; }; }[]' is not assignable to type 'string[]'.

Check failure on line 154 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Type

Argument of type '{ Key: string; Metadata: { 'x-amz-tagging': string | undefined; }; }' is not assignable to parameter of type 'string'.

Check failure on line 154 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Lint

Type '{ Key: string; Metadata: { 'x-amz-tagging': string | undefined; }; }[]' is not assignable to type 'string[]'.

Check failure on line 154 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Lint

Argument of type '{ Key: string; Metadata: { 'x-amz-tagging': string | undefined; }; }' is not assignable to parameter of type 'string'.

Check failure on line 154 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Unit tests (16, 2.36.0)

Type '{ Key: string; Metadata: { 'x-amz-tagging': string | undefined; }; }[]' is not assignable to type 'string[]'.

Check failure on line 154 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Unit tests (16, 2.36.0)

Argument of type '{ Key: string; Metadata: { 'x-amz-tagging': string | undefined; }; }' is not assignable to parameter of type 'string'.

Check failure on line 154 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Unit tests (16, latest)

Type '{ Key: string; Metadata: { 'x-amz-tagging': string | undefined; }; }[]' is not assignable to type 'string[]'.

Check failure on line 154 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Unit tests (16, latest)

Argument of type '{ Key: string; Metadata: { 'x-amz-tagging': string | undefined; }; }' is not assignable to parameter of type 'string'.
return Object.entries(existing)
.filter(([, object]) => {
const tags = new URLSearchParams(object.Metadata['x-amz-tagging'] ?? '');

Check failure on line 157 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Type

Cannot find name 'URLSearchParams'.

Check failure on line 157 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Lint

Cannot find name 'URLSearchParams'.

Check failure on line 157 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Unit tests (16, 2.36.0)

Cannot find name 'URLSearchParams'.

Check failure on line 157 in src/utils/s3-sync.ts

View workflow job for this annotation

GitHub Actions / Unit tests (16, latest)

Cannot find name 'URLSearchParams'.

return !tags.has('Obsolete', true);
})
.filter(([key]) => target.indexOf(key) === -1)
.map(([key]) => key);
}

export async function s3Put(aws: AwsProvider, bucket: string, key: string, fileContent: Buffer): Promise<void> {
Expand Down

0 comments on commit 55058a7

Please sign in to comment.