Skip to content

Instantly share code, notes, and snippets.

@ojmccall
Created January 12, 2022 23:03
Show Gist options
  • Save ojmccall/d8a969c09189248eb8959520b9b23329 to your computer and use it in GitHub Desktop.
Save ojmccall/d8a969c09189248eb8959520b9b23329 to your computer and use it in GitHub Desktop.
CF - SFMC SFTP TO GCS
def SFMC_SFTP(request):
import pysftp
import tempfile
import os
from shutil import copy
from google.cloud import storage
host = "your SFMC Host here"
username ="SFMC Username here"
password = "Password"
folderToGet = 'SFTP Folder to get'
bucketName = "GCS Bucket to put files in"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host=host, username=username,password=password, cnopts=cnopts) as sftp:
with sftp.cd("Import"):
print("Connecting")
with tempfile.TemporaryDirectory() as temp_dir:
sftp.get_d(folderToGet, temp_dir)
files_in_dir = os.listdir(temp_dir)
print(str(files_in_dir))
for idx, val in enumerate(files_in_dir):
filename = val
sourcepath = temp_dir+ "/"+filename
#print(sourcepath+" : "+targetpath)
print("file name: "+filename)
#copy(sourcepath,targetpath)
client = storage.Client() #Set gcloud client.
bucket = client.get_bucket(bucketName)
blob = bucket.blob(filename)
blob.upload_from_filename(sourcepath)
print("copied: "+filename)
return "Job Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment