Wants to transfer files using python

0 votes

Want to transfer files from my remote computer to amazon ec2 and then from there to Amazon s3 using python . I have successfully uploaded one text file but dont know how to upload multiple files. Here are the codes for two programs for one file.

to get file to ec2

import urllib
source = urllib.urlopen('url').read()
fhand = open('file2.txt','w')
fhand.write(source)
fhand.close()

to upload file to s3

import boto
from boto.s3.key import Key
keyId = "acess key"
skeyId = "secret key"

fileName="file2.txt"
bucketName="bname"
file=open(fileName)

conn = boto.connect_s3(keyId,skeyId)
print conn
bucket = conn.get_bucket(bucketName)
print bucket
k = Key(bucket)
print k
k.key=fileName
print k.key
result = k.set_contents_from_file(file)
print result
Jun 2, 2018 in AWS by Cloud gunner
• 4,670 points
1,687 views

1 answer to this question.

0 votes

First install pyformulas pip install pyformulas==0.2.7

Then run the server (receives the files): 

For Server: 

import pyformulas as pf

class server(pf.net.Stream):
    file = bytes()
    def on_receive(self, conn, buffer):
        self.file += buffer

    def on_disconnect(self, conn):
        with open('file_%i' % self.port, 'wb') as file:
            file.write(self.file)

servers = [server(port=1000+i) for i in range(3)]


Then the client (sends the files):

Client:

import pyformulas as pf

class send_file(pf.net.Stream):
    def __init__(self, port, file, address):
        self.file = file
        super(send_file, self).__init__(port, address)

    def on_connect(self, conn):
        conn.send(self.file)
        conn.disconnect()

filenames = ['0.txt', '1.wav', '2.bin'] # Put the filenames here

[ send_file(port=1000+idx, file=open(fname, 'rb').read(), address='server ip address') for idx, fname in enumerate(filenames) ]


Hope it helps, took reference from : https://pypi.org/project/pyformulas/

Cheers!


answered Jun 2, 2018 by Flying geek
• 3,280 points

Related Questions In AWS

+1 vote
2 answers

How to install Python MySQLdb module using pip?

It's easy to do, but hard to ...READ MORE

answered Aug 1, 2018 in AWS by Priyaj
• 58,020 points
5,737 views
0 votes
1 answer

AWS S3 CLI : error while trying to copy files locally using terminal

For the first error you should add ...READ MORE

answered Aug 3, 2018 in AWS by Archana
• 4,170 points
11,408 views
+3 votes
6 answers

How to move files from amazon ec2 to s3 bucket using command line

Hey, 3 ways you can do this: To ...READ MORE

answered Oct 9, 2018 in AWS by Omkar
• 69,220 points
20,004 views
0 votes
2 answers

How to display just the name of files using aws s3 ls command?

aws s3 ls s3://<your_bucket_name>/ | awk '{print ...READ MORE

answered Mar 17, 2019 in AWS by anonymous
22,212 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,036 views
+2 votes
1 answer
0 votes
1 answer

How to test code written against AWS API

The actual uploading and the tests that ...READ MORE

answered May 17, 2018 in AWS by Flying geek
• 3,280 points
666 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP