英文:
How do I copy a file on a Google Drive using v3 of the Google Drive Java API?
问题
I'm having trouble copying a file on a Google Drive using v3 of the Google Drive API for Java.
The file in question is on a shared team drive, and I'm reading in credentials that are associated with a service account. The service account has "manager" access to the drive in question.
Here is my code that is trying to copy the file. It's in Scala, rather than Java, but hopefully it will be clear enough anyway, even if you don't know Scala. (In case you don't already know, Scala is a programming language that targets the JVM and interoperates with Java in a relatively painless manner.)
val fileCopy = new GFile()
fileCopy.setName("Test Copy")
try {
ArchiveDriveService
.files()
.copy(TemplateFileId, fileCopy)
.execute()
} catch {
case e: IOException =>
fprint("An error occured: " + e)
}
[Erratum: As pointed out in the accepted solution provided by ziganotschka below, the only thing I needed to do to get things working right was to add .setSupportsAllDrives(true)
before the .execute()
above.]
Note that GFile
is just an alias for com.google.api.services.drive.model.File
. And ArchiveDriveService
is a variable I defined to hold my Drive
service object. TemplateFileId
contains the ID of the Google Drive file that I want to copy.
This is the error that I get when I run this:
{
"code": 404,
"errors": [{
"domain": "global",
"location": "fileId",
"locationType": "parameter",
"message": "File not found: 1BjvZG6Ub7smBWrhC82aRD2LRUehB_U6AhmnXwrsuLks.",
"Reason": "notFound"
}],
"message": "File not found: 1BjvZG6Ub7smBWrhC82aRD2LRUehB_U6AhmnXwrsuLks."
}
Despite the above error, ArchiveDriveService
works fine for getting a list of files from the drive. E.g., this code works fine:
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})")
}
}
If I run the above, I get the following 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)
You can see from the above listing, that one of the files listed is the file that I'm trying to copy. I.e., it has the fileId
"1BjvZG6Ub7smBWrhC82aRD2LRUehB_U6AhmnXwrsuLks". And yet the error message I'm getting says that this file cannot be found.
This is how I'm making 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
}
Please help.
英文:
I'm having trouble copying a file on a Google Drive using v3 of the Google Drive API for Java.
The file in question is on a shared team drive, and I'm reading in credentials that are associated with a service account. The service account has "manager" access to the drive in question.
Here is my code that is trying to copy the file. It's in Scala, rather than Java, but hopefully it will be clear enough anyway, even if you don't know Scala. (In case you don't already know, Scala is a programming language that targets the JVM and interoperates with Java in a relatively painless manner.)
val fileCopy = new GFile()
fileCopy.setName("Test Copy")
try {
ArchiveDriveService
.files()
.copy(TemplateFileId, fileCopy)
.execute()
} catch {
case e: IOException =>
fprint("An error occured: " + e)
}
[Erratum: As pointed out in the accepted solution provided by ziganotschka below, the only thing I needed to do to get things working right was to add .setSupportsAllDrives(true)
before the .execute()
above.]
Note that GFile
is just an alias for com.google.api.services.drive.model.File
. And ArchiveDriveService
is a variable I defined to hold my Drive
service object. TemplateFileId
contains the ID of the Google Drive file that I want to copy.
This is the error that I get when I run this:
{
"code" : 404,
"errors" : [ {
"domain" : "global",
"location" : "fileId",
"locationType" : "parameter",
"message" : "File not found: 1BjvZG6Ub7smBWrhC82aRD2LRUehB_U6AhmnXwrsuLks.",
"Reason" : "notFound"
} ],
"message" : "File not found: 1BjvZG6Ub7smBWrhC82aRD2LRUehB_U6AhmnXwrsuLks."
}
Desipte the above error, ArchiveDriveService
works fine for getting a list of files from the drive. E.g., this code works fine:
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})")
}
}
If I run the above, I get the following 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)
You can see from the above listing, that one of the files listed is the file that I'm trying to copy. I.e., it has the fileId
"1BjvZG6Ub7smBWrhC82aRD2LRUehB_U6AhmnXwrsuLks". And yet the error message I'm getting says that this file cannot be found.
This is how I'm making 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
}
Please help.
答案1
得分: 1
当你复制位于共享驱动器上的文件时,需要指定相同的设置以进行列表操作:
.setSupportsAllDrives(true)
请查看Files: copy
的参数:
> supportsAllDrives
> 请求应用程序是否支持我的驱动器和共享驱动器。(默认值:false)
- 请注意,默认情况下,文件将复制到与原始文件位于相同的(共享)文件夹/驱动器中。
- 如果您想将其复制到其他位置,您需要指定
parents[]
参数。
英文:
When you copy a file that is located on a shared Drive, you need to specify the same like for listing it:
.setSupportsAllDrives(true)
See parameters of Files: copy
:
> supportsAllDrives
>Whether the requesting application supports
> both My Drives and shared drives. (Default: false)
- Mind that by default the file will be copied into the same (shared) folder / drive where the original file is located
- If you want to copy it elsewhere, you nedd to specify the
parents[]
parameter
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论