简体   繁体   English

如何使用Python将json文件复制到Amazon S3

[英]How to copy json file to Amazon S3 using Python

I am experimenting with writing a json file to AWS S3. 我正在尝试将json文件写入AWS S3。 Below is the sample code. 下面是示例代码。 This is the file I want to write 'fileNew.json'. 这是我要写的文件“ fileNew.json”。 And 'fileOld.json' is an existing file in S3, which I included in the code by mistake and shouldnt be in the code. “ fileOld.json”是S3中的现有文件,我将其错误地包含在代码中,不应包含在代码中。

df.to_json('fileNew.json', orient='records',lines=True)

os.system('aws s3 cp fileNew.json s3://sbx-myproject/fileOld.json --sse')

Will the above command replace the existing file ? 上面的命令会替换现有文件吗? OR will it be just unsuccessful in creating the new file ? 还是创建新文件不成功?

If a file already exists it will be automatically overwritten. 如果文件已经存在,它将被自动覆盖。 So yes fileOld.json will be replaced with the file you are uploading. 因此,是的,fileOld.json将替换为您正在上传的文件。

While your code will work, it is recommended you use the AWS SDK instead of executing shell commands. 虽然代码可以使用,但建议您使用AWS开发工具包而不是执行Shell命令。

import boto3

data = open('fileNew.json', 'rb')
s3 = boto3.resource('s3')
s3.Bucket('sbx-myproject').put_object(Key='fileOld.json', Body=data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用python boto将json文件从我的本地计算机复制到Amazon S3 - using python boto to copy json file from my local machine to amazon S3 使用 Amazon Athena 从 S3 读取多个 json 文件 - Reading multiple json file from S3 using Amazon Athena 使用python将csv文件写入Amazon S3 - Writing csv file to Amazon S3 using python 使用 Python 将标准 JSON 文件转换为 json-serde 格式并上传到 Amazon Athena(Presto、Hive)的 AWS S3 存储桶 - Convert standard JSON file to json-serde format using Python & upload to AWS S3 bucket for Amazon Athena (Presto, Hive) Python-如何获取公共Amazon S3文件的元数据字典? - Python - How to get the metadata dictionary of a public Amazon S3 file? 如何从python或ruby将Excel文件保存到Amazon S3 - How to save excel file to amazon s3 from python or ruby 如何使用 python (boto3) 连接到带有 pem 文件的 Amazon S3 存储桶 - How do I connect to an Amazon S3 bucket with a pem file using python (boto3) 如何在Google App Engine中使用Python(和boto)通过浏览器从Amazon S3下载文件? - How to download a file via the browser from Amazon S3 using Python (and boto) at Google App Engine? 如何使用python boto生成亚马逊S3中存在的文件的下载链接? - how to generate a download link for a file present in amazon S3 using python boto? 如何使用Python请求在Amazon S3上执行“PUT” - How to do `PUT` on Amazon S3 using Python Requests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM