Last active
May 31, 2023 13:19
-
-
Save kgleong/0b6e7f333a7e6dc2d3d9584d20974d9d to your computer and use it in GitHub Desktop.
Google Drive - Upload File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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