def test_delete_file(self): # this is creating a bucket in the moto/mock s3 service s3conn = boto3.resource('s3') s3conn.create_bucket(Bucket='castletest') fileOb = upload_file_to_castle(self) s3, bucket = aws.get_bucket(s3_bucket='castletest') uid = IUUID(fileOb) key = aws.KEY_PREFIX + uid aws.move_file(fileOb) # before the delete operation, the file should exist # on s3, and this statement should not raise an exception s3.meta.client.head_object(Bucket=bucket.name, Key=key) aws.delete_file(uid) # after the delete operation, the file should not exist # on s3, but should still exist in plone (even if it # has no data...apparently...admittedly a place of possible # improvement?) self.assertRaises( botocore.exceptions.ClientError, lambda: s3.meta.client.head_object(Bucket=bucket.name, Key=key))
def __init__(self, site, request): self.site = site self.request = request bucket_name = api.portal.get_registry_record('castle.aws_s3_bucket_name') self.configured_bucket_name = bucket_name self.s3, self.bucket = aws.get_bucket(bucket_name) self.archive_storage = archival.Storage(site)
def test_get_bucket(self): # this is creating a bucket in the moto/mock s3 service s3conn = boto3.resource('s3') s3conn.create_bucket(Bucket='castletest') s3, bucket = aws.get_bucket("castletest") self.assertIsNotNone(s3) self.assertIsNotNone(bucket)
def test_move_file_public(self): # this is creating a bucket in the moto/mock s3 service s3conn = boto3.resource('s3') s3conn.create_bucket(Bucket='castletest') fileOb = upload_file_to_castle(self) api.content.transition(fileOb, 'publish') s3, bucket = aws.get_bucket("castletest") uid = IUUID(fileOb) key = aws.KEY_PREFIX + uid # before move, there should be none of the meta information about the # moved version of the file self.assertFalse(hasattr(fileOb.file, 'original_filename')) self.assertFalse(hasattr(fileOb.file, 'original_size')) self.assertFalse(hasattr(fileOb.file, 'original_content_type')) # before move, there should be no annotations annotations = IAnnotations(fileOb) self.assertIsNone(annotations.get(aws.STORAGE_KEY, None)) # before move, there should be no file in s3 self.assertRaises( botocore.exceptions.ClientError, lambda: s3.meta.client.head_object(Bucket=bucket.name, Key=key)) aws.move_file(fileOb) fileOb = api.content.get(path='/file-repository/foobar.bin') # after move, there should be additional meta information on the # file object self.assertTrue(hasattr(fileOb.file, 'original_filename')) self.assertTrue(hasattr(fileOb.file, 'original_size')) self.assertTrue(hasattr(fileOb.file, 'original_content_type')) # after move, there should be annotations on the object annotations = IAnnotations(fileOb) url = annotations[aws.STORAGE_KEY].get('url', None) expires_in = annotations[aws.STORAGE_KEY].get('expires_in', None) generated_on = annotations[aws.STORAGE_KEY].get('generated_on', None) self.assertIsNotNone(url) self.assertIsNotNone(expires_in) self.assertIsNotNone(generated_on) self.assertEqual(expires_in, 0) # after move, there should be a file in s3, and checking for it should # produce no error s3.meta.client.head_object(Bucket=bucket.name, Key=key)
def test_move_resource(self): # this is creating a bucket in the moto/mock s3 service s3conn = boto3.resource('s3', endpoint_url=self.test_bucket_endpoint) s3conn.create_bucket(Bucket='castletest') s3, bucket = aws.get_bucket("castletest") storage = archival.Storage(self.portal) testcontent = 'this is some content' moveresource = api.content.create(type='Document', id='moveresource', container=self.portal) moveresource.content = testcontent api.content.transition(moveresource, 'publish') vpath = "/moveresource" url = self.portal.absolute_url() + vpath new_url = storage.move_resource(url, use_vhm=False) self.assertIsNotNone(new_url) try: # trim off, e.g., 'https://s3.amazonaws.com/bucketname' # and then convert the path back from the url escaped version droppart = "{}/{}/".format(self.test_bucket_endpoint, self.test_bucket_name) content_path = unquote_plus(new_url[len(droppart):]) bucket.Object(content_path).load() except botocore.exceptions.ClientError: self.fail("object does not exist after move") # move by url of content again new_url2 = storage.move_resource(url, use_vhm=False) self.assertEqual(new_url, new_url2) # test for existence of content in aws still try: # trim off 'https://s3.amazonaws.com/castletest/' # and then convert the path back from the url escaped version droppart = "{}/{}/".format(self.test_bucket_endpoint, self.test_bucket_name) content_path = unquote_plus(new_url[len(droppart):]) bucket.Object(content_path).load() except botocore.exceptions.ClientError: self.fail("object does not exist after move")
def test_move_to_aws(self): # this is creating a bucket in the moto/mock s3 service s3conn = boto3.resource('s3', endpoint_url=self.test_bucket_endpoint) s3conn.create_bucket(Bucket='castletest') s3, bucket = aws.get_bucket("castletest") storage = archival.Storage(self.portal) content = "this is a test" content_path = "a/test/path/for/this/test.html" content_type = "text/html; charset=utf-8" # the key should not be there before we run through this self.assertRaises(botocore.exceptions.ClientError, lambda: bucket.Object(content_path).load()) storage.move_to_aws(content, content_path, content_type) try: bucket.Object(archival.CONTENT_KEY_PREFIX + content_path).load() except botocore.exceptions.ClientError: self.fail("object does not exist after move")
def _initialize_s3(self): bucket_name = api.portal.get_registry_record( 'castle.aws_s3_bucket_name') self._s3_conn, self._bucket = aws.get_bucket(bucket_name)