Home > other >  How do I get my file to be saved as JSON after writing a JSON file into AWS S3 bucket
How do I get my file to be saved as JSON after writing a JSON file into AWS S3 bucket

Time:05-23

I am trying to write a JSON file into my AWS S3 bucket. However, I do not get a JSON file after it has been uploaded.

I get my data from a website using a request.get() and format it into a JSON file which I then run the following script to upload it to me S3 bucket.

r = requests.get(url=id_url, params=params)
data = r.json()

s3_client.put_object(
    Body=json.dumps(data, indent=3),
    Bucket='bucket-name',
    Key=fileName
)

However, I am not sure what the type of file is but it is supposed to be saved as a JSON file.

Screenshot of my S3 bucket having no file type

Screenshot of my download folder, showing unable to identify the file type

When I open the file by selecting Pycharm, it is just a dictionary with key and values

CodePudding user response:

Solved it, I simply added ".JSON" to the filename and it has solved the file formatting issue. Dont know why I didnt think of this earlier.

Thank you

CodePudding user response:

Ideally you shouldn't rely of file extensions to specify the content type. The put_object method supports specifying ContentType. This means that you can use any file name you like, without needing to specify .json.

e.g.

s3_client.put_object(
    Body=json.dumps(data, indent=3),
    Bucket='bucket-name',
    Key=fileName,
    ContentType='application/json'
)
  • Related