-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.py
119 lines (106 loc) · 4.53 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import botocore_tornado.session
import botocore.session
from tornado import gen
from tornado.ioloop import IOLoop
bucket = 'botocore-tornado-test' # change this to your bucket name
key = 'xyzzy' # a subfolder under the bucket
filename = 'testfile.txt' # the file we will put into S3
region = 'us-east-1' # change this to your region
acl = 'public-read' # we are going to set the ACL to public-read so we can access the file via a url
@gen.coroutine
def main_async():
session = botocore_tornado.session.get_session()
session.set_debug_logger()
session.set_debug_logger('botocore_tornado')
s3 = session.get_service('s3')
endpoint = s3.get_endpoint(region)
print "========================="
print "====== ASYNC TEST ======="
print "========================="
print
print "uploading the file to s3"
fp = open('./' + filename, 'rb')
operation = s3.get_operation('PutObject')
http_response, response_data = yield operation.call(endpoint,
bucket=bucket,
key=key + '/' + filename,
body=fp)
print http_response
print response_data
print
print "getting s3 object properties of file we just uploaded"
operation = s3.get_operation('GetObjectAcl')
http_response, response_data = yield operation.call(endpoint,
bucket=bucket,
key=key + '/' + filename)
print http_response
print response_data
print
print "setting the acl to public-read"
operation = s3.get_operation('PutObjectAcl')
http_response, response_data = yield operation.call(endpoint,
bucket=bucket,
key=key + '/' + filename,
acl=acl)
print http_response
print response_data
print
print "The url of the object is:"
print
print 'http://'+bucket+'.s3.amazonaws.com/' + key + '/' + filename
operation = s3.get_operation('DeleteObject')
http_response, response_data = yield operation.call(endpoint,
bucket=bucket,
key=key + '/' + filename)
print http_response
print response_data
def main_sync():
session = botocore.session.get_session()
session.set_debug_logger()
s3 = session.get_service('s3')
endpoint = s3.get_endpoint(region)
print "========================="
print "====== SYNC TEST ========"
print "========================="
print
print "uploading the file to s3"
fp = open('./' + filename, 'rb')
operation = s3.get_operation('PutObject')
http_response, response_data = operation.call(endpoint,
bucket=bucket,
key=key + '/' + filename,
body=fp)
print http_response
print response_data
print
print "getting s3 object properties of file we just uploaded"
operation = s3.get_operation('GetObjectAcl')
http_response, response_data = operation.call(endpoint,
bucket=bucket,
key=key + '/' + filename)
print http_response
print response_data
print
print "setting the acl to public-read"
operation = s3.get_operation('PutObjectAcl')
http_response, response_data = operation.call(endpoint,
bucket=bucket,
key=key + '/' + filename,
acl=acl)
print http_response
print response_data
print
print "The url of the object is:"
print
print 'http://'+bucket+'.s3.amazonaws.com/' + key + '/' + filename
operation = s3.get_operation('DeleteObject')
http_response, response_data = operation.call(endpoint,
bucket=bucket,
key=key + '/' + filename)
print http_response
print response_data
if __name__ == '__main__':
with open(filename, 'w') as f:
f.write("botocore tornado upload test file")
IOLoop.instance().run_sync(main_async)
main_sync()