google-drive.mdx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. ---
  2. slug: /google-drive
  3. ---
  4. import Tabs from '@theme/Tabs';
  5. import TabItem from '@theme/TabItem';
  6. import UppyCdnExample from '/src/components/UppyCdnExample';
  7. # Google Drive
  8. The `@uppy/google-drive` plugin lets users import files from their
  9. [Google Drive](https://drive.google.com) account.
  10. :::tip
  11. [Try out the live example](/examples) or take it for a spin in
  12. [StackBlitz](https://stackblitz.com/edit/vitejs-vite-zaqyaf?file=main.js).
  13. :::
  14. ## When should I use this?
  15. When you want to let users import files from their
  16. [Google Drive](https://drive.google.com) account.
  17. A [Companion](/docs/companion) instance is required for the Google Drive plugin
  18. to work. Companion handles authentication with Google Drive, downloads the
  19. files, and uploads them to the destination. This saves the user bandwidth,
  20. especially helpful if they are on a mobile connection.
  21. You can self-host Companion or get a hosted version with any
  22. [Transloadit plan](https://transloadit.com/pricing/).
  23. <Tabs>
  24. <TabItem value="npm" label="NPM" default>
  25. ```shell
  26. npm install @uppy/google-drive
  27. ```
  28. </TabItem>
  29. <TabItem value="yarn" label="Yarn">
  30. ```shell
  31. yarn add @uppy/google-drive
  32. ```
  33. </TabItem>
  34. <TabItem value="cdn" label="CDN">
  35. <UppyCdnExample>
  36. {`
  37. import { Uppy, GoogleDrive } from "{{UPPY_JS_URL}}"
  38. const uppy = new Uppy()
  39. uppy.use(GoogleDrive, {
  40. // Options
  41. })
  42. `}
  43. </UppyCdnExample>
  44. </TabItem>
  45. </Tabs>
  46. ## Use
  47. Using Google Drive requires setup in both Uppy and Companion.
  48. ### Use in Uppy
  49. ```js {10-13} showLineNumbers
  50. import Uppy from '@uppy/core';
  51. import Dashboard from '@uppy/dashboard';
  52. import GoogleDrive from '@uppy/google-drive';
  53. import '@uppy/core/dist/style.min.css';
  54. import '@uppy/dashboard/dist/style.min.css';
  55. new Uppy()
  56. .use(Dashboard, { inline: true, target: '#dashboard' })
  57. .use(GoogleDrive, { companionUrl: 'https://your-companion.com' });
  58. ```
  59. ### Use with Transloadit
  60. ```js
  61. import { COMPANION_URL, COMPANION_ALLOWED_HOSTS } from '@uppy/transloadit';
  62. import GoogleDrive from '@uppy/google-drive';
  63. uppy.use(GoogleDrive, {
  64. companionUrl: COMPANION_URL,
  65. companionAllowedHosts: COMPANION_ALLOWED_HOSTS,
  66. });
  67. ```
  68. You may also hit rate limits, because the OAuth application is shared between
  69. everyone using Transloadit.
  70. To solve that, you can use your own OAuth keys with Transloadit’s hosted
  71. Companion servers by using Transloadit Template Credentials. [Create a Template
  72. Credential][template-credentials] on the Transloadit site. Select “Companion
  73. OAuth” for the service, and enter the key and secret for the provider you want
  74. to use. Then you can pass the name of the new credentials to that provider:
  75. ```js
  76. import { COMPANION_URL, COMPANION_ALLOWED_HOSTS } from '@uppy/transloadit';
  77. import GoogleDrive from '@uppy/google-drive';
  78. uppy.use(GoogleDrive, {
  79. companionUrl: COMPANION_URL,
  80. companionAllowedHosts: COMPANION_ALLOWED_HOSTS,
  81. companionKeysParams: {
  82. key: 'YOUR_TRANSLOADIT_API_KEY',
  83. credentialsName: 'my_companion_dropbox_creds',
  84. },
  85. });
  86. ```
  87. ### Use in Companion
  88. To sign up for API keys, go to the
  89. [Google Developer Console](https://console.developers.google.com/).
  90. Create a project for your app if you don’t have one yet.
  91. - On the project’s dashboard,
  92. [enable the Google Drive API](https://developers.google.com/drive/api/v3/enable-drive-api).
  93. - [Set up OAuth authorization](https://developers.google.com/drive/api/v3/about-auth).
  94. - Under scopes, add the `https://www.googleapis.com/auth/drive.readonly` Drive
  95. API scope.
  96. - Due to this being a sensitive scope, your app must complete Google’s OAuth
  97. app verification before being granted access. See
  98. [OAuth App Verification Help Center](https://support.google.com/cloud/answer/13463073)
  99. for more information.
  100. The app page has a `"Redirect URIs"` field. Here, add:
  101. ```
  102. https://$YOUR_COMPANION_HOST_NAME/drive/redirect
  103. ```
  104. If you are using Transloadit hosted Companion:
  105. ```
  106. https://api2.transloadit.com/companion/drive/redirect
  107. ```
  108. Google will give you an OAuth client ID and client secret.
  109. Configure the Google key and secret in Companion. With the standalone Companion
  110. server, specify environment variables:
  111. ```shell
  112. export COMPANION_GOOGLE_KEY="Google OAuth client ID"
  113. export COMPANION_GOOGLE_SECRET="Google OAuth client secret"
  114. ```
  115. When using the Companion Node.js API, configure these options:
  116. ```js
  117. companion.app({
  118. providerOptions: {
  119. drive: {
  120. key: 'Google OAuth client ID',
  121. secret: 'Google OAuth client secret',
  122. },
  123. },
  124. });
  125. ```
  126. ## API
  127. ### Options
  128. #### `id`
  129. A unique identifier for this plugin (`string`, default: `'GoogleDrive'`).
  130. #### `title`
  131. Title / name shown in the UI, such as Dashboard tabs (`string`, default:
  132. `'GoogleDrive'`).
  133. #### `target`
  134. DOM element, CSS selector, or plugin to place the drag and drop area into
  135. (`string`, `Element`, `Function`, or `UIPlugin`, default:
  136. [`Dashboard`](/docs/dashboard)).
  137. #### `companionUrl`
  138. URL to a [Companion](/docs/companion) instance (`string`, default: `null`).
  139. #### `companionHeaders`
  140. Custom headers that should be sent along to [Companion](/docs/companion) on
  141. every request (`Object`, default: `{}`).
  142. #### `companionAllowedHosts`
  143. The valid and authorised URL(s) from which OAuth responses should be accepted
  144. (`string` or `RegExp` or `Array`, default: `companionUrl`).
  145. This value can be a `string`, a `RegExp` pattern, or an `Array` of both. This is
  146. useful when you have your [Companion](/docs/companion) running on several hosts.
  147. Otherwise, the default value should do fine.
  148. #### `companionCookiesRule`
  149. This option correlates to the
  150. [RequestCredentials value](https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials)
  151. (`string`, default: `'same-origin'`).
  152. This tells the plugin whether to send cookies to [Companion](/docs/companion).
  153. #### `locale`
  154. ```js
  155. export default {
  156. strings: {
  157. pluginNameGoogleDrive: 'GoogleDrive',
  158. },
  159. };
  160. ```
  161. [template-credentials]:
  162. https://transloadit.com/docs/#how-to-create-template-credentials