使用資源金鑰存取連結共用的雲端硬碟檔案
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以使用
Drive UI 或
Google Drive API,與他人共用 Google 雲端硬碟檔案和資料夾。透過雲端硬碟共用檔案時,您可以指定共用對象可否編輯檔案、對檔案加註,或只能開啟檔案。
資源金鑰有助於防止非預期的人士存取檔案。資源金鑰是傳遞的額外參數,可讓使用者存取透過連結共用的特定檔案。如果使用者未曾檢視該檔案,就必須提供資源金鑰才能存取。如果使用者最近曾查看檔案,或是可以直接存取檔案,就不需要資源金鑰。
如果雲端硬碟檔案是透過連結共用,只有可存取該檔案的使用者才能透過 type=user
或 type=group
permissions
資源找到檔案。如果使用者只具備 type=domain
或 type=anyone
權限,只能透過連結存取這些檔案,可能需要資源金鑰才能存取。
如要進一步瞭解權限,請參閱共用檔案、資料夾和雲端硬碟。如需角色完整清單和各角色允許執行的作業,請參閱「角色與權限」。
從檔案讀取資源金鑰
在 files
資源的唯讀 resourceKey
欄位中,雲端硬碟 API 會傳回檔案的資源金鑰。
如果檔案是雲端硬碟捷徑,系統會在唯讀的 shortcutDetails.targetResourceKey
欄位中傳回捷徑目標的資源金鑰。
files
資源中會傳回網址的欄位 (例如 exportLinks
、webContentLink
和 webViewLink
) 也包含 resourceKey
。整合雲端硬碟 UI 的用戶端也可以在 state
參數中使用 resourceKeys
。詳情請參閱「下載及匯出檔案」。
在要求中設定資源鍵
要求參照的任何檔案,其資源金鑰都會在 X-Goog-Drive-Resource-Keys
HTTP 標頭中設定。
向 Drive API 發出的要求可透過 X-Goog-Drive-Resource-Keys
HTTP 標頭指定一或多個資源金鑰。
語法
檔案 ID 和資源金鑰組會使用正斜線 (/
) 分隔符號,設定在標頭中。標頭是透過逗號 (,
) 分隔符號,合併所有檔案 ID 和資源鍵值組建而成。
舉例來說,假設您要求將檔案 fileId1
從資料夾 fileId2
移至資料夾 fileId3
。假設這三個檔案的資源鍵分別為 resourceKey1
、resourceKey2
和 resourceKey3
。使用正斜線和逗號分隔符號,從這些值建構的標頭如下:
X-Goog-Drive-Resource-Keys: fileId1/resourceKey1,fileId2/resourceKey2,fileId3/resourceKey3
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間:2025-08-29 (世界標準時間)。"],[],[],null,["# Access link-shared Drive files using resource keys\n\nYou can share Google Drive files and folders with others using the [Drive UI](/workspace/drive/api/guides/about-apps) or through the [Google Drive API](/workspace/drive/api/guides/create-file). When you share from\nDrive, you can control whether people can edit, comment on, or\nonly open the file.\n\nA *resource key* helps protect your file from unintended access. Resource keys\nare an additional parameter that are passed so users can access certain files\nthat have been shared using a link. Users who haven't viewed the file before\nmust provide the resource key to gain access. Those who have recently viewed the\nfile, or have direct access, don't need the resource key to access the file.\n\nA Drive file that's shared with a link can only be discovered by\nusers that can access the file as a result of a `type=user` or `type=group`\n[`permissions`](/workspace/drive/api/reference/rest/v3/permissions) resource. Requests from users\nthat only have access to these link-shared files using a `type=domain` or\n`type=anyone` permission might require a resource key.\n\nFor more information about permissions, see [Share files, folders and drives](/workspace/drive/api/guides/manage-sharing). For a complete list of roles and the operations\npermitted by each, see [Roles \\& permissions](/workspace/drive/api/guides/ref-roles).\n\nRead the resource key from the file\n-----------------------------------\n\nThe Drive API returns a file's resource key on the read-only\n`resourceKey` field of the [`files`](/workspace/drive/api/reference/rest/v3/files) resource.\n\nIf the file is a [Drive shortcut](/workspace/drive/api/guides/shortcuts), the\nresource key for the shortcut target is returned on the read-only\n`shortcutDetails.targetResourceKey` field.\n\nFields in the [`files`](/workspace/drive/api/reference/rest/v3/files) resource that return URLs,\nsuch as `exportLinks`, `webContentLink`, and `webViewLink`, also include the\n`resourceKey`. Clients that integrate with the Drive UI can also\nuse `resourceKeys` within the [`state`](/workspace/drive/api/guides/enable-sdk#construct)\nparameter. For more information, see [Download and export\nfiles](/workspace/drive/api/guides/manage-downloads).\n\nSet the resource key on the request\n-----------------------------------\n\nResource keys for any files referenced by requests to the Drive API\nare set on the `X-Goog-Drive-Resource-Keys` HTTP header.\n\nRequests to the Drive API can specify one or more resource keys with\nthe `X-Goog-Drive-Resource-Keys` HTTP header.\n\n### Syntax\n\nA file ID and resource key pair are set on the header using a forward slash\n(`/`) separator. The header is built by combining all the file ID and resource\nkey pairs using comma (`,`) separators.\n\nFor example, consider a request to move file `fileId1` from folder `fileId2` to\nfolder `fileId3`. Assume the resource keys for these three files are\n`resourceKey1`, `resourceKey2`, and `resourceKey3`, respectively. The header\nbuilt from these values using a forward slash and comma separators is: \n\n X-Goog-Drive-Resource-Keys: fileId1/resourceKey1,fileId2/resourceKey2,fileId3/resourceKey3\n\n### Related topics\n\n- [Share files, folders and drives](/workspace/drive/api/guides/manage-sharing)\n- [Download and export files](/workspace/drive/api/guides/manage-downloads)\n- [Protect file content](/workspace/drive/api/guides/content-restrictions)\n- [Configure a Drive UI integration](/workspace/drive/api/guides/enable-sdk)"]]