This plugin allows to upload a file to a Google Drive folder. Currently it does not support downloading of files and uploading to Team drives and the domain corpus, but you are welcome to show your interest in the corresponding issue or to provide a PR.
-
Go to Google API Console and create a project.
-
Go to Credentials section of just created project.
-
Choose Create credentials → OAuth client id → Other. Copy the id and the secret.
You have to provide the values to an extension with name googleDrive
inside
the build script. You may do this anyway you like: get from an environment
variable, system property if you’d like, or from a
gradle.properties
,
which is the most convenient to me.
By default, the plugin applies a task named 'uploadToDrive' which expects an
extension named googleDrive
to be defined, like so:
plugins {
id 'ru.kinca.google-drive-uploader' version '1.1.0'
}
googleDrive {
// Missing folders will be created
destinationFolderPath = 'test/upload'
// Or you can specify a folder id
destinationFolderId = '1abcedefg'
// If not specified, simple file name is used
destinationName = 'cute_picture.jpg'
file = file('c:\\Users\\User\\Pictures\\kittens.jpg')
// Default is true
updateIfExists = false
// Do not put those directly into the build file
clientId = '<YOUR CLIENT ID>'
clientSecret = '<YOUR CLIENT SECRET>'
// Location where Google Drive client's credentials will be stored. You may
// want to have a separate dir for each project.
// Default is `${user.home}/.credentials/google-drive-uploader`, i.e.
// shared between all projects.
credentialsDir = file('.gradle/google-drive-uploader/credentials/')
}
Note
|
If no permissions were set explicitly, the file will be publicly shared for reading. |
Execute uploadToDrive
task to perform the upload.
When updating an existing file, the specified permissions will be set, but existing ones would stay untouched.
import com.google.api.services.drive.model.Permission
googleDrive {
// Other properties configured...
permissions = [new Permission()
.setType('user')
.setEmailAddress('[email protected]')
.setRole('writer')]
}
Go to Shared with me tab, get the URL which will be like
https://drive.google.com/drive/folders/sharedFolderId
.
Or get a direct url to the folder https://drive.google.com/open?id=sharedFolderId
.
googleDrive {
// Other properties configured...
destinationFolderId = 'sharedFolderId'
}
Note
|
You can specify either destinationFolderPath or destinationFolderId, not both. |
rootProject.name = 'example-app'
plugins {
id 'ru.kinca.google-drive-uploader' version '1.1.0'
id 'application'
}
mainClassName = 'ru.kinca.example.App'
googleDrive {
destinationFolder = 'myapp/distributions'
file = files(distZip).singleFile
clientId = driveClientId
clientSecret = driveClientSecret
}
uploadToDrive.dependsOn distZip
The application distribution will be packed to a ZIP-file and uploaded to
myapp/distributions/example-app.zip
and shared publicly for reading.