简体   繁体   中英

Writing csv file to Amazon S3 using python

My requirement is to generate csv file and append to a file in 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. Please help me, i am newbie in Python and Amazon S3 world. Thanks in advance.

It's not possible to append to existing S3 object. You will have to upload the entire content and replace the old object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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