transloadit.mdx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. ---
  2. sidebar_position: 1
  3. slug: /transloadit
  4. ---
  5. import Tabs from '@theme/Tabs';
  6. import TabItem from '@theme/TabItem';
  7. import UppyCdnExample from '/src/components/UppyCdnExample';
  8. # Transloadit
  9. The `@uppy/transloadit` plugin can be used to upload files directly to
  10. [Transloadit](https://transloadit.com/) for all kinds of processing, such as
  11. transcoding video, resizing images, zipping/unzipping, [and much
  12. more][transloadit-services].
  13. ## When should I use it?
  14. :::tip
  15. Not sure which uploader is best for you? Read
  16. “[Choosing the uploader you need](/docs/guides/choosing-uploader)”.
  17. :::
  18. Transloadit’s strength is versatility. By doing video, audio, images, documents,
  19. and more, you only need one vendor for [all your file processing
  20. needs][transloadit-services]. The `@uppy/transloadit` plugin directly uploads to
  21. Transloadit so you only have to worry about creating a
  22. [Template][transloadit-concepts]. Transloadit accepts the files, processes
  23. according to the instructions in the Template, and stores the results in storage
  24. of your choosing, such as a self-owned S3 bucket. The Transloadit plugin uses
  25. [Tus](/docs/tus) under the hood so you don’t have to sacrifice reliable,
  26. resumable uploads.
  27. You should use `@uppy/transloadit` if you don’t want to host your own Tus or
  28. Companion servers, (optionally) need file processing, and store it in the
  29. service (such as S3 or GCS) of your liking. All with minimal effort.
  30. ## Install
  31. <Tabs>
  32. <TabItem value="npm" label="NPM" default>
  33. ```shell
  34. npm install @uppy/transloadit
  35. ```
  36. </TabItem>
  37. <TabItem value="yarn" label="Yarn">
  38. ```shell
  39. yarn add @uppy/transloadit
  40. ```
  41. </TabItem>
  42. <TabItem value="cdn" label="CDN">
  43. <UppyCdnExample>
  44. {`
  45. import { Uppy, Transloadit } from "{{UPPY_JS_URL}}"
  46. new Uppy().use(Transloadit, { /* see options */ })
  47. `}
  48. </UppyCdnExample>
  49. </TabItem>
  50. </Tabs>
  51. ## Use
  52. A quick overview of the complete API.
  53. ```js {10-17} showLineNumbers
  54. import Uppy from '@uppy/core';
  55. import Dashboard from '@uppy/dashboard';
  56. import Transloadit from '@uppy/transloadit';
  57. import '@uppy/core/dist/style.min.css';
  58. import '@uppy/dashboard/dist/style.min.css';
  59. const uppy = new Uppy()
  60. .use(Dashboard, { inline: true, target: 'body' })
  61. .use(Transloadit, {
  62. assemblyOptions: {
  63. params: {
  64. auth: { key: 'your-transloadit-key' },
  65. template_id: 'your-template-id',
  66. },
  67. },
  68. });
  69. // Optionally listen to events
  70. uppy.on('transloadit:assembly-created', (assembly, fileIDs) => {});
  71. uppy.on('transloadit:upload', (file, assembly) => {});
  72. uppy.on('transloadit:assembly-executing', (assembly) => {});
  73. uppy.on('transloadit:result', (stepName, result, assembly) => {});
  74. uppy.on('transloadit:complete', (assembly) => {});
  75. ```
  76. ### Use with Companion
  77. :::note
  78. All [Transloadit plans](https://transloadit.com/pricing/) come with a hosted
  79. version of Companion.
  80. :::
  81. You can use this plugin together with Transloadit’s hosted Companion service to
  82. let your users import files from third party sources across the web. To do so
  83. each provider plugin must be configured with Transloadit’s Companion URLs:
  84. ```js
  85. import { COMPANION_URL, COMPANION_ALLOWED_HOSTS } from '@uppy/transloadit';
  86. import Dropbox from '@uppy/dropbox';
  87. uppy.use(Dropbox, {
  88. companionUrl: COMPANION_URL,
  89. companionAllowedHosts: COMPANION_ALLOWED_HOSTS,
  90. });
  91. ```
  92. This will already work. Transloadit’s OAuth applications are used to
  93. authenticate your users by default. Your users will be asked to provide
  94. Transloadit access to their files. Since your users are probably not aware of
  95. Transloadit, this may be confusing or decrease trust. You may also hit rate
  96. limits, because the OAuth application is shared between everyone using
  97. Transloadit.
  98. To solve that, you can use your own OAuth keys with Transloadit’s hosted
  99. Companion servers by using Transloadit Template Credentials. [Create a Template
  100. Credential][template-credentials] on the Transloadit site. Select “Companion
  101. OAuth” for the service, and enter the key and secret for the provider you want
  102. to use. Then you can pass the name of the new credentials to that provider:
  103. ```js
  104. import { COMPANION_URL, COMPANION_ALLOWED_HOSTS } from '@uppy/transloadit';
  105. import Dropbox from '@uppy/dropbox';
  106. uppy.use(Dropbox, {
  107. companionUrl: COMPANION_URL,
  108. companionAllowedHosts: COMPANION_ALLOWED_HOSTS,
  109. companionKeysParams: {
  110. key: 'YOUR_TRANSLOADIT_API_KEY',
  111. credentialsName: 'my_companion_dropbox_creds',
  112. },
  113. });
  114. ```
  115. ## API
  116. ### Options
  117. #### `id`
  118. A unique identifier for this plugin (`string`, default: `'Transloadit'`).
  119. #### `service`
  120. The Transloadit API URL to use (`string`, default:
  121. `https://api2.transloadit.com`).
  122. The default will try to route traffic efficiently based on the location of your
  123. users. You could for instance set it to `https://api2-us-east-1.transloadit.com`
  124. if you need the traffic to stay inside a particular region.
  125. #### `limit`
  126. Limit the amount of uploads going on at the same time (`number`, default: `20`).
  127. Setting this to `0` means no limit on concurrent uploads, but we recommend a
  128. value between `5` and `20`. This option is passed through to the
  129. [`@uppy/tus`](/docs/tus) plugin, which this plugin uses internally.
  130. #### `assemblyOptions`
  131. Configure the
  132. [Assembly Instructions](https://transloadit.com/docs/topics/assembly-instructions/),
  133. the fields to send along to the assembly, and authentication
  134. (`object | function`, default: `null`).
  135. The object you can pass or return from a function has this structure:
  136. ```js
  137. {
  138. params: {
  139. auth: { key: 'key-from-transloadit' },
  140. template_id: 'id-from-transloadit',
  141. steps: {
  142. // Overruling Template at runtime
  143. },
  144. notify_url: 'https://your-domain.com/assembly-status',
  145. },
  146. signature: 'generated-signature',
  147. fields: {
  148. // Dynamic or static fields to send along
  149. },
  150. }
  151. ```
  152. - `params` is used to authenticate with Transloadit and using your desired
  153. [template](https://transloadit.com/docs/topics/templates/).
  154. - `auth.key` _(required)_ is your authentication key which you can find on the
  155. “Credentials” page of your account.
  156. - `template_id` _(required)_ is the unique identifier to use the right
  157. template from your account.
  158. - `steps` _(optional)_ can be used to
  159. [overrule Templates at runtime](https://transloadit.com/docs/topics/templates/#overruling-templates-at-runtime).
  160. A typical use case might be changing the storage path on the fly based on
  161. the session user id. For most use cases, we recommend to let your Templates
  162. handle dynamic cases (they can accept `fields` and execute arbitrary
  163. JavaScript as well), and not pass in `steps` from a browser. The template
  164. editor also has extra validations and context.
  165. - `notify_url` _(optional)_ is a pingback with the assembly status as JSON.
  166. For instance, if you don’t want to block the user experience by letting them
  167. wait for your template to complete with
  168. [`waitForEncoding`](#waitForEncoding), but you do want to want to
  169. asynchrounously have an update, you can provide an URL which will be
  170. “pinged” with the assembly status.
  171. - `signature` _(optional, but recommended)_ is a cryptographic signature to
  172. provide further trust in unstrusted environments. Refer to
  173. “[Signature Authentication”](https://transloadit.com/docs/topics/signature-authentication/)
  174. for more information.
  175. - `fields` _(optional)_ can be used to to send along key/value pairs, which can
  176. be
  177. [used dynamically in your template](https://transloadit.com/docs/topics/assembly-instructions/#form-fields-in-instructions).
  178. :::info
  179. All your files end up in a single assembly and your `fields` are available
  180. globally in your template. The metadata in your Uppy files is also sent along so
  181. you can do things dynamically per file with `file.user_meta` in your template.
  182. :::
  183. <details>
  184. <summary>Examples</summary>
  185. **As a function**
  186. Most likely you want to use a function to call your backend to generate a
  187. signature and return your configuration.
  188. ```js
  189. uppy.use(Transloadit, {
  190. async assemblyOptions(file) {
  191. const res = await fetch('/transloadit-params');
  192. return res.json();
  193. },
  194. });
  195. ```
  196. **As an object**
  197. If you don’t need to change anything dynamically, you can also pass an object
  198. directly.
  199. ```js
  200. uppy.use(Transloadit, {
  201. assemblyOptions: {
  202. params: { auth: { key: 'transloadit-key' } },
  203. },
  204. });
  205. ```
  206. **Use with @uppy/form**
  207. Combine the `assemblyOptions()` option with the [Form](/docs/form) plugin to
  208. pass user input from a `<form>` to a Transloadit Assembly:
  209. ```js
  210. // This will add form field values to each file's `.meta` object:
  211. uppy.use(Form, { getMetaFromForm: true });
  212. uppy.use(Transloadit, {
  213. async assemblyOptions() {
  214. const res = await fetch('/transloadit-params');
  215. return res.json();
  216. };
  217. });
  218. ```
  219. </details>
  220. :::caution
  221. When you go to production always make sure to set the `signature`. **Not using
  222. [Signature Authentication](https://transloadit.com/docs/topics/signature-authentication/)
  223. can be a security risk**. Signature Authentication is a security measure that
  224. can prevent outsiders from tampering with your Assembly Instructions. While
  225. Signature Authentication is not implemented (yet), we recommend to disable
  226. `allow_steps_override` in your Templates to avoid outsiders being able to pass
  227. in any Instructions and storage targets on your behalf.
  228. :::
  229. #### `waitForEncoding`
  230. Wait for the template to finish, rather than only the upload, before marking the
  231. upload complete (`boolean`, default: `false`).
  232. - When `false`, the Assemblies will complete (or error) in the background but
  233. Uppy won’t know or care about it. You may have to let Transloadit ping you via
  234. a `notify_url` and asynchronously inform your user (email, in-app
  235. notification).
  236. - When `true`, the Transloadit plugin waits for Assemblies to complete before
  237. the files are marked as completed. This means users have to wait for a
  238. potentially long time, depending on how complicated your Assembly instructions
  239. are. But, you can receive the final status and transcoding results on the
  240. client side with less effort.
  241. When this is enabled, you can listen for the
  242. [`transloadit:result`](#transloaditresult) and
  243. [`transloadit:complete`](#transloaditcomplete) events.
  244. #### `waitForMetadata`
  245. Wait for Transloadit’s backend to catch early errors, not the entire Assembly to
  246. complete. (`boolean`, default: `false`)
  247. When set to `true`, the Transloadit plugin waits for Transloadit’s backend to
  248. extract metadata from all the uploaded files. This is mostly handy if you want
  249. to have a quick user experience (so your users don’t necessarily need to wait
  250. for all the encoding to complete), but you do want to let users know about some
  251. types of errors that can be caught early on, like file format issues.
  252. You you can listen for the [`transloadit:upload`](#transloaditupload) event when
  253. this or `waitForEncoding` is enabled.
  254. #### `importFromUploadURLs`
  255. Allow another plugin to upload files, and then import those files into the
  256. Transloadit Assembly (`boolean`, default: `false`).
  257. When enabling this option, Transloadit will _not_ configure the Tus plugin to
  258. upload to Transloadit. Instead, a separate upload plugin must be used. Once the
  259. upload completes, the Transloadit plugin adds the uploaded file to the Assembly.
  260. For example, to upload files to an S3 bucket and then transcode them:
  261. ```js
  262. uppy.use(AwsS3, {
  263. getUploadParameters(file) {
  264. return {
  265. /* upload parameters */
  266. };
  267. },
  268. });
  269. uppy.use(Transloadit, {
  270. importFromUploadURLs: true,
  271. assemblyOptions: {
  272. params: {
  273. auth: { key: 'YOUR_API_KEY' },
  274. template_id: 'YOUR_TEMPLATE_ID',
  275. },
  276. },
  277. });
  278. ```
  279. Tranloadit will download the files and expose them to your Template as
  280. `:original`, as if they were directly uploaded from the Uppy client.
  281. :::note
  282. For this to work, the upload plugin must assign a publicly accessible
  283. `uploadURL` property to the uploaded file object. The Tus and S3 plugins both do
  284. this automatically, but you must configure your S3 bucket to have publicly
  285. readable objects. For the XHRUpload plugin, you may have to specify a custom
  286. `getResponseData` function.
  287. :::
  288. #### `alwaysRunAssembly`
  289. Always create and run an Assembly when `uppy.upload()` is called, even if no
  290. files were selected (`boolean`, default: `false`).
  291. This allows running Assemblies that do not receive files, but instead use a
  292. robot like [`/s3/import`](https://transloadit.com/docs/transcoding/#s3-import)
  293. to download the files from elsewhere, for example, for a bulk transcoding job.
  294. #### `locale`
  295. ```js
  296. export default {
  297. strings: {
  298. // Shown while Assemblies are being created for an upload.
  299. creatingAssembly: 'Preparing upload...',
  300. // Shown if an Assembly could not be created.
  301. creatingAssemblyFailed: 'Transloadit: Could not create Assembly',
  302. // Shown after uploads have succeeded, but when the Assembly is still executing.
  303. // This only shows if `waitForMetadata` or `waitForEncoding` was enabled.
  304. encoding: 'Encoding...',
  305. },
  306. };
  307. ```
  308. #### `clientName`
  309. Append a custom client name to the `Transloadit-Client` header field when
  310. creating an Assembly (`string`, default: `null`).
  311. The `Transloadit-Client` header includes by default information about the used
  312. SDK and is included in the Assembly Status under the `transloadit_client`
  313. property. By providing a value, such as `homepage-file-uploader`, you can
  314. identify the client and SDK that created a given Assembly.
  315. <details>
  316. <summary>Deprecated options</summary>
  317. These options have been deprecated in favor of
  318. [`assemblyOptions`](#assemblyoptions), which we now recommend for all use cases.
  319. You can still use these options, but they will be removed in the next major
  320. version.
  321. #### `getAssemblyOptions`
  322. This function behaves the same as passing a function to
  323. [`assemblyOptions`](#assemblyoptions).
  324. #### `params`
  325. The Assembly parameters to use for the upload (`object`, default: `null`) See
  326. the Transloadit documentation on
  327. [Assembly Instructions](https://transloadit.com/docs/#14-assembly-instructions)
  328. for further information.
  329. The `auth.key` Assembly parameter is required. You can also use the `steps` or
  330. `template_id` options here as described in the Transloadit documentation.
  331. ```js
  332. uppy.use(Transloadit, {
  333. params: {
  334. auth: { key: 'YOUR_TRANSLOADIT_KEY' },
  335. steps: {
  336. encode: {
  337. robot: '/video/encode',
  338. use: {
  339. steps: [':original'],
  340. fields: ['file_input_field2'],
  341. },
  342. preset: 'iphone',
  343. },
  344. },
  345. },
  346. });
  347. ```
  348. #### `signature`
  349. An optional signature for the Assembly parameters. See the Transloadit
  350. documentation on
  351. [Signature Authentication](https://transloadit.com/docs/#26-signature-authentication)
  352. for further information.
  353. If a `signature` is provided, `params` should be a JSON string instead of a
  354. JavaScript object, as otherwise the generated JSON in the browser may be
  355. different from the JSON string that was used to generate the signature.
  356. #### `fields`
  357. An object of form fields to send along to the Assembly. Keys are field names,
  358. and values are field values. See also the Transloadit documentation on
  359. [Form Fields In Instructions](https://transloadit.com/docs/#23-form-fields-in-instructions).
  360. ```js
  361. uppy.use(Transloadit, {
  362. // ...
  363. fields: {
  364. message: 'This is a form field',
  365. },
  366. });
  367. ```
  368. You can also pass an array of field names to send global or file metadata along
  369. to the Assembly. Global metadata is set using the
  370. [`meta` option](/docs/uppy/#meta) in the Uppy constructor, or using the
  371. [`setMeta` method](/docs/uppy/#uppy-setMeta-data). File metadata is set using
  372. the [`setFileMeta`](/docs/uppy/#uppy-setFileMeta-fileID-data) method. The
  373. [Form](/docs/form) plugin also sets global metadata based on the values of
  374. `<input />`s in the form, providing a handy way to use values from HTML form
  375. fields:
  376. ```js
  377. uppy.use(Form, { target: 'form#upload-form', getMetaFromForm: true });
  378. uppy.use(Transloadit, {
  379. fields: ['field_name', 'other_field_name'],
  380. params: {
  381. /* ... */
  382. },
  383. });
  384. ```
  385. Form fields can also be computed dynamically using custom logic, by using the
  386. [`getAssemblyOptions(file)`](/docs/transloadit/#getAssemblyOptions-file) option.
  387. </details>
  388. ### Static exports
  389. #### `COMPANION_URL`
  390. The main endpoint for Transloadit’s hosted companions. You can use this constant
  391. in remote provider options, like so:
  392. ```js
  393. import Dropbox from '@uppy/dropbox';
  394. import { COMPANION_URL } from '@uppy/transloadit';
  395. uppy.use(Dropbox, {
  396. companionUrl: COMPANION_URL,
  397. });
  398. ```
  399. When using `COMPANION_URL`, you should also configure
  400. [`companionAllowedHosts`](#companion_allowed_hosts).
  401. The value of this constant is `https://api2.transloadit.com/companion`. If you
  402. are using a custom [`service`](#service) option, you should also set a custom
  403. host option in your provider plugins, by taking a Transloadit API url and
  404. appending `/companion`:
  405. ```js
  406. uppy.use(Dropbox, {
  407. companionUrl: 'https://api2-us-east-1.transloadit.com/companion',
  408. });
  409. ```
  410. #### `COMPANION_ALLOWED_HOSTS`
  411. A RegExp pattern matching Transloadit’s hosted companion endpoints. The pattern
  412. is used in remote provider `companionAllowedHosts` options, to make sure that
  413. third party authentication messages cannot be faked by an attacker’s page but
  414. can only originate from Transloadit’s servers.
  415. Use it whenever you use `companionUrl: COMPANION_URL`, like so:
  416. ```js
  417. import Dropbox from '@uppy/dropbox';
  418. import { COMPANION_ALLOWED_HOSTS } from '@uppy/transloadit';
  419. uppy.use(Dropbox, {
  420. companionAllowedHosts: COMPANION_ALLOWED_HOSTS,
  421. });
  422. ```
  423. The value of this constant covers _all_ Transloadit’s Companion servers, so it
  424. does not need to be changed if you are using a custom [`service`](#service)
  425. option. But, if you are not using the Transloadit Companion servers at
  426. `*.transloadit.com`, make sure to set the `companionAllowedHosts` option to
  427. something that matches what you do use.
  428. ### Events
  429. #### `transloadit:assembly-created`
  430. Fired when an Assembly is created.
  431. **Parameters**
  432. - `assembly` - The initial [Assembly Status][assembly-status].
  433. - `fileIDs` - The IDs of the files that will be uploaded to this Assembly.
  434. ```js
  435. uppy.on('transloadit:assembly-created', (assembly, fileIDs) => {
  436. console.group('Created', assembly.assembly_id, 'for files:');
  437. for (const id of fileIDs) {
  438. console.log(uppy.getFile(id).name);
  439. }
  440. console.groupEnd();
  441. });
  442. ```
  443. #### `transloadit:upload`
  444. Fired when Transloadit has received an upload. Requires
  445. [`waitForMetadata`](#waitformetadata) to be set.
  446. **Parameters**
  447. - `file` - The Transloadit file object that was uploaded.
  448. - `assembly` - The [Assembly Status][assembly-status] of the Assembly to which
  449. the file was uploaded.
  450. #### `transloadit:assembly-executing`
  451. Fired when Transloadit has received all uploads, and is executing the Assembly.
  452. **Parameters**
  453. - `assembly` - The
  454. [Assembly Status](https://transloadit.com/docs/api/#assembly-status-response)
  455. of the Assembly that is executing.
  456. #### `transloadit:result`
  457. Fired when a result came in from an Assembly. Requires
  458. [`waitForEncoding`](#waitforencoding) to be set.
  459. **Parameters**
  460. - `stepName` - The name of the Assembly step that generated this result.
  461. - `result` - The result object from Transloadit. This result object has one more
  462. property, namely `localId`. This is the ID of the file in Uppy’s local state,
  463. and can be used with `uppy.getFile(id)`.
  464. - `assembly` - The [Assembly Status][assembly-status] of the Assembly that
  465. generated this result.
  466. ```js
  467. uppy.on('transloadit:result', (stepName, result) => {
  468. const file = uppy.getFile(result.localId);
  469. document.body.appendChild(html`
  470. <div>
  471. <h2>From ${file.name}</h2>
  472. <a href=${result.ssl_url}> View </a>
  473. </div>
  474. `);
  475. });
  476. ```
  477. #### `transloadit:complete`
  478. Fired when an Assembly completed. Requires [`waitForEncoding`](#waitForEncoding)
  479. to be set.
  480. **Parameters**
  481. - `assembly` - The final [Assembly Status][assembly-status] of the completed
  482. Assembly.
  483. ```js
  484. uppy.on('transloadit:complete', (assembly) => {
  485. // Could do something fun with this!
  486. console.log(assembly.results);
  487. });
  488. ```
  489. ## Frequently Asked Questions
  490. ### Accessing the assembly when an error occurred
  491. If an error occurs when an Assembly has already started, you can find the
  492. Assembly Status on the error object’s `assembly` property.
  493. ```js
  494. uppy.on('error', (error) => {
  495. if (error.assembly) {
  496. console.log(`Assembly ID ${error.assembly.assembly_id} failed!`);
  497. console.log(error.assembly);
  498. }
  499. });
  500. ```
  501. ### Assembly behavior when Uppy is closed
  502. When integrating `@uppy/transloadit` with `@uppy/dashboard`, closing the
  503. dashboard will result in continuing assemblies on the server. When the user
  504. manually cancels the upload any running assemblies will be cancelled.
  505. [assembly-status]: https://transloadit.com/docs/api/#assembly-status-response
  506. [template-credentials]:
  507. https://transloadit.com/docs/#how-to-create-template-credentials
  508. [transloadit-services]: https://transloadit.com/services/
  509. [transloadit-concepts]: https://transloadit.com/docs/getting-started/concepts/