google-drive.mdx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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://www.drive.google.com) account.
  10. :::tip
  11. [Try out the live example](/examples) or take it for a spin in
  12. [CodeSandbox](https://codesandbox.io/s/uppy-dashboard-xpxuhd).
  13. :::
  14. ## When should I use this?
  15. When you want to let users import files from their
  16. [Google Drive](https://www.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, {
  58. target: Dashboard,
  59. companionUrl: 'https://your-companion.com',
  60. });
  61. ```
  62. ### Use in Companion
  63. To sign up for API keys, go to the
  64. [Google Developer Console](https://console.developers.google.com/).
  65. Create a project for your app if you don’t have one yet.
  66. - On the project’s dashboard,
  67. [enable the Google Drive API](https://developers.google.com/drive/api/v3/enable-drive-api).
  68. - [Set up OAuth authorization](https://developers.google.com/drive/api/v3/about-auth).
  69. - Under scopes, add the `https://www.googleapis.com/auth/drive.readonly` Drive
  70. API scope.
  71. - Due to this being a sensitive scope, your app must complete Google’s OAuth
  72. app verification before being granted access. See
  73. [OAuth App Verification Help Center](https://support.google.com/cloud/answer/13463073)
  74. for more information.
  75. The app page has a `"Redirect URIs"` field. Here, add:
  76. ```
  77. https://$YOUR_COMPANION_HOST_NAME/drive/redirect
  78. ```
  79. If you are using Transloadit hosted Companion:
  80. ```
  81. https://api2.transloadit.com/companion/drive/redirect
  82. ```
  83. Google will give you an OAuth client ID and client secret.
  84. Configure the Google Drive key and secret in Companion. With the standalone
  85. Companion server, specify environment variables:
  86. ```shell
  87. export COMPANION_GOOGLE_KEY="Google Drive OAuth client ID"
  88. export COMPANION_GOOGLE_SECRET="Google Drive OAuth client secret"
  89. ```
  90. When using the Companion Node.js API, configure these options:
  91. ```js
  92. companion.app({
  93. providerOptions: {
  94. drive: {
  95. key: 'Google Drive OAuth client ID',
  96. secret: 'Google Drive OAuth client secret',
  97. },
  98. },
  99. });
  100. ```
  101. ## API
  102. ### Options
  103. #### `id`
  104. A unique identifier for this plugin (`string`, default: `'GoogleDrive'`).
  105. #### `title`
  106. Title / name shown in the UI, such as Dashboard tabs (`string`, default:
  107. `'GoogleDrive'`).
  108. #### `target`
  109. DOM element, CSS selector, or plugin to place the drag and drop area into
  110. (`string` or `Element`, default: `null`).
  111. #### `companionUrl`
  112. URL to a [Companion](/docs/companion) instance (`string`, default: `null`).
  113. #### `companionHeaders`
  114. Custom headers that should be sent along to [Companion](/docs/companion) on
  115. every request (`Object`, default: `{}`).
  116. #### `companionAllowedHosts`
  117. The valid and authorised URL(s) from which OAuth responses should be accepted
  118. (`string` or `RegExp` or `Array`, default: `companionUrl`).
  119. This value can be a `string`, a `RegExp` pattern, or an `Array` of both. This is
  120. useful when you have your [Companion](/docs/companion) running on several hosts.
  121. Otherwise, the default value should do fine.
  122. #### `companionCookiesRule`
  123. This option correlates to the
  124. [RequestCredentials value](https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials)
  125. (`string`, default: `'same-origin'`).
  126. This tells the plugin whether to send cookies to [Companion](/docs/companion).
  127. #### `locale`
  128. ```js
  129. export default {
  130. strings: {
  131. pluginNameGoogleDrive: 'GoogleDrive',
  132. },
  133. };
  134. ```