จัดการไดรฟ์ที่แชร์

เอกสารนี้มีงานที่เกี่ยวข้องกับการจัดการไดรฟ์ที่แชร์ เช่น การสร้างไดรฟ์ที่แชร์และการจัดการสมาชิกและสิทธิ์ โดยใช้ Google Drive API

หากต้องการระบุช่องที่จะแสดงผลในการตอบกลับ คุณสามารถตั้งค่า fields พารามิเตอร์ ระบบ ด้วยเมธอดใดก็ได้ของทรัพยากร drives หากไม่ได้ระบุพารามิเตอร์ fields เซิร์ฟเวอร์จะแสดงผลชุดช่องเริ่มต้นที่เฉพาะเจาะจงกับเมธอด ตัวอย่างเช่น เมธอด list จะแสดงเฉพาะช่อง kind, id, และ name สำหรับไดรฟ์ที่แชร์แต่ละรายการ ดูข้อมูลเพิ่มเติมได้ที่ แสดงผล ช่องที่เฉพาะเจาะจง

ดูข้อมูลเพิ่มเติมเกี่ยวกับขีดจำกัดของโฟลเดอร์ในไดรฟ์ที่แชร์ได้ที่ขีดจำกัดของโฟลเดอร์ในไดรฟ์ที่แชร์

ข้อกำหนดเบื้องต้น

ก่อนที่จะสร้างและจัดการไดรฟ์ที่แชร์ได้ โปรดตรวจสอบว่าคุณมีคุณสมบัติตรงตามข้อกำหนดต่อไปนี้

  • บัญชี Google Workspace: ไดรฟ์ที่แชร์พร้อมใช้งานใน Google Workspace รุ่นที่รองรับ (เช่น Business Standard, Business Plus, Enterprise และ Education) บัญชี Google ส่วนบุคคล (@gmail.com) จะสร้างไดรฟ์ที่แชร์ไม่ได้
  • สิทธิ์ในการสร้าง: ผู้ดูแลระบบโดเมนสามารถกำหนดค่าได้ว่าจะอนุญาตให้ผู้ใช้ในองค์กรสร้างไดรฟ์ที่แชร์ได้หรือไม่ ผู้ใช้ที่ได้รับการตรวจสอบสิทธิ์ต้องอยู่ในหน่วยขององค์กรที่ได้รับอนุญาตให้สร้างไดรฟ์ที่แชร์
  • ขอบเขต OAuth 2.0: แอปของคุณต้องได้รับอนุญาตด้วยขอบเขต https://www.googleapis.com/auth/drive
  • บทบาท: เมื่อผู้ใช้สร้างไดรฟ์ที่แชร์ ระบบจะกำหนดบทบาท organizer ให้ผู้ใช้โดยอัตโนมัติในไดรฟ์ที่แชร์นั้น การจัดการการตั้งค่าไดรฟ์ การเปลี่ยนชื่อไดรฟ์ การเพิ่มหรือนำสมาชิกออก และการลบไดรฟ์ที่แชร์ทั้งหมดต้องใช้บทบาท organizer ดูข้อมูลเพิ่มเติมเกี่ยวกับ บทบาทได้ที่บทบาทสำหรับไดรฟ์ที่แชร์
  • สิทธิ์เข้าถึงของผู้ดูแลระบบโดเมน: ผู้ดูแลระบบโดเมนสามารถจัดการไดรฟ์ที่แชร์ใดก็ได้ภายในองค์กร ไม่ว่าจะเป็นสมาชิกหรือไม่ก็ตาม โดยส่ง useDomainAdminAccess=true ดูข้อมูลเพิ่มเติมได้ที่ จัดการไดรฟ์ที่แชร์ในฐานะผู้ดูแลระบบโดเมน

สร้างไดรฟ์ที่แชร์

หากต้องการสร้างไดรฟ์ที่แชร์ ให้ใช้ create เมธอดในทรัพยากร drives พร้อมพารามิเตอร์ requestId

พารามิเตอร์ requestId จะระบุความพยายามเชิงตรรกะในการสร้างไดรฟ์ที่แชร์แบบ Idempotent หากคำขอหมดเวลาหรือแสดงข้อผิดพลาดแบ็กเอนด์ที่ไม่แน่นอน คุณสามารถส่งคำขอเดิมซ้ำได้และระบบจะไม่สร้างรายการที่ซ้ำกัน requestId และเนื้อหาของคำขอต้องเหมือนเดิม

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้างไดรฟ์ที่แชร์

Java

drive/snippets/drive_v3/src/main/java/CreateDrive.java
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.Drive;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;

/* class to demonstrate use-case of Drive's create drive. */
public class CreateDrive {

  /**
   * Create a drive.
   *
   * @return Newly created drive id.
   * @throws IOException if service account credentials file not found.
   */
  public static String createDrive() throws IOException {
        /*Load pre-authorized user credentials from the environment.
        TODO(developer) - See https://developers.google.com/identity for
        guides on implementing OAuth2 for your application.*/
    GoogleCredentials credentials =
        GoogleCredentials.getApplicationDefault().createScoped(Arrays.asList(DriveScopes.DRIVE));
    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
        credentials);

    // Build a new authorized API client service.
    com.google.api.services.drive.Drive service =
        new com.google.api.services.drive.Drive.Builder(new NetHttpTransport(),
            GsonFactory.getDefaultInstance(),
            requestInitializer)
            .setApplicationName("Drive samples")
            .build();

    Drive driveMetadata = new Drive();
    driveMetadata.setName("Project Resources");
    String requestId = UUID.randomUUID().toString();
    try {
      Drive drive = service.drives().create(requestId,
              driveMetadata)
          .execute();
      System.out.println("Drive ID: " + drive.getId());

      return drive.getId();
    } catch (GoogleJsonResponseException e) {
      // TODO(developer) - handle error appropriately
      System.err.println("Unable to create drive: " + e.getDetails());
      throw e;
    }
  }
}

Python

drive/snippets/drive-v3/drive_snippet/create_drive.py
import uuid

import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def create_drive():
  """Create a drive.
  Returns:
      Id of the created drive

  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """
  creds, _ = google.auth.default()

  try:
    # create drive api client
    service = build("drive", "v3", credentials=creds)

    drive_metadata = {"name": "Project Resources"}
    request_id = str(uuid.uuid4())
    # pylint: disable=maybe-no-member
    drive = (
        service.drives()
        .create(body=drive_metadata, requestId=request_id, fields="id")
        .execute()
    )
    print(f'Drive ID: {drive.get("id")}')

  except HttpError as error:
    print(f"An error occurred: {error}")
    drive = None

  return drive.get("id")


if __name__ == "__main__":
  create_drive()

Node.js

drive/snippets/drive_v3/drive_snippets/create_drive.js
import {GoogleAuth} from 'google-auth-library';
import {google} from 'googleapis';
import {v4 as uuid} from 'uuid';

/**
 * Creates a new shared drive.
 * @return {Promise<string>} The ID of the created shared drive.
 */
async function createDrive() {
  // Authenticate with Google and get an authorized client.
  // TODO (developer): Use an appropriate auth mechanism for your app.
  const auth = new GoogleAuth({
    scopes: 'https://www.googleapis.com/auth/drive',
  });

  // Create a new Drive API client (v3).
  const service = google.drive({version: 'v3', auth});

  // The metadata for the new shared drive.
  const driveMetadata = {
    name: 'Project resources',
  };

  // A unique request ID to avoid creating duplicate shared drives.
  const requestId = uuid();

  // Create the new shared drive.
  const Drive = await service.drives.create({
    requestBody: driveMetadata,
    requestId,
    fields: 'id',
  });

  // Print the ID of the new shared drive.
  console.log('Drive Id:', Drive.data.id);
  if (!Drive.data.id) {
    throw new Error('Drive ID not found.');
  }
  return Drive.data.id;
}

PHP

drive/snippets/drive_v3/src/DriveCreateDrive.php
<?php
use Google\Client;
use Google\Service\Drive;
use Ramsey\Uuid\Uuid;
function createDrive()
{
    try {
        $client = new Client();
        $client->useApplicationDefaultCredentials();
        $client->addScope(Drive::DRIVE);
        $driveService = new Drive($client);

        $driveMetadata = new Drive\Drive(array(
                'name' => 'Project Resources'));
        $requestId = Uuid::uuid4()->toString();
        $drive = $driveService->drives->create($requestId, $driveMetadata, array(
                'fields' => 'id'));
        printf("Drive ID: %s\n", $drive->id);
        return $drive->id;
    } catch(Exception $e)  {
        echo "Error Message: ".$e;
    }  

}

.NET

drive/snippets/drive_v3/DriveV3Snippets/CreateDrive.cs
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Drive.v3.Data;
using Google.Apis.Services;

namespace DriveV3Snippets
{
    // Class to demonstrate use of Drive's create drive.
    public class CreateDrive
    {
        /// <summary>
        /// Create a drive.
        /// </summary>
        /// <returns>newly created drive Id.</returns>
        public static string DriveCreateDrive()
        {
            try
            {
                /* Load pre-authorized user credentials from the environment.
                 TODO(developer) - See https://developers.google.com/identity for
                 guides on implementing OAuth2 for your application. */
                GoogleCredential credential = GoogleCredential.GetApplicationDefault()
                    .CreateScoped(DriveService.Scope.Drive);

                // Create Drive API service.
                var service = new DriveService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Drive API Snippets"
                });

                var driveMetadata = new Drive()
                {
                    Name = "Project Resources"
                };
                var requestId = Guid.NewGuid().ToString();
                var request = service.Drives.Create(driveMetadata, requestId);
                request.Fields = "id";
                var drive = request.Execute();
                Console.WriteLine("Drive ID: " + drive.Id);
                return drive.Id;
            }
            catch (Exception e)
            {
                // TODO(developer) - handle error appropriately
                if (e is AggregateException)
                {
                    Console.WriteLine("Credential Not found");
                }
                else
                {
                    throw;
                }
            }
            return null;
        }
    }
}

การเรียกไปยังเมธอด create เป็น แบบ Idempotent

หากสร้างไดรฟ์ที่แชร์สำเร็จในคำขอก่อนหน้าหรือเนื่องจากการลองอีกครั้ง เมธอดจะแสดงผลอินสแตนซ์ของทรัพยากร drives บางครั้ง เช่น หลังจากผ่านไประยะเวลานานหรือหากเนื้อหาของคำขอมีการเปลี่ยนแปลง ระบบอาจแสดงข้อผิดพลาด 409 ซึ่งบ่งชี้ว่าต้องทิ้ง requestId

รับไดรฟ์ที่แชร์

หากต้องการรับข้อมูลเมตาของไดรฟ์ที่แชร์ ให้ใช้เมธอด get ในทรัพยากร drives พร้อมพารามิเตอร์เส้นทาง driveId หากไม่ทราบรหัสไดรฟ์ คุณสามารถ แสดงไดรฟ์ที่แชร์ทั้งหมดโดยใช้เมธอด list

เมธอด get จะแสดงผลไดรฟ์ที่แชร์เป็นอินสแตนซ์ของทรัพยากร drives

หากต้องการส่งคำขอในฐานะผู้ดูแลระบบโดเมน ให้ตั้งค่าพารามิเตอร์การค้นหา useDomainAdminAccess เป็น true ดูข้อมูลเพิ่มเติมได้ที่ จัดการไดรฟ์ที่แชร์ในฐานะ ผู้ดูแลระบบโดเมน

แสดงไดรฟ์ที่แชร์

หากต้องการแสดงไดรฟ์ที่แชร์ของผู้ใช้ ให้ใช้ list เมธอดใน drives ทรัพยากร เมธอดจะแสดงผลรายการไดรฟ์ที่แชร์

ส่งพารามิเตอร์การค้นหาต่อไปนี้เพื่อปรับแต่งการแบ่งหน้าหรือกรองไดรฟ์ที่แชร์

  • pageSize: จำนวนไดรฟ์ที่แชร์สูงสุดที่จะแสดงผลต่อหน้า

  • pageToken: โทเค็นหน้าซึ่งได้รับจากการเรียกรายการก่อนหน้า ระบุโทเค็นนี้เพื่อดึงข้อมูลหน้าถัดไป

  • q: สตริงการค้นหาสำหรับค้นหาไดรฟ์ที่แชร์ ดูข้อมูลเพิ่มเติมได้ที่ ค้นหาไดรฟ์ที่แชร์

  • useDomainAdminAccess: ตั้งค่าเป็น true เพื่อส่งคำขอในฐานะผู้ดูแลระบบโดเมนเพื่อแสดงผลไดรฟ์ที่แชร์ทั้งหมดของโดเมนที่ผู้ส่งคำขอเป็นผู้ดูแลระบบ ดูข้อมูลเพิ่มเติมได้ที่ จัดการไดรฟ์ที่แชร์ในฐานะผู้ดูแลระบบโดเมน

อัปเดตไดรฟ์ที่แชร์

หากต้องการอัปเดตข้อมูลเมตาของไดรฟ์ที่แชร์ ให้ใช้เมธอด update ในทรัพยากร drives พร้อมพารามิเตอร์เส้นทาง driveId

เมธอดจะแสดงผลไดรฟ์ที่แชร์เป็นอินสแตนซ์ของทรัพยากร drives

หากต้องการส่งคำขอในฐานะผู้ดูแลระบบโดเมน ให้ตั้งค่าพารามิเตอร์การค้นหา useDomainAdminAccess เป็น true ดูข้อมูลเพิ่มเติมได้ที่ จัดการไดรฟ์ที่แชร์ในฐานะ ผู้ดูแลระบบโดเมน

ซ่อนและเลิกซ่อนไดรฟ์ที่แชร์

หากต้องการซ่อนไดรฟ์ที่แชร์จากมุมมองเริ่มต้น ให้ใช้เมธอด hide ในทรัพยากร drives พร้อมพารามิเตอร์ driveId

