0

Is it possible to query or extract the metadata/document properties from attachments in bulk? Previously I have been able to get around this by using the object IDs to bulk download the files and then use python to output the metadata to a .csv however I've only found that workable for smaller batches of files (<500) with consistent file types (such as all PDFs).

I have tried accessing the Attachments salesforce object through Power BI but it doesn't contain the file metadata and I've also tried writing a python script to access the objects via API (using the simple_salesforce library) and writing to a temporary file which has also not worked out.

Ideally I'd like to avoid bulk downloading files and just working with them in Salesforce (there are >250,000 attachment at this point that I need to analyse the metadata of) however open to any suggestions. I can arrange access to Data Loader (have not yet been set up at my org), or comfortable working on Einstein, SOQL, python or Power BI if any of these will be suitable.

0

I didn't get any responses but I was able to write a python script that will achieve bulk metadata querying and bulk download of attachments so I thought I'd post in case anyone looks for this in the future. It does require having ExifTool which I already had installed. My next step is to create a function that will read a .csv or dataframe as input and use the object Ids from one column and also return in the infoDict a relevant identifier from another column, then output all of the metadata to a dataframe.

Also happy for any feedback on my code as I'm self taught so always looking to improve.

# Import python libraries
import pandas as pd
import requests
import os
import subprocess
from SalesforceXytoolsCore import *

# Test with single item
attachID = ''
frID = 'test'
session = requests.Session()

tempFile = 'tempOutput'

rest_api = RestApi(username= '', 
                    password= '', 
                    security_token = '', 
                    session=session
                )

rest_path = '/services/data/v42.0/sobjects/Attachment/{0}/Body'.format(attachID)
result = rest_api.call_rest(method='GET',
                           path=rest_path,
                           params={})

with open(tempFile, 'wb') as f:
f.write(result.content)

exifToolPath = 'C:/Windows/exiftool.exe'
infoDict = {} #Creating the dict to get the metadata tags

process = subprocess.Popen([exifToolPath,tempFile],stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines=True)

# write tags/values from io to dict
for tag in process.stdout:
    line = tag.strip().split(':')
    infoDict[line[0].strip()] = line[-1].strip() 

for k,v in infoDict.items():
    print(k,':', v)

# to download attachment(s)    
SAVE_DIR = './attachments'
if not os.path.exists(SAVE_DIR):
    os.mkdir(SAVE_DIR)
    
with open(os.path.join(SAVE_DIR, attachID + '_' + frID + '.pdf'), mode='wb') as f:
    f.write(result.content)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.