I am attempting to use the open ai image API and send images. Unfortunately, they don’t let you send image data directly, instead, you have to save and share the image to the cloud, and send their API a link.
I am trying to save the image via icloud, and make it public.
Here are the Dashboard configuration steps I’ve taken
Here is my code:
import CloudKit
import Foundation
extension Data {
func uploadToCloudKit(completion: @escaping (Result<URL, Error>) -> Void) {
do {
let containerIdentifier = try String.iCloudContainerIdentifier()
let container = CKContainer(identifier: containerIdentifier)
let publicDatabase = container.publicCloudDatabase
// Create a record
let record = CKRecord(recordType: "Image")
// Save the image data to a temporary file
let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
try self.write(to: tempURL)
// Create a CKAsset with the file URL
let imageAsset = CKAsset(fileURL: tempURL)
record["image"] = imageAsset
// Save the record to CloudKit
publicDatabase.save(record) { savedRecord, error in
// Delete the temporary file
try? FileManager.default.removeItem(at: tempURL)
if let error = error {
completion(.failure(GenericError("Error uploading image: (error.localizedDescription)")))
return
}
guard let savedRecord = savedRecord else {
completion(.failure(GenericError("Failed to save record without error")))
return
}
let share = CKShare(rootRecord: record)
let container = CKContainer.default()
let modifyRecordsOperation = CKModifyRecordsOperation(recordsToSave: [record, share])
modifyRecordsOperation.modifyRecordsCompletionBlock = { savedRecords, deletedIds, error in
DispatchQueue.main.async {
if let error = error as? CKError {
let genericError: GenericError
switch error.code {
case .networkUnavailable, .networkFailure:
genericError = GenericError("Network issues: (error.localizedDescription)")
case .quotaExceeded:
genericError = GenericError("Quota exceeded: (error.localizedDescription)")
case .partialFailure:
if let partialErrors = error.partialErrorsByItemID {
let partialErrorMessages = partialErrors.map { "Partial error for recordID ($0.key): ($0.value.localizedDescription)" }
genericError = GenericError(partialErrorMessages.joined(separator: ", "))
} else {
genericError = GenericError("Partial failure without specific errors: (error.localizedDescription)")
}
case .serverRecordChanged:
genericError = GenericError("Server record changed: (error.localizedDescription)")
case .notAuthenticated:
genericError = GenericError("Not authenticated: (error.localizedDescription)")
case .permissionFailure:
genericError = GenericError("Permission failure: (error.localizedDescription)")
default:
genericError = GenericError("Other error: (error.localizedDescription)")
}
completion(.failure(genericError))
} else if let shareURL = share.url {
completion(.success(shareURL))
} else {
completion(.failure(GenericError("Failed to save record without error")))
}
}
}
publicDatabase.add(modifyRecordsOperation)
}
} catch {
completion(.failure(error))
}
}
}
When I po the error I get:
(lldb) po genericError
▿ GenericError
- message : "Partial error for recordID <CKRecordID: 0x6000010dcf20; recordName=2D6350E1-F1BC-4CE9-89CE-35A1E52D259C, zoneID=_defaultZone:__defaultOwner__>: Record <CKRecordID: 0x1239aa420; recordName=2D6350E1-F1BC-4CE9-89CE-35A1E52D259C, zoneID=_defaultZone:__defaultOwner__> will not be saved because of previous error in atomic zone, Partial error for recordID <CKRecordID: 0x6000010dcfe0; recordName=Share-E804AED9-AC79-445A-96B4-27EB2647A1AD, zoneID=_defaultZone:__defaultOwner__>: Error adding private sharing PCS to public sharing PCS for share <CKRecordID: 0x1238956b0; recordName=Share-E804AED9-AC79-445A-96B4-27EB2647A1AD, zoneID=_defaultZone:__defaultOwner__>: <CKError 0x122721e30: "Internal Error" (5005); "You can't add a nil share PCS to a record PCS">"
I am not sure what else needs to be done, but I keep getting this error.