[英]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.