I am getting an error when trying to create a file in google drive using the salesforce contentversion data url
for (index, row), (index2, row2) in zip(df2.iterrows(),df3.iterrows()):
acid = row['AccountId']
acname = adict[acid]
fdname = acname +' - '+ acid
folder_metadata = {
'name': fdname,
'mimeType':'application/vnd.google-apps.folder',
'parents': ['1-5zP-GutalHIJvS8xpBpe0QuYUqOeAV7']
}
fres = service.files().create(body=folder_metadata).execute()
folderId = fres['id']
fname = row2['Title']
data = row2['VersionData']
url = data.encode()
fileurl = url.decode('ascii')
finurl = 'https://'+ instance + fileurl
print(finurl)
#sample url returned is - https://xxx.salesforce.com/services/data/v57.0/sobjects/ContentVersion/0689Q0000026HeXQAU/VersionData
media = requests.get(finurl, headers={'Authorization': f'Bearer {sf.session_id}'})
fbody = bytes.decode(media.content)
file_metadata = {
'name': fname,
'parents': folderId
}
service.files().create(
body=file_metadata,
media_body=fbody
).execute()
But I am getting an error
fbody = bytes.decode(media.content)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 11: invalid start byte
Any pointers? Thanks!
UPDATE - SOLUTION FOUND After several failures, this finally worked
media = requests.get(finurl, headers={'Authorization': f'Bearer {sf.session_id}'})
fbody = MediaIoBaseUpload(BytesIO(media.content), mimetype='application/octet-stream', resumable=True)
file_metadata = {
'name': fname,
'parents': [folderId]
}
service.files().create(
body=file_metadata,
media_body=fbody
).execute()
bytes
and why are you decoding it