0

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()
6
  • 1
    What is bytes and why are you decoding it
    – identigral
    Commented Feb 22 at 17:27
  • Is the content of this file UTF-8 text data? Even if yes, why do you need to decode it if you're just going to send it to another storage service?
    – David Reed
    Commented Feb 22 at 18:26
  • I was following an example so am not even sure. From your previous comment here you said the link has binary content but I dont know how to pass it to drive salesforce.stackexchange.com/questions/300845/… I am actually downloading pdf files.
    – Pauline
    Commented Feb 23 at 6:04
  • When i pass media.content directly to the create method without decode, i get TypeError: media_filename must be str or MediaUpload.
    – Pauline
    Commented Feb 23 at 6:09
  • Thanks David, i found something that worked.
    – Pauline
    Commented Feb 23 at 6:33

0

You must log in to answer this question.

Browse other questions tagged .