Question

Python code to upload file to S3 getting “No such file or directory” error

  • 14 March 2022
  • 2 replies
  • 2049 views

Hi folks, this one is stumping me - I have a file created in G-Suite in a Zap step and want to then upload this to a s3 site using some Python code (and then link to a database).

When I test the code, I get an error:

Traceback (most recent call last): File "<string>", line 18, in the_function FileNotFoundError: [Errno 2] No such file or directory: 'https://docs.google.com/feeds/download/documents/export/Export?id=1-Oht6-vzbYIpC5gq4rPpS83zYu

Here’s the input and the code (API Key redacted):

 

Input Data

ID : de685b16-7119-432a-8298-d919a0c3d15c

REFFILE : Ref1_file

FILE : https://docs.google.com/feeds/download/documents/export/Export?id=1-Oht6-vzbYIpC5gq4rPpS83zYu10JfAuxvJotULLevg&exportFormat=pdf

 

Code

import json

import xml.etree.ElementTree

import mimetypes

import os

import requests

 

HOST="ceracare.fountain.com/api"

API_KEY="REDACTED"

APPLICANT=input_data['ID']

FILENAME=input_data['FILE']

 

UPLOAD_FILE=(FILENAME, open(FILENAME, 'rb'), mimetypes.guess_type(FILENAME)[0])

UPLOAD_KEY=input_data['REFFILE']

UPLOAD_URL="https://%s/v2/applicants/%s/secure_documents/upload" % (HOST, APPLICANT)

LINK_UPLOAD_URL="https://%s/v2/applicants/%s/secure_documents/link_upload" % (HOST, APPLICANT)

HEADERS = { 'X-ACCESS-TOKEN': API_KEY }

 

upload_response = requests.post(UPLOAD_URL, headers=HEADERS)

upload_response_json = json.loads(upload_response.text)

form_data = upload_response_json['form_data']

form_data['Content-type'] = ""

 

s3_upload_response = requests.post(upload_response_json['url'], data=form_data, files={'file': UPLOAD_FILE})

s3_upload_response_xml = xml.etree.ElementTree.fromstring(s3_upload_response.text)

s3_filename = s3_upload_response_xml.find("Key").text

finish_upload_data = {

'key': UPLOAD_KEY,

's3_key': s3_filename,

'size': os.path.getsize(FILENAME),

}

finish_upload_response = requests.post(LINK_UPLOAD_URL, headers=HEADERS, data=finish_upload_data)

print(finish_upload_response)


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Userlevel 7
Badge +14

Hi @PeterCera 

Is there a reason you are not using the AWS S3 Zap app?

 

Hi @Troy Tessalone thanks - thanks for this. Yes, the reason were not using the AWS app is that the specific AWS location subsequent file linkage is not on our own S3 but part of a cloud SaaS provider system called Fountain (our recruitment platform). They have a specific way of uploading and linking files which they have provided the above python script for. I’m mostly confused that using the FILE url directly serves me the pdf, but in the script is not recognised. I’ll try discussing direct access to S3 but doubt I’ll get it.