-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_upload_sample.ts
147 lines (127 loc) · 3.3 KB
/
file_upload_sample.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import axios from "axios";
import fetch from "cross-fetch";
import * as fs from "fs";
import * as FormData from "form-data";
const USERNAME = "";
const PASSWORD = "";
const ORGANIZATION_UID = "";
const URL = "";
interface AuthResponse {
sessionToken: string;
}
interface Fields {
key: string;
"x-amz-algorithm": string;
"x-amz-credential": string;
"x-amz-date": string;
"x-amz-security-token": string;
policy: string;
"x-amz-signature": string;
}
interface PreSignedData {
url: string;
fields: Fields;
id: string;
}
interface UploadDetails {
data: PreSignedData;
status: Number;
}
async function authenticate(): Promise<string> {
const authUrl = `${URL}/login`;
const authPayload = {
userName: USERNAME,
password: PASSWORD,
acceptedTerms: true,
};
const authResponse = await fetch(authUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(authPayload),
});
if (authResponse.status === 200) {
const authData: AuthResponse = (await authResponse.json()) as AuthResponse;
return authData.sessionToken;
} else {
throw new Error("Authentication failed.");
}
}
async function getPreSignedData(
authToken: string,
fileType: string,
fileName: string,
fileDescription: string,
originalFileName: string
): Promise<PreSignedData> {
const uploadUrl = `${URL}/files/upload`;
const uploadHeaders = {
Authorization: `JWT ${authToken}`,
Organization: ORGANIZATION_UID,
};
const uploadPayload = {
fileName,
productType: fileType,
fileDescription,
originalFileName,
};
try {
const uploadDetails: UploadDetails = await axios.post(
uploadUrl,
uploadPayload,
{
headers: uploadHeaders,
}
);
if (uploadDetails.status === 200) {
return uploadDetails.data;
} else {
throw new Error("Failed to get upload details.");
}
} catch (error) {
throw new Error("Failed to get upload details.");
}
}
export async function uploadFile(
fileType: string,
filePath: string,
fileName: string,
fileDescription: string,
originalFileName: string
): Promise<void> {
try {
// Get an authentication token by providing credentials
const authToken: string = await authenticate();
// Get pre-signed data to upload file to AWS S3
const preSignedData: PreSignedData = await getPreSignedData(
authToken,
fileType,
fileName,
fileDescription,
originalFileName
);
// Extract URL and fields from the pre-signed data response
const { url, fields } = preSignedData;
// Load file data
const fileData = fs.readFileSync(filePath);
// Prepare form data
const formData = new FormData();
// Append the pre-signed fields data
Object.entries(fields).forEach(([key, value]) => {
formData.append(key, String(value));
});
// Append fileData
formData.append("file", fileData);
const uploadResponse = await axios.post(url, formData, {
headers: {
"Content-Type": "multipart/form-data",
},
});
if (uploadResponse.status === 204) {
console.log("File uploaded successfully.");
} else {
console.log("Failed to upload file. Status code:", uploadResponse.status);
}
} catch (error) {
console.log("Error:", error.message);
}
}