예제 #1
0
    def test_delete_image_from_s3(self, s3_client, organization_with_image):

        uploader = S3Uploader(u'group')
        storage_path = S3Uploader.get_storage_path(u'group')
        key = os.path.join(storage_path, organization_with_image[u'image_url'])

        # key must exist
        assert s3_client.head_object(Bucket=self.bucket_name, Key=key)

        uploader.delete(organization_with_image[u'image_url'])

        # key shouldn't exist, this raises ClientError
        with pytest.raises(ClientError):
            s3_client.head_object(Bucket=self.bucket_name, Key=key)
예제 #2
0
    def uploaded_file_redirect(self, upload_to, filename):
        '''Redirect static file requests to their location on S3.'''
        bucket_name = config.get('ckanext.s3filestore.aws_bucket_name')
        region_name = config.get('ckanext.s3filestore.region_name')
        if is_path_addressing():
            host_name = config.get(
                'ckanext.s3filestore.host_name',
                'https://s3-{region_name}.amazonaws.com'.format(
                    region_name=region_name))
            # ensure trailing slash
            if host_name[-1] != '/':
                host_name += '/'
            host_name += bucket_name
        else:
            host_name = config.get(
                'ckanext.s3filestore.download_proxy',
                'https://{bucket_name}.s3.{region_name}.amazonaws.com'.format(
                    bucket_name=bucket_name, region_name=region_name))
        storage_path = S3Uploader.get_storage_path(upload_to)
        filepath = os.path.join(storage_path, filename)

        redirect_url = '{host_name}/{filepath}'\
            .format(filepath=filepath,
                    host_name=host_name)
        redirect(redirect_url)
예제 #3
0
    def test_create_organization_with_image_then_clear(self, s3_client,
                                                       organization_with_image, ckan_config):
        user = factories.Sysadmin()
        context = {
            u"user": user["name"]
        }
        result = helpers.call_action(u'organization_show', context,
                                     id=organization_with_image[u'id'])

        storage_path = S3Uploader.get_storage_path(u'group')
        key = os.path.join(storage_path, result[u'image_url'])

        # key must exist
        assert s3_client.head_object(Bucket=self.bucket_name, Key=key)

        helpers.call_action(u'organization_update', context,
                            clear_upload=True,
                            id=organization_with_image[u'id'],
                            name=organization_with_image[u'name'])

        # key shouldn't exist, this raises ClientError
        with pytest.raises(ClientError) as e:
            s3_client.head_object(Bucket=self.bucket_name, Key=key)

        assert e.value.response[u'Error'][u'Code'] == u'404'
예제 #4
0
    def test_create_organization_with_image(self, s3_client,
                                            organization_with_image,
                                            ckan_config):
        user = factories.Sysadmin()
        context = {u"user": user["name"]}
        result = helpers.call_action(u'organization_show',
                                     context,
                                     id=organization_with_image[u'id'])

        storage_path = S3Uploader.get_storage_path(u'group')
        filepath = os.path.join(storage_path, result[u'image_url'])

        # key must exist
        assert s3_client.head_object(Bucket=self.bucket_name, Key=filepath)
예제 #5
0
def uploaded_file_redirect(upload_to, filename):
    '''Redirect static file requests to their location on S3.'''

    storage_path = S3Uploader.get_storage_path(upload_to)
    filepath = os.path.join(storage_path, filename)
    base_uploader = BaseS3Uploader()

    try:
        url = base_uploader.get_signed_url_to_key(filepath)
    except ClientError as ex:
        if ex.response['Error']['Code'] == 'NoSuchKey' or '404':
            return abort(404, _('Keys not found on S3'))
        else:
            raise ex

    return redirect(url)
예제 #6
0
 def uploaded_file_redirect(self, upload_to, filename):
     '''Redirect static file requests to their location on S3.'''
     host_name = config.get('ckanext.s3filestore.host_name')
     # Remove last characted if it's a slash
     if host_name[-1] == '/':
         host_name = host_name[:-1]
     storage_path = S3Uploader.get_storage_path(upload_to)
     filepath = os.path.join(storage_path, filename)
     #host = config.get('ckanext.s3.filestore.hostname')
     # redirect_url = 'https://{bucket_name}.minio.omc.ckan.io/{filepath}' \
     #     .format(bucket_name=config.get('ckanext.s3filestore.aws_bucket_name'),
     #             filepath=filepath)
     redirect_url = '{host_name}/{bucket_name}/{filepath}'\
                       .format(bucket_name=config.get('ckanext.s3filestore.aws_bucket_name'),
                       filepath=filepath,
                       host_name=host_name)
     redirect(redirect_url)
예제 #7
0
 def uploaded_file_redirect(self, upload_to, filename):
     '''Redirect static file requests to their location on S3.'''
     host_name = config.get('ckanext.s3filestore.host_name')
     # Remove last characted if it's a slash
     if host_name[-1] == '/':
         host_name = host_name[:-1]
     storage_path = S3Uploader.get_storage_path(upload_to)
     filepath = os.path.join(storage_path, filename)
     #host = config.get('ckanext.s3.filestore.hostname')
     # redirect_url = 'https://{bucket_name}.minio.omc.ckan.io/{filepath}' \
     #     .format(bucket_name=config.get('ckanext.s3filestore.aws_bucket_name'),
     #             filepath=filepath)
     redirect_url = '{host_name}/{bucket_name}/{filepath}'\
                       .format(bucket_name=config.get('ckanext.s3filestore.aws_bucket_name'),
                       filepath=filepath,
                       host_name=host_name)
     redirect(redirect_url)
예제 #8
0
 def test_uploader_get_path(self):
     storage_path = S3Uploader.get_storage_path(u'group')
     assert 'storage/uploads/group' == storage_path
 def test_uploader_storage_path(self):
     '''S3Uploader get_storage_path returns as expected'''
     returned_path = S3Uploader.get_storage_path('myfiles')
     assert_equal(returned_path, 'my-path/storage/uploads/myfiles')
예제 #10
0
 def test_uploader_storage_path(self):
     '''S3Uploader get_storage_path returns as expected'''
     returned_path = S3Uploader.get_storage_path('myfiles')
     assert_equal(returned_path, 'my-path/storage/uploads/myfiles')
 def test_clean_dict(self):
     '''S3Uploader retrieves bucket as expected'''
     uploader = S3Uploader('')
     date_dict = {'key': datetime.datetime(1970, 1, 2, 3, 4, 5, 6)}
     clean_dict = uploader.as_clean_dict(date_dict)
     assert_equal(clean_dict['key'], '1970-01-02T03:04:05.000006')
 def test_get_bucket(self):
     '''S3Uploader retrieves bucket as expected'''
     uploader = S3Uploader('')
     assert_true(uploader.get_s3_bucket(BUCKET_NAME))