@bytescale/sdk 3.0.0-alpha.9 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +388 -48
- package/dist/browser/cjs/main.js +704 -568
- package/dist/browser/esm/main.mjs +2803 -0
- package/dist/node/cjs/main.js +622 -240
- package/dist/node/esm/main.mjs +622 -240
- package/dist/types/index.d.ts +1 -1
- package/dist/types/private/AuthSessionState.d.ts +25 -8
- package/dist/types/private/EnvChecker.d.ts +1 -1
- package/dist/types/private/Mutex.d.ts +1 -1
- package/dist/types/private/StreamUtils.d.ts +1 -0
- package/dist/types/private/UploadManagerBase.d.ts +6 -1
- package/dist/types/private/UploadManagerBrowserWorkerBase.d.ts +19 -0
- package/dist/types/private/UploadManagerFetchUtils.d.ts +8 -0
- package/dist/types/private/model/AuthManagerInterface.d.ts +22 -5
- package/dist/types/private/model/AuthSession.d.ts +2 -2
- package/dist/types/private/model/OnPartProgress.d.ts +1 -1
- package/dist/types/private/model/UploadSourceProcessed.d.ts +6 -1
- package/dist/types/public/browser/AuthManagerBrowser.d.ts +10 -6
- package/dist/types/public/browser/UploadManagerBrowser.d.ts +3 -12
- package/dist/types/public/node/AuthManagerNode.d.ts +8 -3
- package/dist/types/public/shared/CommonTypes.d.ts +0 -1
- package/dist/types/public/shared/UrlBuilder.d.ts +10 -12
- package/dist/types/public/shared/UrlBuilderTypes.d.ts +0 -6
- package/dist/types/public/shared/generated/runtime.d.ts +7 -5
- package/dist/types/public/worker/UploadManagerWorker.d.ts +8 -0
- package/dist/types/public/worker/index.d.ts +2 -0
- package/dist/worker/cjs/main.js +2578 -0
- package/dist/worker/esm/main.mjs +2578 -0
- package/package.json +11 -4
- package/tests/UploadManager.test.ts +0 -7
- package/tests/UrlBuilder.test.ts +33 -27
package/README.md
CHANGED
|
@@ -1,76 +1,257 @@
|
|
|
1
|
-
|
|
1
|
+
<h1 align="center">
|
|
2
|
+
<a href="https://www.bytescale.com/docs/sdks/javascript">
|
|
3
|
+
<img alt="Bytescale JavaScript SDK" width="557" height="67" src="https://raw.githubusercontent.com/bytescale/bytescale-javascript-sdk/main/.github/assets/bytescale-javascript-sdk.svg">
|
|
4
|
+
</a>
|
|
5
|
+
</h1>
|
|
2
6
|
|
|
3
|
-
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://www.npmjs.com/package/@bytescale/sdk">
|
|
9
|
+
<img src="https://img.shields.io/badge/%40bytescale%2Fsdk-npm-4ba0f6" />
|
|
10
|
+
</a>
|
|
4
11
|
|
|
5
|
-
|
|
12
|
+
<a href="https://github.com/bytescale/bytescale-javascript-sdk/actions/workflows/ci.yml">
|
|
13
|
+
<img src="https://img.shields.io/badge/build-passing-4ba0f6" />
|
|
14
|
+
</a>
|
|
15
|
+
|
|
16
|
+
<a href="https://github.com/bytescale/bytescale-javascript-sdk/">
|
|
17
|
+
<img src="https://img.shields.io/badge/gzipped-9%20kb-4ba0f6" />
|
|
18
|
+
</a>
|
|
19
|
+
|
|
20
|
+
<a href="https://www.npmjs.com/package/@bytescale/sdk">
|
|
21
|
+
<img src="https://img.shields.io/npm/dt/upload-js?color=%234ba0f6" />
|
|
22
|
+
</a>
|
|
23
|
+
<br/>
|
|
24
|
+
|
|
25
|
+
<a href="https://www.npmjs.com/package/@bytescale/sdk">
|
|
26
|
+
<img src="https://img.shields.io/badge/TypeScript-included-4ba0f6" />
|
|
27
|
+
</a>
|
|
28
|
+
|
|
29
|
+
<a href="https://github.com/bytescale/bytescale-javascript-sdk/actions/workflows/ci.yml">
|
|
30
|
+
<img src="https://img.shields.io/npms-io/maintenance-score/upload-js?color=4ba0f6" />
|
|
31
|
+
</a>
|
|
32
|
+
|
|
33
|
+
<a target="_blank" href="https://twitter.com/intent/tweet?text=This%20was%20a%20great%20find...%0A%0Ahttps%3A%2F%2Fgithub.com%2Fbytescale%2Fbytescale-javascript-sdk">
|
|
34
|
+
<img alt="Twitter URL" src="https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2Fbytescale%2Fbytescale-javascript-sdk" />
|
|
35
|
+
</a>
|
|
36
|
+
|
|
37
|
+
</p>
|
|
38
|
+
|
|
39
|
+
<hr/>
|
|
40
|
+
|
|
41
|
+
<p align="center">
|
|
42
|
+
Use the Bytescale JavaScript SDK to upload, transform, and serve files at scale.
|
|
43
|
+
<br />
|
|
44
|
+
<br />
|
|
45
|
+
<a href="https://www.bytescale.com/docs/sdks/javascript" rel="nofollow"><b>Full SDK Documentation</b></a> • <a href="https://www.bytescale.com/docs/upload-widget" rel="nofollow">Upload Widget</a> • <a href="https://www.bytescale.com/docs/media-processing-apis" rel="nofollow">Media Processing APIs</a> • <a href="https://www.bytescale.com/docs/storage/sources" rel="nofollow">Storage</a> • <a href="https://www.bytescale.com/docs/cdn" rel="nofollow">CDN</a>
|
|
46
|
+
</p>
|
|
47
|
+
|
|
48
|
+
<hr/>
|
|
49
|
+
|
|
50
|
+
<img alt="Bytescale JavaScript SDK Example" src="https://raw.githubusercontent.com/bytescale/bytescale-javascript-sdk/main/.github/assets/bytescale-javascript-sdk-2.png">
|
|
6
51
|
|
|
7
52
|
## Installation
|
|
8
53
|
|
|
54
|
+
#### For Node.js:
|
|
55
|
+
|
|
9
56
|
```bash
|
|
10
|
-
npm install @bytescale/sdk
|
|
57
|
+
npm install @bytescale/sdk node-fetch
|
|
11
58
|
```
|
|
12
59
|
|
|
13
|
-
|
|
60
|
+
#### For Browsers:
|
|
14
61
|
|
|
15
62
|
```bash
|
|
16
|
-
npm install
|
|
63
|
+
npm install @bytescale/sdk
|
|
17
64
|
```
|
|
18
65
|
|
|
19
|
-
|
|
66
|
+
If you want to use a script tag:
|
|
20
67
|
|
|
21
|
-
|
|
68
|
+
```html
|
|
69
|
+
<script src="https://js.bytescale.com/sdk/v3"></script>
|
|
70
|
+
```
|
|
22
71
|
|
|
23
|
-
##
|
|
72
|
+
## Uploading Files
|
|
24
73
|
|
|
25
|
-
|
|
26
|
-
- [Download a File](#download-a-file)
|
|
27
|
-
- [Process a File](#process-a-file)
|
|
28
|
-
- [Get File Details](#get-file-details)
|
|
29
|
-
- [List Folder](#list-folder)
|
|
30
|
-
- **[See All Methods »](https://www.bytescale.com/docs/sdks/javascript)**
|
|
74
|
+
This library is isomorphic, which means you can upload files from Node.js, or the browser, or both.
|
|
31
75
|
|
|
32
|
-
|
|
76
|
+
#### From Node.js:
|
|
33
77
|
|
|
34
78
|
```javascript
|
|
35
79
|
import * as Bytescale from "@bytescale/sdk";
|
|
36
|
-
import
|
|
80
|
+
import nodeFetch from "node-fetch";
|
|
37
81
|
|
|
38
82
|
const uploadManager = new Bytescale.UploadManager({
|
|
39
|
-
fetchApi:
|
|
40
|
-
apiKey: "
|
|
83
|
+
fetchApi: nodeFetch, // import nodeFetch from "node-fetch"; // Only required for Node.js. TypeScript: 'nodeFetch as any' may be necessary.
|
|
84
|
+
apiKey: "free" // Get API keys from: www.bytescale.com
|
|
41
85
|
});
|
|
42
86
|
|
|
43
87
|
uploadManager
|
|
44
88
|
.upload({
|
|
45
|
-
|
|
46
|
-
// Supported types for 'data' field:
|
|
89
|
+
// Supported types:
|
|
47
90
|
// - String
|
|
48
91
|
// - Blob
|
|
49
|
-
// -
|
|
92
|
+
// - ArrayBuffer
|
|
50
93
|
// - Buffer
|
|
51
94
|
// - ReadableStream (Node.js), e.g. fs.createReadStream("file.txt")
|
|
52
|
-
data: "
|
|
95
|
+
data: "Hello World",
|
|
96
|
+
|
|
97
|
+
// ---------
|
|
98
|
+
// Optional:
|
|
99
|
+
// ---------
|
|
100
|
+
|
|
101
|
+
// Required if 'data' is a stream.
|
|
102
|
+
// size: 5098, // e.g. fs.statSync("file.txt").size
|
|
103
|
+
|
|
104
|
+
// Required if 'data' is a stream, buffer, or string.
|
|
105
|
+
mime: "text/plain",
|
|
106
|
+
|
|
107
|
+
// Required if 'data' is a stream, buffer, or string.
|
|
108
|
+
originalFileName: "my_file.txt"
|
|
109
|
+
|
|
110
|
+
// Reports progress: bytesTotal, bytesSent, progress.
|
|
111
|
+
// onProgress: ({ progress }) => console.log(progress),
|
|
112
|
+
|
|
113
|
+
// Controls multipart upload concurrency. Ignored if 'data' is a stream.
|
|
114
|
+
// maxConcurrentUploadParts: 4,
|
|
115
|
+
|
|
116
|
+
// Up to 2KB of arbitrary JSON.
|
|
117
|
+
// metadata: {
|
|
118
|
+
// productId: 60891
|
|
119
|
+
// },
|
|
120
|
+
|
|
121
|
+
// Up to 25 tags per file.
|
|
122
|
+
// tags: [
|
|
123
|
+
// "example_tag"
|
|
124
|
+
// ],
|
|
125
|
+
|
|
126
|
+
// About file paths:
|
|
127
|
+
// - Your API key's "file upload path" is used by default, and can be changed by editing the API key's settings.
|
|
128
|
+
// - You can override the API key's file upload path by specifying a path below.
|
|
129
|
+
// - You may use path variables (e.g. "{UTC_DAY}"): http://localhost:3201/docs/path-variables
|
|
130
|
+
// path: {
|
|
131
|
+
// folderPath: "/uploads/{UTC_YEAR}/{UTC_MONTH}/{UTC_DAY}",
|
|
132
|
+
// fileName: "{UTC_TIME_TOKEN_INVERSE}{UNIQUE_DIGITS_2}{ORIGINAL_FILE_EXT}"
|
|
133
|
+
// },
|
|
134
|
+
|
|
135
|
+
// Set to 'isCancelled = true' after invoking 'upload' to cancel the upload.
|
|
136
|
+
// cancellationToken: {
|
|
137
|
+
// isCancelled: false
|
|
138
|
+
// }
|
|
53
139
|
})
|
|
54
140
|
.then(
|
|
55
|
-
|
|
56
|
-
|
|
141
|
+
({ fileUrl, filePath }) => {
|
|
142
|
+
// --------------------------------------------
|
|
143
|
+
// File successfully uploaded!
|
|
144
|
+
// --------------------------------------------
|
|
145
|
+
// The 'filePath' uniquely identifies the file,
|
|
146
|
+
// and is what you should save to your DB.
|
|
147
|
+
// --------------------------------------------
|
|
148
|
+
console.log(`File uploaded to: ${fileUrl}`);
|
|
149
|
+
},
|
|
150
|
+
error => console.error(`Error: ${error.message}`, error)
|
|
57
151
|
);
|
|
58
152
|
```
|
|
59
153
|
|
|
60
|
-
|
|
154
|
+
#### From the Browser:
|
|
155
|
+
|
|
156
|
+
```html
|
|
157
|
+
<html>
|
|
158
|
+
<head>
|
|
159
|
+
<script src="https://js.bytescale.com/sdk/v3"></script>
|
|
160
|
+
<script>
|
|
161
|
+
// import * as Bytescale from "@bytescale/sdk"
|
|
162
|
+
const uploadManager = new Bytescale.UploadManager({
|
|
163
|
+
apiKey: "free" // Get API keys from: www.bytescale.com
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
const onFileSelected = async event => {
|
|
167
|
+
const file = event.target.files[0];
|
|
168
|
+
|
|
169
|
+
try {
|
|
170
|
+
const { fileUrl, filePath } = await uploadManager.upload({
|
|
171
|
+
// Supported types:
|
|
172
|
+
// - String
|
|
173
|
+
// - Blob
|
|
174
|
+
// - ArrayBuffer
|
|
175
|
+
// - File (i.e. from a DOM file input element)
|
|
176
|
+
data: file
|
|
177
|
+
|
|
178
|
+
// ---------
|
|
179
|
+
// Optional:
|
|
180
|
+
// ---------
|
|
181
|
+
|
|
182
|
+
// Required if 'data' is a stream. (Not required for DOM file inputs, blobs, buffers, or strings.)
|
|
183
|
+
// size: 5098, // e.g. fs.statSync("file.txt").size
|
|
184
|
+
|
|
185
|
+
// Required if 'data' is a stream, buffer, or string. (Not required for DOM file inputs or blobs.)
|
|
186
|
+
// mime: "application/octet-stream",
|
|
187
|
+
|
|
188
|
+
// Required if 'data' is a stream, buffer, or string. (Not required for DOM file inputs or blobs.)
|
|
189
|
+
// originalFileName: "my_file.txt",
|
|
190
|
+
|
|
191
|
+
// Reports progress: bytesTotal, bytesSent, progress.
|
|
192
|
+
// onProgress: ({ progress }) => console.log(progress),
|
|
193
|
+
|
|
194
|
+
// Controls multipart upload concurrency. Ignored if 'data' is a stream.
|
|
195
|
+
// maxConcurrentUploadParts: 4,
|
|
196
|
+
|
|
197
|
+
// Up to 2KB of arbitrary JSON.
|
|
198
|
+
// metadata: {
|
|
199
|
+
// productId: 60891
|
|
200
|
+
// },
|
|
201
|
+
|
|
202
|
+
// Up to 25 tags per file.
|
|
203
|
+
// tags: [
|
|
204
|
+
// "example_tag"
|
|
205
|
+
// ],
|
|
206
|
+
|
|
207
|
+
// About file paths:
|
|
208
|
+
// - Your API key's "file upload path" is used by default, and can be changed by editing the API key's settings.
|
|
209
|
+
// - You can override the API key's file upload path by specifying a path below.
|
|
210
|
+
// - You may use path variables (e.g. "{UTC_DAY}"): http://localhost:3201/docs/path-variables
|
|
211
|
+
// path: {
|
|
212
|
+
// folderPath: "/uploads/{UTC_YEAR}/{UTC_MONTH}/{UTC_DAY}",
|
|
213
|
+
// fileName: "{UTC_TIME_TOKEN_INVERSE}{UNIQUE_DIGITS_2}{ORIGINAL_FILE_EXT}"
|
|
214
|
+
// },
|
|
215
|
+
|
|
216
|
+
// Set to 'isCancelled = true' after invoking 'upload' to cancel the upload.
|
|
217
|
+
// cancellationToken: {
|
|
218
|
+
// isCancelled: false
|
|
219
|
+
// }
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
// --------------------------------------------
|
|
223
|
+
// File successfully uploaded!
|
|
224
|
+
// --------------------------------------------
|
|
225
|
+
// The 'filePath' uniquely identifies the file,
|
|
226
|
+
// and is what you should save to your API.
|
|
227
|
+
// --------------------------------------------
|
|
228
|
+
alert(`File uploaded:\n${fileUrl}`);
|
|
229
|
+
} catch (e) {
|
|
230
|
+
alert(`Error:\n${e.message}`);
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
</script>
|
|
234
|
+
</head>
|
|
235
|
+
<body>
|
|
236
|
+
<input type="file" onchange="onFileSelected(event)" />
|
|
237
|
+
</body>
|
|
238
|
+
</html>
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## Downloading Files
|
|
61
242
|
|
|
62
243
|
```javascript
|
|
63
244
|
import * as Bytescale from "@bytescale/sdk";
|
|
64
|
-
import
|
|
245
|
+
import nodeFetch from "node-fetch"; // Only required for Node.js
|
|
65
246
|
|
|
66
247
|
const fileApi = new Bytescale.FileApi({
|
|
67
|
-
fetchApi:
|
|
68
|
-
apiKey: "
|
|
248
|
+
fetchApi: nodeFetch, // import nodeFetch from "node-fetch"; // Only required for Node.js. TypeScript: 'nodeFetch as any' may be necessary.
|
|
249
|
+
apiKey: "YOUR_API_KEY" // e.g. "secret_xxxxx"
|
|
69
250
|
});
|
|
70
251
|
|
|
71
252
|
fileApi
|
|
72
253
|
.downloadFile({
|
|
73
|
-
accountId: "
|
|
254
|
+
accountId: "YOUR_ACCOUNT_ID", // e.g. "W142hJk"
|
|
74
255
|
filePath: "/uploads/2022/12/25/hello_world.txt"
|
|
75
256
|
})
|
|
76
257
|
.then(response => response.text()) // .text() | .json() | .blob() | .stream()
|
|
@@ -80,24 +261,31 @@ fileApi
|
|
|
80
261
|
);
|
|
81
262
|
```
|
|
82
263
|
|
|
83
|
-
|
|
264
|
+
Use the [`UrlBuilder`](#urlbuilder) to get a URL to your file (if you need a URL instead of downloading the file).
|
|
84
265
|
|
|
85
|
-
|
|
266
|
+
## Processing Files
|
|
86
267
|
|
|
87
268
|
```javascript
|
|
88
269
|
import * as Bytescale from "@bytescale/sdk";
|
|
89
|
-
import fetch from "node-fetch"; // Node.js
|
|
270
|
+
import fetch from "node-fetch"; // Only required for Node.js
|
|
271
|
+
import fs from "fs";
|
|
90
272
|
|
|
91
273
|
const fileApi = new Bytescale.FileApi({
|
|
92
|
-
fetchApi:
|
|
93
|
-
apiKey: "
|
|
274
|
+
fetchApi: nodeFetch, // import nodeFetch from "node-fetch"; // Only required for Node.js. TypeScript: 'nodeFetch as any' may be necessary.
|
|
275
|
+
apiKey: "YOUR_API_KEY" // e.g. "secret_xxxxx"
|
|
94
276
|
});
|
|
95
277
|
|
|
96
278
|
fileApi
|
|
97
279
|
.processFile({
|
|
98
|
-
accountId: "
|
|
280
|
+
accountId: "YOUR_ACCOUNT_ID", // e.g. "W142hJk"
|
|
99
281
|
filePath: "/uploads/2022/12/25/image.jpg",
|
|
100
|
-
|
|
282
|
+
|
|
283
|
+
// See: https://www.bytescale.com/docs/image-processing-api
|
|
284
|
+
transformation: "image",
|
|
285
|
+
transformationParams: {
|
|
286
|
+
w: 800,
|
|
287
|
+
h: 600
|
|
288
|
+
}
|
|
101
289
|
})
|
|
102
290
|
.then(response => response.stream()) // .text() | .json() | .blob() | .stream()
|
|
103
291
|
.then(
|
|
@@ -115,22 +303,22 @@ fileApi
|
|
|
115
303
|
);
|
|
116
304
|
```
|
|
117
305
|
|
|
118
|
-
|
|
306
|
+
Use the [`UrlBuilder`](#urlbuilder) to get a URL to your file (if you need a URL instead of downloading the file).
|
|
119
307
|
|
|
120
|
-
|
|
308
|
+
## Get File Details
|
|
121
309
|
|
|
122
310
|
```javascript
|
|
123
311
|
import * as Bytescale from "@bytescale/sdk";
|
|
124
|
-
import fetch from "node-fetch"; // Node.js
|
|
312
|
+
import fetch from "node-fetch"; // Only required for Node.js
|
|
125
313
|
|
|
126
314
|
const fileApi = new Bytescale.FileApi({
|
|
127
|
-
fetchApi:
|
|
128
|
-
apiKey: "
|
|
315
|
+
fetchApi: nodeFetch, // import nodeFetch from "node-fetch"; // Only required for Node.js. TypeScript: 'nodeFetch as any' may be necessary.
|
|
316
|
+
apiKey: "YOUR_API_KEY" // e.g. "secret_xxxxx"
|
|
129
317
|
});
|
|
130
318
|
|
|
131
319
|
fileApi
|
|
132
320
|
.getFileDetails({
|
|
133
|
-
accountId: "
|
|
321
|
+
accountId: "YOUR_ACCOUNT_ID", // e.g. "W142hJk"
|
|
134
322
|
filePath: "/uploads/2022/12/25/image.jpg"
|
|
135
323
|
})
|
|
136
324
|
.then(
|
|
@@ -139,20 +327,20 @@ fileApi
|
|
|
139
327
|
);
|
|
140
328
|
```
|
|
141
329
|
|
|
142
|
-
|
|
330
|
+
## Listing Folders
|
|
143
331
|
|
|
144
332
|
```javascript
|
|
145
333
|
import * as Bytescale from "@bytescale/sdk";
|
|
146
|
-
import fetch from "node-fetch"; // Node.js
|
|
334
|
+
import fetch from "node-fetch"; // Only required for Node.js
|
|
147
335
|
|
|
148
336
|
const folderApi = new Bytescale.FolderApi({
|
|
149
|
-
fetchApi:
|
|
150
|
-
apiKey: "
|
|
337
|
+
fetchApi: nodeFetch, // import nodeFetch from "node-fetch"; // Only required for Node.js. TypeScript: 'nodeFetch as any' may be necessary.
|
|
338
|
+
apiKey: "YOUR_API_KEY" // e.g. "secret_xxxxx"
|
|
151
339
|
});
|
|
152
340
|
|
|
153
341
|
folderApi
|
|
154
342
|
.listFolder({
|
|
155
|
-
accountId: "
|
|
343
|
+
accountId: "YOUR_ACCOUNT_ID", // e.g. "W142hJk"
|
|
156
344
|
folderPath: "/",
|
|
157
345
|
recursive: false
|
|
158
346
|
})
|
|
@@ -163,6 +351,158 @@ folderApi
|
|
|
163
351
|
);
|
|
164
352
|
```
|
|
165
353
|
|
|
354
|
+
## More Operations
|
|
355
|
+
|
|
356
|
+
For a complete list of operations, please see:
|
|
357
|
+
|
|
358
|
+
**[Bytescale JavaScript SDK Docs »](https://www.bytescale.com/docs/sdks/javascript)**
|
|
359
|
+
|
|
360
|
+
## Authorization
|
|
361
|
+
|
|
362
|
+
The Bytescale JavaScript SDK supports two types of authorization:
|
|
363
|
+
|
|
364
|
+
### API Keys
|
|
365
|
+
|
|
366
|
+
The Bytescale JavaScript SDK automatically adds the `apiKey` from the constructor to the `Authorization` header for all requests made via the SDK.
|
|
367
|
+
|
|
368
|
+
With API key auth, the requester has access to the resources available to the API key:
|
|
369
|
+
|
|
370
|
+
- Secret API keys (`secret_***`) have access to all API endpoints.
|
|
371
|
+
|
|
372
|
+
- Public API keys (`public_***`) have access to file upload, file download, and file listing API endpoints. File overwrites, file deletes, and all other destructive operations cannot be performed using public API keys. File listing is also disabled by default (but can be changed in the API key's settings).
|
|
373
|
+
|
|
374
|
+
Each Public API Key and Secret API Key can have its read/write access limited to a subset of files/folders.
|
|
375
|
+
|
|
376
|
+
### JWT Cookies
|
|
377
|
+
|
|
378
|
+
JWT cookies are optional.
|
|
379
|
+
|
|
380
|
+
With JWT cookies, the user can download private files directly via the URL, as authorization is performed implicitly via a session cookie. This allows the browser to display private files in `<img>` and `<video>` elements.
|
|
381
|
+
|
|
382
|
+
With JWT cookies, the user can also perform API requests (e.g. file uploads) granted by the [JWT's payload](https://www.bytescale.com/docs/types/BytescaleJwt). This is because the Bytescale JavaScript SDK automatically injects the user's JWT into the `authorization-token` request header for all API requests, assuming the `AuthManager.beginAuthSession` method has been called.
|
|
383
|
+
|
|
384
|
+
_Note: when using JWT cookies to download files, the `?auth=true` query parameter must be added to the URL._
|
|
385
|
+
|
|
386
|
+
[Learn more about the `AuthManager` and JWT cookies »](https://www.bytescale.com/docs/authorization#jwt-cookie)
|
|
387
|
+
|
|
388
|
+
## UrlBuilder
|
|
389
|
+
|
|
390
|
+
Use the `UrlBuilder` to construct URLs for your uploaded files:
|
|
391
|
+
|
|
392
|
+
```javascript
|
|
393
|
+
import { UrlBuilder } from "@bytescale/sdk";
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
#### Raw Files
|
|
397
|
+
|
|
398
|
+
To get the URL for the uploaded image `/example.jpg` in its original form, use the following params:
|
|
399
|
+
|
|
400
|
+
```javascript
|
|
401
|
+
// Returns: "https://upcdn.io/1234abc/raw/example.jpg"
|
|
402
|
+
UrlBuilder.url({
|
|
403
|
+
accountId: "1234abc",
|
|
404
|
+
filePath: "/example.jpg"
|
|
405
|
+
});
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
#### Images
|
|
409
|
+
|
|
410
|
+
To resize the uploaded image `/example.jpg` to 800x600, use the following params:
|
|
411
|
+
|
|
412
|
+
```javascript
|
|
413
|
+
// Returns: "https://upcdn.io/1234abc/image/example.jpg?w=800&h=600"
|
|
414
|
+
UrlBuilder.url({
|
|
415
|
+
accountId: "1234abc",
|
|
416
|
+
filePath: "/example.jpg",
|
|
417
|
+
options: {
|
|
418
|
+
transformation: "image",
|
|
419
|
+
transformationParams: {
|
|
420
|
+
w: 800,
|
|
421
|
+
h: 600
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
[Image Processing API Docs »](https://www.bytescale.com/docs/image-processing-api)
|
|
428
|
+
|
|
429
|
+
#### Videos
|
|
430
|
+
|
|
431
|
+
To transcode the uploaded video `/example.mov` to MP4/H.264 in HD, use the following params:
|
|
432
|
+
|
|
433
|
+
```javascript
|
|
434
|
+
// Returns: "https://upcdn.io/1234abc/video/example.mov?f=mp4-h264&h=1080"
|
|
435
|
+
UrlBuilder.url({
|
|
436
|
+
accountId: "1234abc",
|
|
437
|
+
filePath: "/example.mov",
|
|
438
|
+
options: {
|
|
439
|
+
transformation: "video",
|
|
440
|
+
transformationParams: {
|
|
441
|
+
f: "mp4-h264",
|
|
442
|
+
h: 1080
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
[Video Processing API Docs »](https://www.bytescale.com/docs/video-processing-api)
|
|
449
|
+
|
|
450
|
+
#### Audio
|
|
451
|
+
|
|
452
|
+
To transcode the uploaded audio `/example.wav` to AAC in 192kbps, use the following params:
|
|
453
|
+
|
|
454
|
+
```javascript
|
|
455
|
+
// Returns: "https://upcdn.io/1234abc/audio/example.wav?f=aac&br=192"
|
|
456
|
+
UrlBuilder.url({
|
|
457
|
+
accountId: "1234abc",
|
|
458
|
+
filePath: "/example.wav",
|
|
459
|
+
options: {
|
|
460
|
+
transformation: "audio",
|
|
461
|
+
transformationParams: {
|
|
462
|
+
f: "aac",
|
|
463
|
+
br: 192
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
[Audio Processing API Docs »](https://www.bytescale.com/docs/audio-processing-api)
|
|
470
|
+
|
|
471
|
+
#### Archives
|
|
472
|
+
|
|
473
|
+
To extract the file `document.docx` from the uploaded ZIP file `/example.zip`, use the following params:
|
|
474
|
+
|
|
475
|
+
```javascript
|
|
476
|
+
// Returns: "https://upcdn.io/1234abc/archive/example.zip?m=extract&artifact=/document.docx"
|
|
477
|
+
UrlBuilder.url({
|
|
478
|
+
accountId: "1234abc",
|
|
479
|
+
filePath: "/example.zip",
|
|
480
|
+
options: {
|
|
481
|
+
transformation: "archive",
|
|
482
|
+
transformationParams: {
|
|
483
|
+
m: "extract"
|
|
484
|
+
},
|
|
485
|
+
artifact: "/document.docx"
|
|
486
|
+
}
|
|
487
|
+
});
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
[Archive Processing API Docs »](https://www.bytescale.com/docs/archive-processing-api)
|
|
491
|
+
|
|
492
|
+
## 🙋 Can I use my own storage?
|
|
493
|
+
|
|
494
|
+
Bytescale supports AWS S3, Cloudflare R2, Google Storage, DigitalOcean Spaces, and Bytescale Storage.
|
|
495
|
+
|
|
496
|
+
**[Bytescale Storage Docs »](https://www.bytescale.com/docs/storage/sources)**
|
|
497
|
+
|
|
498
|
+
**[Bytescale JavaScript SDK Docs »](https://www.bytescale.com/docs/sdks/javascript)**
|
|
499
|
+
|
|
500
|
+
## 👋 Create your Bytescale Account
|
|
501
|
+
|
|
502
|
+
Bytescale is the best way to upload, transform, and serve images, videos, and audio at scale.
|
|
503
|
+
|
|
504
|
+
**[Create a Bytescale account »](https://www.bytescale.com/get-started)**
|
|
505
|
+
|
|
166
506
|
## License
|
|
167
507
|
|
|
168
508
|
[MIT](LICENSE)
|