0

I want to read the csv file from the s3 bucket using boto3 and upload it to external API using multipart/form-data request. so far I am able to read the csv

response = s3.get_object(Bucket=bucket, Key=key)
body = response['Body']

Not sure on how to convert this body into multipart. External api will be taking request in multipart/form-data. Any Suggestions would be helpful.

Taufik Pirjade
  • 380
  • 6
  • 26
  • Can you add more details? What's the specification of the external API? – jellycsc Jul 08 '21 at 19:23
  • @jellycsc external api will be taking request in multipart/form-data – Taufik Pirjade Jul 08 '21 at 19:28
  • @jellycsc i have updated my question. – Taufik Pirjade Jul 08 '21 at 19:32
  • I don't think this question has anything to do with AWS or S3. What you need is to make a request with `multipart/form-data`, check out whether this answer helps or not: https://stackoverflow.com/a/12385661/2361752 – xhg Jul 08 '21 at 20:14
  • @xhg i want to convert my response which is taken from boto3 to multipart. I have checked the link, but I haven't got any solution. – Taufik Pirjade Jul 09 '21 at 16:20
  • you can start by: 1. understand what a multipart request needs, is it a stream or other data type; 2. understand what data type boto3 produced; 3. if the types match, great, if not, you need to do conversion. – xhg Jul 09 '21 at 23:02

1 Answers1

0

Following method solved my issue.

body = response['Body'].read()
multipart_data = MultipartEncoder(
        fields={
            'file': (file_name, body, 'application/vnd.ms-excel'),
            'field01': 'test'
        }
    )

.read() method will convert the file into binary string.

Taufik Pirjade
  • 380
  • 6
  • 26