Skip to content

Instantly share code, notes, and snippets.

@kgleong
Last active May 31, 2023 13:19
Show Gist options
  • Save kgleong/0b6e7f333a7e6dc2d3d9584d20974d9d to your computer and use it in GitHub Desktop.
Save kgleong/0b6e7f333a7e6dc2d3d9584d20974d9d to your computer and use it in GitHub Desktop.
Google Drive - Upload File
func uploadFile(
name: String,
folderID: String,
fileURL: URL,
mimeType: String,
service: GTLRDriveService) {
let file = GTLRDrive_File()
file.name = name
file.parents = [folderID]
// Optionally, GTLRUploadParameters can also be created with a Data object.
let uploadParameters = GTLRUploadParameters(fileURL: fileURL, mimeType: mimeType)
let query = GTLRDriveQuery_FilesCreate.query(withObject: file, uploadParameters: uploadParameters)
service.uploadProgressBlock = { _, totalBytesUploaded, totalBytesExpectedToUpload in
// This block is called multiple times during upload and can
// be used to update a progress indicator visible to the user.
}
service.executeQuery(query) { (_, result, error) in
guard error == nil else {
fatalError(error!.localizedDescription)
}
// Successful upload if no error is returned.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment