Project

General

Profile

s3copyobject.py

Javier M. Mellid, 05/15/2015 12:14 PM

Download (2.73 KB)

 
1
#!/usr/bin/python
2

    
3
import sys
4
import boto
5
import boto.s3.key
6
import boto.s3.connection
7

    
8
# change your test users, access and secret keys here
9

    
10
user = [{'username': 'testuser1', 'access_key': 'BAX3MDFDOP3486G240TW', 'secret_key': 'te+6j+E7ROa7NDyouUQeZoOzCx7p1Hv95S3TpOkA'},
11
        {'username': 'testuser2', 'access_key': 'ECBRXYBYCBY7J6VD9PHD', 'secret_key': '2DpVj83Baf3lzMwg0YV7qStTUv7qPGigG2rPIHJg'}]
12

    
13
uploadfile  = '/etc/os-release'
14
bucket_name = user[0]['username'] + '-99999-transfer-bug-bucket'
15
file_name   = user[0]['username'] + '-99999-test-file'
16

    
17
hostname = 'localhost'
18

    
19
def connect_s3(user_id=0):
20
    return boto.connect_s3(
21
        aws_access_key_id = user[user_id]['access_key'],
22
        aws_secret_access_key = user[user_id]['secret_key'],
23
        is_secure = False,
24
        host = hostname,
25
        port = 8000,
26
        calling_format = boto.s3.connection.OrdinaryCallingFormat(),
27
)
28

    
29
def pause(next_task):
30
        raw_input('Press <Enter> to continue with ' + next_task + '...')
31

    
32
def print_content(user_id):
33
        print "Content as per " + user[user_id]['username']
34
        connection = connect_s3(user_id)
35
        bucket = connection.get_bucket(bucket_name, validate=False)
36
        print bucket.get_acl()
37
        for k in bucket.list():
38
                try:
39
                        k2 = bucket.get_key(k.name)
40
                        print "'{name}' by '{owner}' ['meta1': '{meta1}', 'meta2': '{meta2}']".format(
41
                                name = k.name,
42
                                owner = k.owner.display_name,
43
                                meta1 = k2.get_metadata('meta1'),
44
                                meta2 = k2.get_metadata('meta2'),
45
                        )
46
                        print "'{name}' => {acl}".format(
47
                                name = k.name,
48
                                acl = k.get_acl()
49
                        )
50
                except boto.exception.S3ResponseError:
51
                        print "Error reading " + k.name
52

    
53
def create_and_grant():
54
        print "Creating '{bucket}".format(
55
                bucket = bucket_name
56
        )
57
        conn = connect_s3(0)
58
        conn2 = connect_s3(1)
59
        bucket = conn.create_bucket(bucket_name)
60
        bucket.add_user_grant('FULL_CONTROL', conn2.get_canonical_user_id(), recursive=True)
61
        k = boto.s3.key.Key(bucket)
62
        k.key = file_name
63
        k.set_contents_from_filename(uploadfile)
64
        k.add_user_grant('FULL_CONTROL', conn2.get_canonical_user_id())
65

    
66
def copy_object():
67
        print "Copying object"
68
        conn = connect_s3(1)
69
        bucket = conn.get_bucket(bucket_name, validate=False)
70
        bucket.copy_key(file_name + "-02", bucket_name, file_name)
71

    
72
create_and_grant()
73
pause ("copy object")
74
copy_object()
75
pause("print results")
76
print_content(0)
77
print_content(1)