เมื่อซ่อนไดรฟ์ที่แชร์ ไดรฟ์จะทำเครื่องหมายทรัพยากรไดรฟ์ที่แชร์เป็น hidden=true ไดรฟ์ที่แชร์ที่ซ่อนไว้จะไม่ปรากฏใน UI ของไดรฟ์หรือในรายการไฟล์ที่แสดงผล

หากต้องการกู้คืนไดรฟ์ที่แชร์ไปยังมุมมองเริ่มต้น ให้ใช้เมธอด unhide ในทรัพยากร drives พร้อมพารามิเตอร์ driveId

ทั้ง 2 เมธอดจะแสดงผลไดรฟ์ที่แชร์เป็นอินสแตนซ์ของทรัพยากร drives

ลบไดรฟ์ที่แชร์

หากต้องการลบไดรฟ์ที่แชร์อย่างถาวร ให้ใช้เมธอด delete ในทรัพยากร drives พร้อมพารามิเตอร์ driveId

ก่อนที่จะลบไดรฟ์ที่แชร์ คุณต้องย้ายเนื้อหาทั้งหมดในไดรฟ์ที่แชร์ไปยังถังขยะหรือลบเนื้อหา นอกจากนี้ ผู้ใช้ต้องมี role=organizer ในโฟลเดอร์ไดรฟ์ที่แชร์ด้วย ดูข้อมูลเพิ่มเติมได้ที่ ย้ายไฟล์และโฟลเดอร์ไปที่ถังขยะหรือลบไฟล์และโฟลเดอร์

ส่งพารามิเตอร์การค้นหาต่อไปนี้

  • useDomainAdminAccess: ตั้งค่าเป็น true เพื่อส่งคำขอในฐานะผู้ดูแลระบบโดเมน ดูข้อมูลเพิ่มเติมได้ที่ จัดการไดรฟ์ที่แชร์ในฐานะ ผู้ดูแลระบบโดเมน

  • allowItemDeletion: ตั้งค่าเป็น true เพื่อลบรายการภายในไดรฟ์ที่แชร์ รองรับเฉพาะเมื่อตั้งค่า useDomainAdminAccess เป็น true ด้วย

เพิ่มหรือนำสมาชิกไดรฟ์ที่แชร์ออก

เพิ่มหรือนำสมาชิกไดรฟ์ที่แชร์ออกโดยใช้ทรัพยากร permissions

หากต้องการเพิ่มสมาชิก ให้สร้างสิทธิ์ในไดรฟ์ที่แชร์ นอกจากนี้ คุณยังใช้เมธอดสิทธิ์กับไฟล์แต่ละไฟล์ภายในไดรฟ์ที่แชร์เพื่อมอบสิทธิพิเศษเพิ่มเติมให้สมาชิกหรืออนุญาตให้ผู้ที่ไม่ใช่สมาชิกทำงานร่วมกันในรายการที่เฉพาะเจาะจงได้ด้วย

ดูข้อมูลเพิ่มเติมและโค้ดตัวอย่างได้ที่ แชร์ไฟล์ โฟลเดอร์ และไดรฟ์

จัดการไดรฟ์ที่แชร์ในฐานะผู้ดูแลระบบโดเมน

ใช้พารามิเตอร์ useDomainAdminAccess กับทรัพยากร drives และ permissions เพื่อจัดการไดรฟ์ที่แชร์ ทั่วทั้งองค์กร

ผู้ใช้ที่เรียกเมธอดเหล่านี้ด้วย useDomainAdminAccess=true ต้องมีสิทธิ์ของผู้ดูแลระบบ Drive and Docs ผู้ดูแลระบบสามารถค้นหาไดรฟ์ที่แชร์หรืออัปเดตสิทธิ์สำหรับไดรฟ์ที่แชร์ ซึ่งองค์กรเป็นเจ้าของได้ ไม่ว่าผู้ดูแลระบบจะเป็นสมาชิก ของไดรฟ์ที่แชร์นั้นหรือไม่ก็ตาม

เมื่อใช้บัญชีบริการ คุณอาจต้องแอบอ้างเป็นผู้ดูแลระบบที่ได้รับการตรวจสอบสิทธิ์ โดยใช้ การแอบอ้างเป็นบัญชีบริการ โปรดทราบว่าบัญชีบริการ ไม่ได้ อยู่ในโดเมน Google Workspace ของคุณ ซึ่งแตกต่างจากบัญชีผู้ใช้ หากคุณแชร์ชิ้นงาน Google Workspace เช่น เอกสารหรือกิจกรรม กับโดเมน Google Workspace ทั้งหมด ระบบจะไม่แชร์ชิ้นงานเหล่านั้นกับบัญชีบริการ ดูข้อมูลเพิ่มเติมได้ที่ภาพรวม ของบัญชีบริการ

กู้คืนไดรฟ์ที่แชร์ที่ไม่มีผู้จัดการ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีกู้คืนไดรฟ์ที่แชร์ที่ไม่มีผู้จัดการอีกต่อไป

Java

drive/snippets/drive_v3/src/main/java/RecoverDrive.java
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.Drive;
import com.google.api.services.drive.model.DriveList;
import com.google.api.services.drive.model.Permission;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/* class to demonstrate use-case of Drive's shared drive without an organizer. */
public class RecoverDrive {

  /**
   * Find all shared drives without an organizer and add one.
   *
   * @param realUser User's email id.
   * @return All shared drives without an organizer.
   * @throws IOException if shared drive not found.
   */
  public static List<Drive> recoverDrives(String realUser)
      throws IOException {
        /*Load pre-authorized user credentials from the environment.
        TODO(developer) - See https://developers.google.com/identity for
        guides on implementing OAuth2 for your application.*/
    GoogleCredentials credentials =
        GoogleCredentials.getApplicationDefault().createScoped(Arrays.asList(DriveScopes.DRIVE));
    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
        credentials);

    // Build a new authorized API client service.
    com.google.api.services.drive.Drive service =
        new com.google.api.services.drive.Drive.Builder(new NetHttpTransport(),
            GsonFactory.getDefaultInstance(),
            requestInitializer)
            .setApplicationName("Drive samples")
            .build();
    List<Drive> drives = new ArrayList<Drive>();

    // Find all shared drives without an organizer and add one.
    // Note: This example does not capture all cases. Shared drives
    // that have an empty group as the sole organizer, or an
    // organizer outside the organization are not captured. A
    // more exhaustive approach would evaluate each shared drive
    // and the associated permissions and groups to ensure an active
    // organizer is assigned.
    String pageToken = null;
    Permission newOrganizerPermission = new Permission()
        .setType("user")
        .setRole("organizer");

    newOrganizerPermission.setEmailAddress(realUser);


    do {
      DriveList result = service.drives().list()
          .setQ("organizerCount = 0")
          .setFields("nextPageToken, drives(id, name)")
          .setUseDomainAdminAccess(true)
          .setPageToken(pageToken)
          .execute();
      for (Drive drive : result.getDrives()) {
        System.out.printf("Found drive without organizer: %s (%s)\n",
            drive.getName(), drive.getId());
        // Note: For improved efficiency, consider batching
        // permission insert requests
        Permission permissionResult = service.permissions()
            .create(drive.getId(), newOrganizerPermission)
            .setUseDomainAdminAccess(true)
            .setSupportsAllDrives(true)
            .setFields("id")
            .execute();
        System.out.printf("Added organizer permission: %s\n",
            permissionResult.getId());

      }

      drives.addAll(result.getDrives());

      pageToken = result.getNextPageToken();
    } while (pageToken != null);

    return drives;
  }
}

Python

drive/snippets/drive-v3/drive_snippet/recover_drives.py
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def recover_drives(real_user):
  """Find all shared drives without an organizer and add one.
  Args:
      real_user:User ID for the new organizer.
  Returns:
      drives object

  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """
  creds, _ = google.auth.default()
  try:
    # create drive api client
    service = build("drive", "v3", credentials=creds)

    drives = []

    # pylint: disable=maybe-no-member
    page_token = None
    new_organizer_permission = {
        "type": "user",
        "role": "organizer",
        "emailAddress": "user@example.com",
    }
    new_organizer_permission["emailAddress"] = real_user

    while True:
      response = (
          service.drives()
          .list(
              q="organizerCount = 0",
              fields="nextPageToken, drives(id, name)",
              useDomainAdminAccess=True,
              pageToken=page_token,
          )
          .execute()
      )
      for drive in response.get("drives", []):
        print(
            "Found shared drive without organizer: "
            f"{drive.get('title')}, {drive.get('id')}"
        )
        permission = (
            service.permissions()
            .create(
                fileId=drive.get("id"),
                body=new_organizer_permission,
                useDomainAdminAccess=True,
                supportsAllDrives=True,
                fields="id",
            )
            .execute()
        )
        print(f'Added organizer permission: {permission.get("id")}')

      drives.extend(response.get("drives", []))
      page_token = response.get("nextPageToken", None)
      if page_token is None:
        break

  except HttpError as error:
    print(f"An error occurred: {error}")

  return drives


if __name__ == "__main__":
  recover_drives(real_user="gduser1@workspacesamples.dev")

Node.js

drive/snippets/drive_v3/drive_snippets/recover_drives.js
import {GoogleAuth} from 'google-auth-library';
import {google} from 'googleapis';

/**
 * Finds all shared drives without an organizer and adds one.
 * @param {string} userEmail The email of the user to assign ownership to.
 * @return {Promise<object[]>} A list of the recovered drives.
 */
async function recoverDrives(userEmail) {
  // Authenticate with Google and get an authorized client.
  // TODO (developer): Use an appropriate auth mechanism for your app.
  const auth = new GoogleAuth({
    scopes: 'https://www.googleapis.com/auth/drive',
  });

  // Create a new Drive API client (v3).
  const service = google.drive({version: 'v3', auth});

  // The permission to add to the shared drive.
  const newOrganizerPermission = {
    type: 'user',
    role: 'organizer',
    emailAddress: userEmail, // e.g., 'user@example.com'
  };

  // List all shared drives with no organizers.
  const result = await service.drives.list({
    q: 'organizerCount = 0',
    fields: 'nextPageToken, drives(id, name)',
    useDomainAdminAccess: true,
  });

  // Add the new organizer to each found shared drive.
  for (const drive of result.data.drives ?? []) {
    if (!drive.id) {
      continue;
    }

    console.log('Found shared drive without organizer:', drive.name, drive.id);
    await service.permissions.create({
      requestBody: newOrganizerPermission,
      fileId: drive.id,
      useDomainAdminAccess: true,
      supportsAllDrives: true,
      fields: 'id',
    });
  }
  return result.data.drives ?? [];
}

PHP

drive/snippets/drive_v3/src/DriveRecoverDrives.php
<?php
use Google\Client;
use Google\Service\Drive;
use Ramsey\Uuid\Uuid;
function recoverDrives()
{
   try {
    $client = new Client();
    $client->useApplicationDefaultCredentials();
    $client->addScope(Drive::DRIVE);
    $driveService = new Drive($client);

    $realUser = readline("Enter user email address: ");

    $drives = array();
    // Find all shared drives without an organizer and add one.
    // Note: This example does not capture all cases. Shared drives
    // that have an empty group as the sole organizer, or an
    // organizer outside the organization are not captured. A
    // more exhaustive approach would evaluate each shared drive
    // and the associated permissions and groups to ensure an active
    // organizer is assigned.
    $pageToken = null;
    $newOrganizerPermission = new Drive\Permission(array(
        'type' => 'user',
        'role' => 'organizer',
        'emailAddress' => 'user@example.com'
    ));
    $newOrganizerPermission['emailAddress'] = $realUser;
    do {
        $response = $driveService->drives->listDrives(array(
            'q' => 'organizerCount = 0',
            'fields' => 'nextPageToken, drives(id, name)',
            'useDomainAdminAccess' => true,
            'pageToken' => $pageToken
        ));
        foreach ($response->drives as $drive) {
            printf("Found shared drive without organizer: %s (%s)\n",
                $drive->name, $drive->id);
            $permission = $driveService->permissions->create($drive->id,
                $newOrganizerPermission,
                array(
                    'fields' => 'id',
                    'useDomainAdminAccess' => true,
                    'supportsAllDrives' => true
                ));
            printf("Added organizer permission: %s\n", $permission->id);
        }
        array_push($drives, $response->drives);
        $pageToken = $response->pageToken;
    } while ($pageToken != null);
    return $drives;
   } catch(Exception $e) {
      echo "Error Message: ".$e;
   }
}

.NET

drive/snippets/drive_v3/DriveV3Snippets/RecoverDrives.cs
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Drive.v3.Data;
using Google.Apis.Services;

namespace DriveV3Snippets
{
    // Class to demonstrate use-case of Drive's shared drive without an organizer.
    public class RecoverDrives
    {
        /// <summary>
        /// Find all shared drives without an organizer and add one.
        /// </summary>
        /// <param name="realUser">User ID for the new organizer.</param>
        /// <returns>all shared drives without an organizer.</returns>
        public static IList<Drive> DriveRecoverDrives(string realUser)
        {
            try
            {
                /* Load pre-authorized user credentials from the environment.
                 TODO(developer) - See https://developers.google.com/identity for
                 guides on implementing OAuth2 for your application. */
                GoogleCredential credential = GoogleCredential.GetApplicationDefault()
                    .CreateScoped(DriveService.Scope.Drive);

                // Create Drive API service.
                var service = new DriveService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Drive API Snippets"
                });

                var drives = new List<Drive>();
                // Find all shared drives without an organizer and add one.
                // Note: This example does not capture all cases. Shared drives
                // that have an empty group as the sole organizer, or an
                // organizer outside the organization are not captured. A
                // more exhaustive approach would evaluate each shared drive
                // and the associated permissions and groups to ensure an active
                // organizer is assigned.
                string pageToken = null;
                var newOrganizerPermission = new Permission()
                {
                    Type = "user",
                    Role = "organizer",
                    EmailAddress = realUser
                };

                do
                {
                    var request = service.Drives.List();
                    request.UseDomainAdminAccess = true;
                    request.Q = "organizerCount = 0";
                    request.Fields = "nextPageToken, drives(id, name)";
                    request.PageToken = pageToken;
                    var result = request.Execute();
                    foreach (var drive in result.Drives)
                    {
                        Console.WriteLine(("Found abandoned shared drive: {0} ({1})",
                            drive.Name, drive.Id));
                        // Note: For improved efficiency, consider batching
                        // permission insert requests
                        var permissionRequest = service.Permissions.Create(
                            newOrganizerPermission,
                            drive.Id
                        );
                        permissionRequest.UseDomainAdminAccess = true;
                        permissionRequest.SupportsAllDrives = true;
                        permissionRequest.Fields = "id";
                        var permissionResult = permissionRequest.Execute();
                        Console.WriteLine("Added organizer permission: {0}", permissionResult.Id);
                    }

                    pageToken = result.NextPageToken;
                } while (pageToken != null);

                return drives;
            }
            catch (Exception e)
            {
                // TODO(developer) - handle error appropriately
                if (e is AggregateException)
                {
                    Console.WriteLine("Credential Not found");
                }
                else
                {
                    throw;
                }
            }
            return null;
        }
    }
}

ป้องกันไม่ให้ผู้ใช้ดาวน์โหลด พิมพ์ หรือคัดลอกไฟล์

คุณสามารถจำกัดวิธีที่ผู้ใช้ดาวน์โหลด พิมพ์ และคัดลอกไฟล์ภายในไดรฟ์ที่แชร์ได้

หากต้องการตรวจสอบว่าผู้ใช้สามารถเปลี่ยนข้อจำกัดการดาวน์โหลดที่ผู้จัดการใช้กับไดรฟ์ที่แชร์ได้หรือไม่ ให้ตรวจสอบช่องบูลีน capabilities.canChangeDownloadRestriction หากตั้งค่า capabilities.canChangeDownloadRestriction เป็น true คุณจะใช้ข้อจำกัดการดาวน์โหลดกับไดรฟ์ที่แชร์ได้ ดูข้อมูลเพิ่มเติมได้ที่ ทำความเข้าใจความสามารถของไฟล์

ทรัพยากร drives มีคอลเล็กชันของ ช่องบูลีน restrictions ที่ใช้เพื่อระบุว่าสามารถดำเนินการกับไดรฟ์ที่แชร์ได้หรือไม่ ข้อจำกัดจะใช้กับไดรฟ์ที่แชร์หรือรายการภายในไดรฟ์ที่แชร์ คุณตั้งค่าข้อจำกัดได้โดยใช้เมธอด drives.update

หากต้องการใช้ข้อจำกัดการดาวน์โหลดกับไดรฟ์ที่แชร์ ผู้จัดการไดรฟ์ที่แชร์สามารถตั้งค่า ช่อง restrictions.downloadRestriction ของทรัพยากร drives โดยใช้ออบเจ็กต์ DownloadRestriction การตั้งค่าช่องบูลีน restrictedForReaders เป็น true จะประกาศว่าทั้งการดาวน์โหลดและการคัดลอกถูกจำกัดสำหรับผู้มีสิทธิ์อ่าน การตั้งค่าช่องบูลีน restrictedForWriters เป็น true จะประกาศว่าทั้งการดาวน์โหลดและการคัดลอกถูกจำกัดสำหรับผู้มีสิทธิ์เขียน โปรดทราบว่าหากช่อง restrictedForWriters เป็น true การดาวน์โหลดและการคัดลอกจะถูกจำกัดสำหรับผู้มีสิทธิ์อ่านด้วย ในทำนองเดียวกัน การตั้งค่า restrictedForWriters เป็น true และ restrictedForReaders เป็น false จะเทียบเท่ากับการตั้งค่าทั้ง restrictedForWriters และ restrictedForReaders เป็น true

ความเข้ากันได้แบบย้อนหลัง

เมื่อมีการเปิดตัวออบเจ็กต์ DownloadRestriction ฟังก์ชันการทำงานของ restrictions.copyRequiresWriterPermission ช่องบูลีน ได้รับการอัปเดตแล้ว

ตอนนี้การตั้งค่า restrictions.copyRequiresWriterPermission เป็น true จะอัปเดตช่องบูลีน restrictedForReaders ของออบเจ็กต์ DownloadRestriction เป็น true เพื่อประกาศว่า ทั้งการดาวน์โหลดและการคัดลอกถูกจำกัดสำหรับผู้มีสิทธิ์อ่าน

การตั้งค่าช่อง copyRequiresWriterPermission เป็น false จะอัปเดตทั้งช่อง restrictedForWriters และ restrictedForReaders เป็น false ซึ่งหมายความว่าระบบจะนำการตั้งค่าข้อจำกัดการดาวน์โหลดหรือการคัดลอกออกสำหรับผู้ใช้ทุกคน

