test.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import boto
  2. from boto.s3.key import Key
  3. from flask import Flask, request, render_template
  4. app = Flask(__name__)
  5. @app.route('/', methods=['GET', 'POST'])
  6. def index():
  7. # If user has submitted the form...
  8. if request.method == 'POST':
  9. # Connect to Amazon S3
  10. s3 = boto.connect_s3()
  11. # Get a handle to the S3 bucket
  12. bucket_name = 'mybucket'
  13. bucket = s3.get_bucket(bucket_name)
  14. k = Key(bucket)
  15. # Loop over the list of files from the HTML input control
  16. data_files = request.files.getlist('file[]')
  17. for data_file in data_files:
  18. # Read the contents of the file
  19. file_contents = data_file.read()
  20. # Use Boto to upload the file to the S3 bucket
  21. k.key = data_file.filename
  22. print( "Uploading some data to " + bucket_name + " with key: " + k.key)
  23. k.set_contents_from_string(file_contents)
  24. return render_template('index.html')
  25. if __name__ == '__main__':
  26. app.run(
  27. host='0.0.0.0',
  28. port=int('8080'),
  29. debug=True
  30. )