Uppy Google Drive 插件
@uppy/google-drive
插件允许用户从他们的 Google Drive 账户导入文件。
何时使用此插件?
当你想让用户从他们的 Google Drive 账户导入文件时。
使用 Google Drive 插件需要一个 Companion 实例。Companion 负责与 Google Drive 进行身份验证、下载文件并上传到目标位置。这节省了用户的带宽,特别是当他们在移动连接上时非常有用。
你可以自托管 Companion 或者通过任何 Transloadit 计划 获取托管版本。
npm
npm install @uppy/google-drive
yarn
yarn add @uppy/google-drive
CDN
警告
此捆绑包包含了大部分Uppy插件,因此不建议在生产环境中使用此方法,因为尽管您可能仅仅用到其中一小部分插件,但用户却需要下载全部插件。
然而,这对于加速您的开发环境非常有帮助,所以在起步阶段,请放心使用它来提升开发速度。<!-- 1. Add CSS to `<head>` --> <link href="https://releases.transloadit.com/uppy/v3.27.3/uppy.min.css" rel="stylesheet"> <!-- 2. Initialize --> <div id="uppy"></div> <script type="module"> import { Uppy, GoogleDrive } from "https://releases.transloadit.com/uppy/v3.27.3/uppy.min.mjs" const uppy = new Uppy() uppy.use(GoogleDrive, { // Options }) </script>
使用方法
使用 Google Drive 需要在 Uppy 和 Companion 中进行设置。
在 Uppy 中使用
import Uppy from "@uppy/core";
import Dashboard from "@uppy/dashboard";
import GoogleDrive from "@uppy/google-drive";
import "@uppy/core/dist/style.min.css";
import "@uppy/dashboard/dist/style.min.css";
new Uppy()
.use(Dashboard, { inline: true, target: "#dashboard" })
.use(GoogleDrive, { companionUrl: "https://your-companion.com" });
在 Companion 中使用
要注册 API 密钥,请前往 Google 开发者控制台。
如果你还没有项目,为你的应用创建一个项目。
- 在项目的仪表板上,启用 Google Drive API 。
- 设置 OAuth 授权 。
- 在 “范围” 下,添加
https://www.googleapis.com/auth/drive.readonly
Drive API 范围 。 - 由于这是一个敏感范围,你的应用必须完成 Google 的 OAuth 应用验证才能获得访问权限。更多详情请参阅 OAuth 应用验证帮助中心 。
- 在 “范围” 下,添加
应用页面有一个 “重定向 URI” 字段。在这里添加:
https://$YOUR_COMPANION_HOST_NAME/drive/redirect
```text
如果你使用的是 Transloadit 托管的 Companion:
```text
https://api2.transloadit.com/companion/drive/redirect
Google 会提供给你一个 OAuth 客户端 ID 和客户端密钥。
在 Companion 中配置 Google 密钥和密钥。对于独立的 Companion 服务器,指定环境变量:
export COMPANION_GOOGLE_KEY="Google OAuth 客户端 ID"
export COMPANION_GOOGLE_SECRET="Google OAuth 客户端密钥"
当使用 Companion Node.js API 时,配置这些选项:
companion.app({
providerOptions: {
drive: {
key: "Google OAuth 客户端 ID",
secret: "Google OAuth 客户端密钥",
},
},
});
API
选项
id
此插件的唯一标识符( string
,默认值:'GoogleDrive'
)。
title
在 UI 中显示的标题/名称,如 Dashboard 标签( string
,默认值:'GoogleDrive'
)。
target
放置拖放区域的 DOM 元素、CSS 选择器或插件( string
,Element
,Function
,或 UIPlugin
,默认值:Dashboard
)。
companionUrl
指向 Companion 实例的 URL( string
,默认值:null
)。
companionHeaders
应随每次请求一起发送到 Companion 的自定义头( Object
,默认值:{}
)。
companionAllowedHosts
从 OAuth 响应应被接受的有效和授权的 URL( string
或RegExp
或Array
,默认:companionUrl
)。
此值可以是 string
、RegExp
模式,或是两者的 Array
。当你在多个主机上运行 Companion 时,这非常有用。否则,默认值应该足够好。
companionCookiesRule
此选项对应于 RequestCredentials 值( string
,默认:'same-origin'
)。
这告诉插件是否向 Companion 发送 cookies。
locale
export default {
strings: {
pluginNameGoogleDrive: "GoogleDrive",
},
};