ช่องที่ควบคุมฟีเจอร์การดาวน์โหลด การพิมพ์ และการคัดลอก

ตารางต่อไปนี้แสดงช่องทรัพยากร drives ที่ส่งผลต่อฟังก์ชันการทำงานของการดาวน์โหลด การพิมพ์ และการคัดลอก

ช่อง คำอธิบาย เวอร์ชัน
capabilities.canCopy ระบุว่าผู้ใช้ปัจจุบันคัดลอกไฟล์ในไดรฟ์ที่แชร์ได้หรือไม่ v2 และ v3
capabilities.canDownload ระบุว่าผู้ใช้ปัจจุบันดาวน์โหลดไฟล์ในไดรฟ์ที่แชร์ได้หรือไม่ v2 และ v3
capabilities.canChangeCopyRequiresWriterPermissionRestriction ระบุว่าผู้ใช้ปัจจุบันเปลี่ยนข้อจำกัด copyRequiresWriterPermission ของไดรฟ์ที่แชร์ได้หรือไม่ v2 และ v3
capabilities.canResetDriveRestrictions ระบุว่าผู้ใช้ปัจจุบันรีเซ็ตข้อจำกัดของไดรฟ์ที่แชร์เป็นค่าเริ่มต้นได้หรือไม่ v2 และ v3
capabilities.canChangeDownloadRestriction ระบุว่าผู้ใช้ปัจจุบันเปลี่ยนข้อจำกัดการดาวน์โหลดของไดรฟ์ที่แชร์ได้หรือไม่ v3 เท่านั้น
restrictions.copyRequiresWriterPermission ระบุว่าตัวเลือกในการคัดลอก พิมพ์ หรือดาวน์โหลดไฟล์ภายในไดรฟ์ที่แชร์ปิดอยู่สำหรับผู้มีสิทธิ์อ่านและผู้แสดงความคิดเห็นหรือไม่ เมื่อเป็น true ระบบจะตั้งค่าช่องที่มีชื่อคล้ายกันเป็น true สำหรับไฟล์ใดก็ตามภายในไดรฟ์ที่แชร์นี้ v2 และ v3
restrictions.downloadRestriction ข้อจำกัดการดาวน์โหลดที่ผู้จัดการไดรฟ์ที่แชร์ใช้ v3 เท่านั้น

ขีดจำกัดของโฟลเดอร์

ไดรฟ์ที่แชร์มีขีดจำกัดของรายการและความลึกของโฟลเดอร์ นอกจากนี้ ระบบจะจัดการการใช้พื้นที่เก็บข้อมูลเทียบกับพื้นที่เก็บข้อมูลรวมขององค์กร ไม่ใช่โควต้าพื้นที่เก็บข้อมูล Google ไดรฟ์ของฉันส่วนบุคคลของผู้ใช้แต่ละราย ดูข้อมูลเพิ่มเติมได้ที่ ดู ขีดจำกัดของไดรฟ์ที่แชร์ใน Google ไดรฟ์

จำนวนรายการสูงสุด

ไดรฟ์ที่แชร์ 1 ไดรฟ์มีรายการได้สูงสุด 500,000 รายการ ซึ่งรวมถึงไฟล์ โฟลเดอร์ และทางลัด

เมื่อถึงขีดจำกัดแล้ว ไดรฟ์ที่แชร์จะไม่รับรายการอีกต่อไป หากต้องการรับไฟล์ต่อ ผู้ใช้ต้องลบรายการออกจากไดรฟ์ที่แชร์อย่างถาวร โปรดทราบว่ารายการในถังขยะจะนับรวมในขีดจำกัด แต่รายการที่ลบอย่างถาวรจะไม่นับรวม ดูข้อมูลเพิ่มเติมได้ที่ ย้ายไฟล์และโฟลเดอร์ไปที่ถังขยะหรือลบ ไฟล์และโฟลเดอร์

ขีดจำกัดความลึกของโฟลเดอร์

โฟลเดอร์ในไดรฟ์ที่แชร์จะมีโฟลเดอร์ซ้อนกันได้ไม่เกิน 100 ระดับ ซึ่งหมายความว่าคุณจะจัดเก็บโฟลเดอร์ย่อยไว้ในโฟลเดอร์ที่มีความลึกมากกว่า 99 ระดับไม่ได้ ข้อจำกัดนี้ใช้กับโฟลเดอร์ย่อยเท่านั้น

การพยายามเพิ่มโฟลเดอร์มากกว่า 100 ระดับจะแสดงการตอบกลับรหัสสถานะ teamDriveHierarchyTooDeep HTTP