Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing json to file in s3 bucket

Tags:

python

boto3

This code writes json to a file in s3, what i wanted to achieve is instead of opening data.json file and writing to s3 (sample.json) file,

how do i pass the json directly and write to a file in s3 ?

import boto3  s3 = boto3.resource('s3', aws_access_key_id='aws_key', aws_secret_access_key='aws_sec_key') s3.Object('mybucket', 'sample.json').put(Body=open('data.json', 'rb')) 
like image 509
Learning Avatar asked Oct 20 '17 07:10

Learning


People also ask

Can you write to a file in S3?

You can write a file or data to S3 Using Boto3 using the Object. put() method. Other methods available to write a file to s3 are, Object.

Can S3 store JSON?

Amazon S3 Select works on objects stored in CSV, JSON, or Apache Parquet format. It also works with objects that are compressed with GZIP or BZIP2 (for CSV and JSON objects only), and server-side encrypted objects.

How do you write a JSON file in Python?

Method 2: Writing JSON to a file in Python using json.dump() Another way of writing JSON to a file is by using json. dump() method The JSON package has the “dump” function which directly writes the dictionary to a file in the form of JSON, without needing to convert it into an actual JSON object.


1 Answers

I'm not sure, if I get the question right. You just want to write JSON data to a file using Boto3? The following code writes a python dictionary to a JSON file.

import json import boto3     s3 = boto3.resource('s3') s3object = s3.Object('your-bucket-name', 'your_file.json')  s3object.put(     Body=(bytes(json.dumps(json_data).encode('UTF-8'))) ) 
like image 117
Uwe Bretschneider Avatar answered Sep 17 '22 14:09

Uwe Bretschneider