I want to upload the files to shared drive using java. I have created a service account and I have shared the shared-drive (name is DB-Drive) with the service account's address.
Global variables
private static HttpTransport httpTransport;
private static Drive drive;
Main function (initialize and listing of the files)
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Credential credential = authorize();
drive = new Drive.Builder(httpTransport, JacksonFactory.getDefaultInstance(), credential).setApplicationName(
"Quickstart").build();
uploadFile();
FileList result = drive.files().list().execute();
List<File> files = result.getFiles();
if (result == null || result.isEmpty()) {
System.out.println("No files found.");
} else {
System.out.println("Files:");
for (File file : files) {
System.out.printf("%s %s \n", file.getName(),file.getId());
}
}
}
Authorization code
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("resources/credentials.json"))
.createScoped(Collections.singleton(DriveScopes.DRIVE_FILE));
return credential;
Uploading code
String folderId = "***DB-Drive id****";
File fileMetadata = new File();
fileMetadata.setName("photo.jpg");
fileMetadata.setParents(Collections.singletonList(folderId));
java.io.File filePath = new java.io.File("/home/*****/Downloads/qck_1552369371.jpeg");
FileContent mediaContent = new FileContent("image/jpeg", filePath);
File file = drive.files().create(fileMetadata, mediaContent)
.setFields("id, parents")
.execute();
System.out.println("File ID: " + file.getId());
return file;
I am setting DB-Drive which is the teamdrive or the shareddrive as the parent of the uploaded file but still I am not able to view this in the shared drive folder.. Please help me with this.
I am adding the complete flow :-
1.) I create the service account using the steps mentioned at https://developers.google.com/identity/protocols/oauth2/service-account I selected the project and gave the description and everything.
2.) I did'nt gave domain level delegation to service account rather I shared the DB-drive(team drive) with service account address (which was mentioned in credentials.json file as client_email *****.gserviceaccount.com)
3.) Then I used the above code to upload the file asking service account to upload the file to shared drive i.e DB-Drive by specifying the DB-Drive's id as the parent id of the file.
4.) but still the file is getting uploaded to service account's drive it is not getting linked to shared drive.
5.) DB-Drive is the parent folder (the only folder).
Any leads would be a great help.