我必须使用python将文件从远程计算机传输到amazon ec2,然后从那里传输到Amazon s3。我已成功上传了一个文本文件,但不知道如何上传多个文件。以下是一个文件的两个程序的代码。
获取文件到ec2
import urllib
source = urllib.urlopen('url').read()
fhand = open('file2.txt','w')
fhand.write(source)
fhand.close()
将文件上传到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
答案 0 :(得分:0)
循环从ec2
获取文件import urllib,os
sources = ['//url1/file1.txt','//url2/file2.txt','//url3/file3.txt'] # List of urls
for myurl in sources:
source = urllib.urlopen(myurl).read() #Open each url file
fname = os.path.split(myurl)[1] #Split the url to get the file name
print('Copying file:',fname)
fhand = open(fname,'w')
fhand.write(source)
fhand.close()
使用相同的原则从list
mylist发送文件= [" a"," b"," c"]
https://wiki.python.org/moin/ForLoop
注意:
如果在不同的URL中使用相同的文件名,结果将被覆盖,因此如果是概率,您将需要添加测试或其他一些应对机制。
我使用os.path.split()
来获取文件名,您可能会发现urlparse
函数可以为您提供更好的里程,更复杂的网址
答案 1 :(得分:0)
如果在每台计算机上安装了python,以下是如何使用pyformulas传输文件。
首先安装pyformulas pip install pyformulas==0.2.7
然后运行服务器(接收文件):
服务器:
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)]
然后客户端(发送文件):
客户端:
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) ]