简体   繁体   English

使用python将csv文件写入Amazon S3

[英]Writing csv file to Amazon S3 using python

My requirement is to generate csv file and append to a file in Amazon S3. 我的要求是生成csv文件并追加到Amazon S3中的文件。 I am able to do it using loop. 我可以使用循环来做到这一点。 My code is something like this. 我的代码是这样的。

import csv
import random
import simples3
from  simples3 import S3Bucket


quantity=['12','5','13','2','7','6','4','9','15','21','33','62','54','18','26','1']
unitPrice=['1$','2$','3$','4$','5$','6$','7$','8$','9$','10$']



loop= [10]

myfile = open('testcsv.csv', 'ab')
s = S3Bucket('bucket_name',access_key='access_key',secret_key='secret_key')

for a in range(10):

    writing=(random.choice(quantity),random.choice(unitPrice))
    wr =csv.writer(myfile)
    wr.writerow(writing)

    loop.append(writing)    

myfile.close()
print loop
pls = str(loop)
s.put("testconnection.csv",pls,mimetype="text/plain")

Now i want to write to the bucket file within my loop as i did for file in my local file system. 现在,我想像在本地文件系统中一样对循环中的存储桶文件进行写入。 I am not getting enough method in S3Bucket to append to a file and put is overwriting the content of file each time the loop executes. 我不是S3Bucket得到足够的方法追加到一个文件, put每天执行一次循环覆盖文件的内容。 Please help me, i am newbie in Python and Amazon S3 world. 请帮助我,我是Python和Amazon S3世界的新手。 Thanks in advance. 提前致谢。

It's not possible to append to existing S3 object. 无法附加到现有的S3对象。 You will have to upload the entire content and replace the old object. 您将必须上传整个内容并替换旧对象。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM