-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy paths3_upload_object.ts
More file actions
130 lines (118 loc) · 3.76 KB
/
s3_upload_object.ts
File metadata and controls
130 lines (118 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { S3, waitUntilObjectExists } from '@aws-sdk/client-s3';
import { WaiterOptions } from '@aws-sdk/util-waiter';
import { AwsS3Module } from '..';
import { AWS, crudBuilderFormat } from '../../../services/aws_macros';
import { Context, RpcBase, RpcResponseObject } from '../../interfaces';
import { BucketObject } from '../entity';
export class S3UploadObjectRpc extends RpcBase {
module: AwsS3Module;
outputTable = {
bucket: 'varchar',
key: 'varchar',
status: 'varchar',
response_message: 'varchar',
} as const;
getBucketLocation = crudBuilderFormat<S3, 'getBucketLocation', string | undefined>(
'getBucketLocation',
name => ({
Bucket: name,
}),
res => res?.LocationConstraint,
);
call = async (
_dbId: string,
_dbUser: string,
ctx: Context,
bucketName: string,
bucketKey: string,
content: string,
contentType: string,
): Promise<RpcResponseObject<typeof this.outputTable>[]> => {
// we need to have bucket name, key and source
if (!bucketName || !bucketKey || !content || !contentType) {
throw new Error('Cannot upload an object without bucket name, key, content or content type');
}
const client = (await ctx.getAwsClient(await ctx.getDefaultRegion())) as AWS;
// first determine bucket region
const region = (await this.getBucketLocation(client.s3Client, bucketName)) ?? 'us-east-1';
const enabledRegions = (await ctx.getEnabledAwsRegions()) as string[];
if (region) {
// check if it is on enabled regions
if (enabledRegions.includes(region)) {
const clientRegion = (await ctx.getAwsClient(region)) as AWS;
const result = await client.s3Client.putObject({
Bucket: bucketName,
Key: bucketKey,
Body: content,
ContentType: contentType,
});
// wait until the object is available
const res = await waitUntilObjectExists(
{
client: clientRegion.s3Client,
// all in seconds
maxWaitTime: 900,
minDelay: 1,
maxDelay: 4,
} as WaiterOptions<S3>,
{ Bucket: bucketName, Key: bucketKey },
);
if (res.state === 'SUCCESS') {
// query for the bucket
const bucket =
(await this.module.bucket.db.read(ctx, `${bucketName}|${region}`)) ??
(await this.module.bucket.cloud.read(ctx, `${bucketName}|${region}`));
// need to insert records in the db
const bucketObject: BucketObject = {
key: bucketKey,
bucketName,
region,
bucket,
eTag: result.ETag,
};
await this.module.bucketObject.db.create(bucketObject, ctx);
// that was ok
return [
{
bucket: bucketName,
key: bucketKey,
status: 'OK',
response_message: 'Bucket content has been updated',
},
];
} else {
return [
{
bucket: bucketName,
key: bucketKey,
status: 'OK',
response_message: 'There was a problem creating your object',
},
];
}
} else {
return [
{
bucket: bucketName,
key: bucketKey,
status: 'KO',
response_message: 'Bucket content could not be updated because it is not on supported regions',
},
];
}
}
return [
{
bucket: 'none',
key: 'none',
status: 'KO',
response_message: 'There was an unexpected error uploading your object to bucket',
},
];
};
constructor(module: AwsS3Module) {
super();
this.module = module;
super.init();
}
}