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 1, 2018 in AWS by Cloud gunner
• 4,250 points
57 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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 : pypi.org/project/pyformulas/

Cheers!

answered Jun 1, 2018 by Flying geek
• 3,150 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
• 56,140 points
606 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,090 points
794 views
+3 votes
5 answers

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

First Option sm3cmd Use s3cmd s3cmd get s3://AWS_S3_Bucket/dir/file Take a ...READ MORE

answered Aug 29, 2018 in AWS by Priyaj
• 56,140 points
1,111 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 in AWS by anonymous
1,815 views
+13 votes
2 answers
0 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,150 points
21 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.