2. Download thu vien boto:
Vao Developer/Sample code & Libraries, search boto
3. Giai nen va chay lenh python setup.py install
4. Neu muon nhung code: copy thu muc boto vao source project
5. Introduction and sample: http://boto.cloudhackers.com/s3_tut.html,
6. Su dung boto voi Amazon S3, tren web2py:
record_create.html:
{{=form.custom.begin}}
input tyle="file" name="img" >
{{=form.custom.end}}
trong controllers cua web2py: record.py
from boto.s3.connection import S3Connection
from boto.s3.key import Key
...
def record_create
...
conn = S3Connection('aws_access_key', 'secret_key')
# rs = conn.get_all_buckets()
bucket = conn.get_bucket('myimg') # Trong TH nay bucket myimg da duoc tao san
if request.vars.has_key('img'):
img = request.vars['img']
if img.file:
#data = img.file.read()
k = Key(bucket)
k.key = 'Winter.jpg'
k.set_contents_from_file(img.file, headers=None, replace=True, cb=None, num_cb=10, policy=None, md5=None)
OK. That's all
Load anh truc tiep tu Amazon: (bucket phai duoc public)
http://bucket_name.s3.amazonaws.com/image_name?AWSAccessKeyId=a ws_acesskey
Quan li % upload:
....
bucket = conn.create_bucket(bucket_name,
location=s3.connection.Location.DEFAULT)
testfile = "replace this with an actual filename"
print 'Uploading %s to Amazon S3 bucket %s' % \
(testfile, bucket_name)
import sys
def percent_cb(complete, total):
sys.stdout.write('.')
sys.stdout.flush()
from boto.s3.key import Key
k = Key(bucket)
k.key = 'my test file'
k.set_contents_from_filename(testfile, cb=percent_cb, num_cb=10)
location=s3.connection.Location.DEFAULT)
testfile = "replace this with an actual filename"
print 'Uploading %s to Amazon S3 bucket %s' % \
(testfile, bucket_name)
import sys
def percent_cb(complete, total):
sys.stdout.write('.')
sys.stdout.flush()
from boto.s3.key import Key
k = Key(bucket)
k.key = 'my test file'
k.set_contents_from_filename(testfile, cb=percent_cb, num_cb=10)
Upload to S3
Here is the code we use to upload the picture files:
01 | def push_picture_to_s3( id ): |
02 | try : |
03 | import boto |
04 | from boto.s3.key import Key |
05 | # set boto lib debug to critical |
06 | logging.getLogger( 'boto' ).setLevel(logging.CRITICAL) |
07 | bucket_name = settings.BUCKET_NAME |
08 | # connect to the bucket |
09 | conn = boto.connect_s3(settings.AWS_ACCESS_KEY_ID, |
10 | settings.AWS_SECRET_ACCESS_KEY) |
11 | bucket = conn.get_bucket(bucket_name) |
12 | # go through each version of the file |
13 | key = '%s.png' % id |
14 | fn = '/var/www/data/%s.png' % id |
15 | # create a key to keep track of our file in the storage |
16 | k = Key(bucket) |
17 | k.key = key |
18 | k.set_contents_from_filename(fn) |
19 | # we need to make it public so it can be accessed publicly |
20 | # using a URL like http://s3.amazonaws.com/bucket_name/key |
21 | k.make_public() |
22 | # remove the file from the web server |
23 | os.remove(fn) |
24 | except : |
25 | ... |
Không có nhận xét nào:
Đăng nhận xét