| 
									
										
										
										
											2014-08-27 11:17:06 -04:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							| 
									
										
										
										
											2014-08-26 13:25:50 -04:00
										 |  |  | from six.moves.urllib.request import urlopen | 
					
						
							|  |  |  | from six.moves.urllib.error import HTTPError | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import boto | 
					
						
							|  |  |  | from boto.exception import S3ResponseError | 
					
						
							|  |  |  | from boto.s3.key import Key | 
					
						
							|  |  |  | from boto.s3.connection import OrdinaryCallingFormat | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from freezegun import freeze_time | 
					
						
							|  |  |  | import requests | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import sure  # noqa | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | from moto import mock_s3, mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def create_connection(key=None, secret=None): | 
					
						
							|  |  |  |     return boto.connect_s3(key, secret, calling_format=OrdinaryCallingFormat()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MyModel(object): | 
					
						
							|  |  |  |     def __init__(self, name, value): | 
					
						
							|  |  |  |         self.name = name | 
					
						
							|  |  |  |         self.value = value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def save(self): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn = create_connection("the_key", "the_secret") | 
					
						
							|  |  |  |         bucket = conn.get_bucket("mybucket") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |         k = Key(bucket) | 
					
						
							|  |  |  |         k.key = self.name | 
					
						
							|  |  |  |         k.set_contents_from_string(self.value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_my_model_save(): | 
					
						
							|  |  |  |     # Create Bucket so that test can run | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = create_connection("the_key", "the_secret") | 
					
						
							|  |  |  |     conn.create_bucket("mybucket") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     #################################### | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     model_instance = MyModel("steve", "is awesome") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     model_instance.save() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.get_bucket("mybucket").get_key("steve").get_contents_as_string().should.equal( | 
					
						
							|  |  |  |         b"is awesome" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_missing_key(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = create_connection("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     bucket = conn.create_bucket("foobar") | 
					
						
							|  |  |  |     bucket.get_key("the-key").should.equal(None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_missing_key_urllib2(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = create_connection("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     conn.create_bucket("foobar") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     urlopen.when.called_with("http://s3.amazonaws.com/foobar/the-key").should.throw( | 
					
						
							|  |  |  |         HTTPError | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_empty_key(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = create_connection("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     bucket = conn.create_bucket("foobar") | 
					
						
							|  |  |  |     key = Key(bucket) | 
					
						
							|  |  |  |     key.key = "the-key" | 
					
						
							|  |  |  |     key.set_contents_from_string("") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     bucket.get_key("the-key").get_contents_as_string().should.equal(b"") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_empty_key_set_on_existing_key(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = create_connection("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     bucket = conn.create_bucket("foobar") | 
					
						
							|  |  |  |     key = Key(bucket) | 
					
						
							|  |  |  |     key.key = "the-key" | 
					
						
							|  |  |  |     key.set_contents_from_string("foobar") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     bucket.get_key("the-key").get_contents_as_string().should.equal(b"foobar") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     key.set_contents_from_string("") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     bucket.get_key("the-key").get_contents_as_string().should.equal(b"") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_large_key_save(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = create_connection("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     bucket = conn.create_bucket("foobar") | 
					
						
							|  |  |  |     key = Key(bucket) | 
					
						
							|  |  |  |     key.key = "the-key" | 
					
						
							|  |  |  |     key.set_contents_from_string("foobar" * 100000) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     bucket.get_key("the-key").get_contents_as_string().should.equal(b"foobar" * 100000) | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_copy_key(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = create_connection("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     bucket = conn.create_bucket("foobar") | 
					
						
							|  |  |  |     key = Key(bucket) | 
					
						
							|  |  |  |     key.key = "the-key" | 
					
						
							|  |  |  |     key.set_contents_from_string("some value") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     bucket.copy_key("new-key", "foobar", "the-key") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     bucket.get_key("the-key").get_contents_as_string().should.equal(b"some value") | 
					
						
							|  |  |  |     bucket.get_key("new-key").get_contents_as_string().should.equal(b"some value") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_set_metadata(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = create_connection("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     bucket = conn.create_bucket("foobar") | 
					
						
							|  |  |  |     key = Key(bucket) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     key.key = "the-key" | 
					
						
							|  |  |  |     key.set_metadata("md", "Metadatastring") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     key.set_contents_from_string("Testval") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     bucket.get_key("the-key").get_metadata("md").should.equal("Metadatastring") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @freeze_time("2012-01-01 12:00:00") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_last_modified(): | 
					
						
							|  |  |  |     # See https://github.com/boto/boto/issues/466 | 
					
						
							|  |  |  |     conn = create_connection() | 
					
						
							|  |  |  |     bucket = conn.create_bucket("foobar") | 
					
						
							|  |  |  |     key = Key(bucket) | 
					
						
							|  |  |  |     key.key = "the-key" | 
					
						
							|  |  |  |     key.set_contents_from_string("some value") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     rs = bucket.get_all_keys() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     rs[0].last_modified.should.equal("2012-01-01T12:00:00.000Z") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     bucket.get_key("the-key").last_modified.should.equal( | 
					
						
							|  |  |  |         "Sun, 01 Jan 2012 12:00:00 GMT" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_missing_bucket(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = create_connection("the_key", "the_secret") | 
					
						
							|  |  |  |     conn.get_bucket.when.called_with("mybucket").should.throw(S3ResponseError) | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_bucket_with_dash(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = create_connection("the_key", "the_secret") | 
					
						
							|  |  |  |     conn.get_bucket.when.called_with("mybucket-test").should.throw(S3ResponseError) | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_bucket_deletion(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = create_connection("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     bucket = conn.create_bucket("foobar") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     key = Key(bucket) | 
					
						
							|  |  |  |     key.key = "the-key" | 
					
						
							|  |  |  |     key.set_contents_from_string("some value") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Try to delete a bucket that still has keys | 
					
						
							|  |  |  |     conn.delete_bucket.when.called_with("foobar").should.throw(S3ResponseError) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bucket.delete_key("the-key") | 
					
						
							|  |  |  |     conn.delete_bucket("foobar") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Get non-existing bucket | 
					
						
							|  |  |  |     conn.get_bucket.when.called_with("foobar").should.throw(S3ResponseError) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-20 15:21:11 -08:00
										 |  |  |     # Delete non-existent bucket | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     conn.delete_bucket.when.called_with("foobar").should.throw(S3ResponseError) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_get_all_buckets(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = create_connection("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     conn.create_bucket("foobar") | 
					
						
							|  |  |  |     conn.create_bucket("foobar2") | 
					
						
							|  |  |  |     buckets = conn.get_all_buckets() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     buckets.should.have.length_of(2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3 | 
					
						
							|  |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_post_to_bucket(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = create_connection("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     bucket = conn.create_bucket("foobar") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     requests.post( | 
					
						
							|  |  |  |         "https://s3.amazonaws.com/foobar", {"key": "the-key", "file": "nothing"} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     bucket.get_key("the-key").get_contents_as_string().should.equal(b"nothing") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3 | 
					
						
							|  |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_post_with_metadata_to_bucket(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = create_connection("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     bucket = conn.create_bucket("foobar") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     requests.post( | 
					
						
							|  |  |  |         "https://s3.amazonaws.com/foobar", | 
					
						
							|  |  |  |         {"key": "the-key", "file": "nothing", "x-amz-meta-test": "metadata"}, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     bucket.get_key("the-key").get_metadata("test").should.equal("metadata") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_bucket_name_with_dot(): | 
					
						
							|  |  |  |     conn = create_connection() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     bucket = conn.create_bucket("firstname.lastname") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     k = Key(bucket, "somekey") | 
					
						
							|  |  |  |     k.set_contents_from_string("somedata") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_key_with_special_characters(): | 
					
						
							|  |  |  |     conn = create_connection() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     bucket = conn.create_bucket("test_bucket_name") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     key = Key(bucket, "test_list_keys_2/*x+?^@~!y") | 
					
						
							|  |  |  |     key.set_contents_from_string("value1") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     key_list = bucket.list("test_list_keys_2/", "/") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     keys = [x for x in key_list] | 
					
						
							| 
									
										
										
										
											2014-01-07 12:59:10 -08:00
										 |  |  |     keys[0].name.should.equal("test_list_keys_2/*x+?^@~!y") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | def test_bucket_key_listing_order(): | 
					
						
							|  |  |  |     conn = create_connection() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     bucket = conn.create_bucket("test_bucket") | 
					
						
							|  |  |  |     prefix = "toplevel/" | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def store(name): | 
					
						
							|  |  |  |         k = Key(bucket, prefix + name) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         k.set_contents_from_string("somedata") | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     names = ["x/key", "y.key1", "y.key2", "y.key3", "x/y/key", "x/y/z/key"] | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for name in names: | 
					
						
							|  |  |  |         store(name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     delimiter = None | 
					
						
							|  |  |  |     keys = [x.name for x in bucket.list(prefix, delimiter)] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     keys.should.equal( | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             "toplevel/x/key", | 
					
						
							|  |  |  |             "toplevel/x/y/key", | 
					
						
							|  |  |  |             "toplevel/x/y/z/key", | 
					
						
							|  |  |  |             "toplevel/y.key1", | 
					
						
							|  |  |  |             "toplevel/y.key2", | 
					
						
							|  |  |  |             "toplevel/y.key3", | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     delimiter = "/" | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     keys = [x.name for x in bucket.list(prefix, delimiter)] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     keys.should.equal( | 
					
						
							|  |  |  |         ["toplevel/y.key1", "toplevel/y.key2", "toplevel/y.key3", "toplevel/x/"] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Test delimiter with no prefix | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     delimiter = "/" | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  |     keys = [x.name for x in bucket.list(prefix=None, delimiter=delimiter)] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     keys.should.equal(["toplevel/"]) | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     delimiter = None | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     keys = [x.name for x in bucket.list(prefix + "x", delimiter)] | 
					
						
							|  |  |  |     keys.should.equal(["toplevel/x/key", "toplevel/x/y/key", "toplevel/x/y/z/key"]) | 
					
						
							| 
									
										
										
										
											2013-10-28 13:43:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     delimiter = "/" | 
					
						
							|  |  |  |     keys = [x.name for x in bucket.list(prefix + "x", delimiter)] | 
					
						
							|  |  |  |     keys.should.equal(["toplevel/x/"]) | 
					
						
							| 
									
										
										
										
											2015-09-04 18:48:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2015-09-04 18:48:48 +02:00
										 |  |  | def test_delete_keys(): | 
					
						
							|  |  |  |     conn = create_connection() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     bucket = conn.create_bucket("foobar") | 
					
						
							| 
									
										
										
										
											2015-09-04 18:48:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     Key(bucket=bucket, name="file1").set_contents_from_string("abc") | 
					
						
							|  |  |  |     Key(bucket=bucket, name="file2").set_contents_from_string("abc") | 
					
						
							|  |  |  |     Key(bucket=bucket, name="file3").set_contents_from_string("abc") | 
					
						
							|  |  |  |     Key(bucket=bucket, name="file4").set_contents_from_string("abc") | 
					
						
							| 
									
										
										
										
											2015-09-04 18:48:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     result = bucket.delete_keys(["file2", "file3"]) | 
					
						
							| 
									
										
										
										
											2015-09-04 18:48:48 +02:00
										 |  |  |     result.deleted.should.have.length_of(2) | 
					
						
							|  |  |  |     result.errors.should.have.length_of(0) | 
					
						
							|  |  |  |     keys = bucket.get_all_keys() | 
					
						
							|  |  |  |     keys.should.have.length_of(2) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     keys[0].name.should.equal("file1") | 
					
						
							| 
									
										
										
										
											2015-09-04 18:48:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2015-09-04 18:48:48 +02:00
										 |  |  | def test_delete_keys_with_invalid(): | 
					
						
							|  |  |  |     conn = create_connection() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     bucket = conn.create_bucket("foobar") | 
					
						
							| 
									
										
										
										
											2015-09-04 18:48:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     Key(bucket=bucket, name="file1").set_contents_from_string("abc") | 
					
						
							|  |  |  |     Key(bucket=bucket, name="file2").set_contents_from_string("abc") | 
					
						
							|  |  |  |     Key(bucket=bucket, name="file3").set_contents_from_string("abc") | 
					
						
							|  |  |  |     Key(bucket=bucket, name="file4").set_contents_from_string("abc") | 
					
						
							| 
									
										
										
										
											2015-09-04 18:48:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     result = bucket.delete_keys(["abc", "file3"]) | 
					
						
							| 
									
										
										
										
											2015-09-04 18:48:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     result.deleted.should.have.length_of(1) | 
					
						
							|  |  |  |     result.errors.should.have.length_of(1) | 
					
						
							|  |  |  |     keys = bucket.get_all_keys() | 
					
						
							|  |  |  |     keys.should.have.length_of(3) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     keys[0].name.should.equal("file1") |