簡體   English   中英

如何使用 Google Drive Java API v3 復制 Google Drive 上的文件?

[英]How do I copy a file on a Google Drive using v3 of the Google Drive Java API?

我在使用 Google Drive API for Java v3 復制 Google Drive 上的文件時遇到問題。

有問題的文件位於共享團隊驅動器上,我正在讀取與服務帳戶關聯的憑據。 服務帳戶具有對相關驅動器的“管理員”訪問權限。

這是我試圖復制文件的代碼。 它在 Scala 中,而不是 Java,但希望無論如何它都會足夠清楚,即使你不知道 Scala。 (如果您還不知道,Scala 是一種針對 JVM 並以相對輕松的方式與 Java 互操作的編程語言。)

    val fileCopy = new GFile()
    fileCopy.setName("Test Copy")
    try {
      ArchiveDriveService
        .files()
        .copy(TemplateFileId, fileCopy)
        .execute()
    } catch {
      case e: IOException =>
        fprint("An error occured: " + e)
    }

[勘誤表:正如下面 ziganotschka 提供的公認解決方案中所指出的,我唯一需要做的就是在上面的.execute() ) 之前添加.setSupportsAllDrives(true) 。]

請注意, GFile只是com.google.api.services.drive.model.File的別名。 ArchiveDriveService是我定義的一個變量,用於保存我的Drive服務 object。 TemplateFileId包含我要復制的 Google Drive 文件的 ID。

這是我運行此程序時遇到的錯誤:

     {
       "code" : 404,
       "errors" : [ {
         "domain" : "global",
         "location" : "fileId",
         "locationType" : "parameter",
         "message" : "File not found: 1BjvZG6Ub7smBWrhC82aRD2LRUehB_U6AhmnXwrsuLks.",
         "Reason" : "notFound"
       } ],
       "message" : "File not found: 1BjvZG6Ub7smBWrhC82aRD2LRUehB_U6AhmnXwrsuLks."
     }

除了上述錯誤, ArchiveDriveService可以很好地從驅動器中獲取文件列表 例如,這段代碼工作正常:

    val result = ArchiveDriveService
      .files()
      .list()
      .setCorpora("drive")
      .setDriveId(driveId)
      .setFields("nextPageToken, files(id, name)")
      .setIncludeItemsFromAllDrives(true)
      .setPageSize(10)
      .setSpaces("drive")
      .setSupportsAllDrives(true)
      .execute()

    val files = result.getFiles()
    if (files == null || files.isEmpty()) {
        fprint("No files found.")
    } else {
        fprint("Files:")
        for (file <- files.asScala) {
          fprint(s"${file.getName} (${file.getId})")
        }
    }

如果我運行上面的,我得到以下 output:

     Files: 
     Copy of Template for Double Stranded (plasmid NGS, gDNA NGS) calculator (12_UODoLGbN04btC3dKh1yWV0yblGzMe0jEM-FWibVSM)
     Copy of Template for Double Stranded (plasmid NGS, gDNA NGS) calculator (1B0RUHZlakDLfH-12czZAzgABv3pGDhZXD-GIyeXgARA)
     Template for Double Stranded (plasmid NGS, gDNA NGS) calculator (1BjvZG6Ub7smBWrhC82aRD2LRUehB_U6AhmnXwrsuLks)
     Double Stranded Sheet (1MZAf4L0tPiqxW3yNG1oFigEkVjZB1G98)
     test (1zIayPNDWHNscbPdVYBICF001l-TbbZTl)
     foo (1tLlflOLg5OJ7w-YGRloWF-ShriRkrvTO4WV6-X9nl5A)
     foo.xlsx (1BF8--UOriadQSxF0kWMr9-Z0bRepcntI)
     Template for Double Stranded (plasmid NGS, gDNA NGS) calculator.xlsx (1qXAW1BCaFJDUxKd20LJB7haBmf4YCGkc)
     build.sbt (1OqG1iTc0fzcwbszc2-H-rhiq-3jNJpEw)

您可以從上面的清單中看到,列出的文件之一是我要復制的文件。 即,它的文件 ID 為“ fileId ”。 然而,我收到的錯誤消息說找不到這個文件。

這就是我制作ArchiveDriveService的方式:

  val ArchiveDriveService = newDriveService(ArchiveCredentialsFilename)

  def newDriveService(credentialsFilename: String): Drive = {
    new Drive.Builder(HttpTransport,
                      TheJsonFactory,
                      new HttpCredentialsAdapter(getCredentials(credentialsFilename)))
      .setApplicationName(ApplicationName)
      .build()
  }

  def getCredentials(credentialsFilename: String): GoogleCredentials = {
    val parent = Paths.get(System.getProperty("user.home"))
    val secrets = parent.resolve(".secrets")
    val credentialsPath = secrets.resolve(credentialsFilename)
    val in = new FileInputStream(credentialsPath.toFile)

    // This scope gives us full read/write access to the shared drive:
    val scopes = List(DriveScopes.DRIVE).asJava

    val credentials = GoogleCredentials.fromStream(in).createScoped(scopes)
    in.close()
    credentials
  }

請幫忙。

當您復制位於共享雲端硬盤上的文件時,您需要指定相同的內容來列出它:

.setSupportsAllDrives(true)

查看 Files 的參數Files: copy

支持所有驅動器
請求的應用程序是否同時支持“我的雲端硬盤”和共享雲端硬盤。 (默認:假)

  • 請注意,默認情況下,文件將被復制到原始文件所在的相同(共享)文件夾/驅動器中
  • 如果你想把它復制到別處,你需要指定parents[]參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM