I’m having trouble with the Google Drive API for iOS. I can update file data using the ‘media’ uploadType and both data and metadata with ‘multipart’. But I’m stuck when it comes to ‘resumable’ uploads.
The API docs say file updates should use PUT. But for resumable uploads, you first send metadata with POST, then file data with PUT. This is really confusing me!
Here’s what I’ve tried:
// First request (metadata)
let metadataRequest = URLRequest(url: uploadURL)
metadataRequest.httpMethod = "POST"
metadataRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
metadataRequest.httpBody = JSONEncoder().encode(fileMetadata)
// Second request (file data)
let dataRequest = URLRequest(url: resumableUploadURL)
dataRequest.httpMethod = "PUT"
dataRequest.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
dataRequest.httpBody = fileData
But this doesn’t work. Any ideas on how to properly implement resumable uploads for file updates? I’m not using the Objective-C client library, by the way. Thanks for any help!
hey mate, i feel ur pain with the drive API. it’s a real headache sometimes. have u tried using the resumable upload session ID? u get it from the initial POST response. then u can use that for subsequent PUT requests to upload ur file chunks. might solve ur issue. good luck!
I’ve dealt with this exact issue before, and it can be quite tricky. The key is understanding that for resumable uploads, you’re essentially creating a new upload session each time, even for updates.
Here’s what worked for me:
Initiate the session with a POST request to the /upload endpoint, including your file metadata and the ‘X-Upload-Content-Type’ header.
From the response, grab the ‘Location’ header. This is your resumable upload URL.
Send your file data to this URL using PUT requests. For large files, you can split this into chunks.
If you need to pause and resume, use the ‘Content-Range’ header in subsequent PUT requests.
Remember, error handling is crucial. The API can be finicky, so implement robust retry logic.
Also, consider using a library like Alamofire. It can simplify a lot of the networking complexities, especially for resumable uploads.
Hope this helps! Let me know if you need more specifics.
Having worked extensively with the Google Drive API, I can confirm that resumable uploads for file updates can be challenging. The key is to treat it as a two-step process.
First, initiate the upload session with a POST request to the /upload endpoint. Include your file metadata and set the ‘X-Upload-Content-Type’ header. The response will contain a ‘Location’ header - this is your resumable upload URL.
Next, send your file data to this URL using PUT requests. For large files, you can split this into chunks and use the ‘Content-Range’ header to specify which part you’re uploading.
One common pitfall is not handling errors properly. The API can be temperamental, so implement robust retry logic. Also, ensure you’re correctly setting the ‘Authorization’ header for all requests.
If you’re still struggling, consider using a third-party library that handles resumable uploads. While it adds a dependency, it can significantly simplify the process and save you time in the long run.