Skip to content

Commit 02ed8da

Browse files
committed
refactor(s3): change bucket equal check
1 parent 79bb4f1 commit 02ed8da

3 files changed

Lines changed: 24 additions & 18 deletions

File tree

packages/providers/s3/src/provider.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,7 @@ export class S3Provider<ID> extends StorageServiceProvider<ID> {
126126
// If the resource bucket is different from the upload bucket
127127
// we need to check if the file exists in the upload bucket too.
128128
// This is because the file might not have been processed yet.
129-
if (
130-
S3ProviderConfigurationParser.isResourceBucketDifferent(
131-
this.configuration,
132-
this.configuration.resourceBucket
133-
)
134-
) {
129+
if (!this.buckets.upload.equals(this.buckets.resource)) {
135130
const exists = await this.buckets.upload.keyExists(
136131
this.buckets.upload.keyResolver.resolve(fileId),
137132
{}

packages/providers/s3/src/utils/s3-bucket.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import { ExpiresIn } from 'shared-types'
1919
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
2020

21-
class S3Bucket<
21+
abstract class S3Bucket<
2222
Config extends
2323
| Required<S3DefaultBucketConfiguration>
2424
| Required<S3ResourceBucketConfiguration>,
@@ -42,6 +42,12 @@ class S3Bucket<
4242
this.path = s3Config.bucketPath
4343
}
4444

45+
abstract equals(
46+
bucket: this extends S3UploadBucket<ID>
47+
? S3ResourceBucket<ID>
48+
: S3UploadBucket<ID>
49+
): boolean
50+
4551
async keyExists(
4652
key: string,
4753
commandInput?: Omit<HeadObjectCommandInput, 'Bucket' | 'Key'>
@@ -72,6 +78,14 @@ export class S3UploadBucket<ID> extends S3Bucket<
7278
Required<S3DefaultBucketConfiguration>,
7379
ID
7480
> {
81+
equals(bucket: S3ResourceBucket<ID>): boolean {
82+
return (
83+
this.name === bucket.name &&
84+
this.region === bucket.region &&
85+
this.path === bucket.path[0]
86+
)
87+
}
88+
7589
async getSignedUploadUrl(
7690
fileId: ID,
7791
expiresIn: ExpiresIn,
@@ -93,6 +107,14 @@ export class S3ResourceBucket<ID> extends S3Bucket<
93107
Required<S3ResourceBucketConfiguration>,
94108
ID
95109
> {
110+
equals(bucket: S3UploadBucket<ID>): boolean {
111+
return (
112+
this.name === bucket.name &&
113+
this.region === bucket.region &&
114+
this.path[0] === bucket.path
115+
)
116+
}
117+
96118
async getSignedDownloadUrl(
97119
key: string,
98120
commandInput?: Omit<GetObjectCommandInput, 'Bucket' | 'Key'>

packages/providers/s3/src/utils/s3-provider-configuration-parser.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,4 @@ export class S3ProviderConfigurationParser {
8787
acl: configuration.acl ?? ObjectCannedACL.bucket_owner_full_control,
8888
}
8989
}
90-
91-
static isResourceBucketDifferent(
92-
a: S3DefaultBucketConfiguration,
93-
b: S3ResourceBucketConfiguration
94-
) {
95-
return !(
96-
a.bucketName === b.bucketName &&
97-
a.bucketRegion === b.bucketRegion &&
98-
a.bucketPath === b.bucketPath?.[0]
99-
)
100-
}
10190
}

0 commit comments

Comments
 (0)