예제 #1
0
    def test_upload_to_s3(self, mock_get_s3_client, mock_isfile):
        mock_client = MagicMock()
        mock_get_s3_client.return_value = mock_client
        example_filename = os.path.join(settings.EXPORT_STAGING_ROOT, self._uuid, self._filename)
        with patch('audit_logging.file_logging.logging_open', mock_open(read_data='test'), create=True) as mock_open_obj:
            upload_to_s3(self._uuid, example_filename, self._filename)

        mock_client.upload_fileobj.assert_called_once()
        mock_isfile.assert_called_once_with(example_filename)
        mock_client.generate_presigned_url.assert_called_once_with('get_object', Params={'Bucket': 'test-bucket', 'Key': 'd34db33f/cool.pbf'})
예제 #2
0
 def test_s3_put(self):
     client = get_s3_client()
     stubber = Stubber(client)
     stubber.activate()
     stubber.add_response('put_object', self._base_response,
                          dict(Bucket=ANY, Key=self._path, Body='test'))
     with patch('eventkit_cloud.utils.s3.open',
                mock_open(read_data='test'),
                create=True) as mock_open_obj:
         upload_to_s3(self._uuid,
                      self._filename,
                      self._filename,
                      client=client)
예제 #3
0
def make_thumbnail_downloadable(filepath,
                                provider_uid,
                                download_filename=None):

    filename = os.path.basename(filepath)
    if download_filename is None:
        download_filename = filename

    filesize = os.stat(filepath).st_size
    thumbnail_snapshot = MapImageSnapshot.objects.create(download_url="",
                                                         filename=filepath,
                                                         size=filesize)
    if getattr(settings, "USE_S3", False):
        download_url = s3.upload_to_s3(str(thumbnail_snapshot.uid), filepath,
                                       download_filename)
        os.remove(filepath)
    else:
        download_path = os.path.join(
            get_provider_image_download_dir(provider_uid), download_filename)
        download_url = os.path.join(
            get_provider_image_download_path(provider_uid), download_filename)
        make_dirs(os.path.split(download_path)[0])
        shutil.copy(filepath, download_path)

    thumbnail_snapshot.download_url = download_url
    thumbnail_snapshot.save()

    return thumbnail_snapshot
예제 #4
0
    def test_upload_to_s3(self, mock_get_s3_client, mock_isfile):
        mock_client = MagicMock()
        mock_get_s3_client.return_value = mock_client
        example_filename = os.path.join(settings.EXPORT_STAGING_ROOT,
                                        self._uuid, self._filename)
        expected_download_path = f"{self._uuid}/{self._filename}"
        with patch("audit_logging.file_logging.logging_open",
                   mock_open(read_data="test"),
                   create=True):
            upload_to_s3(example_filename)

        mock_client.upload_fileobj.assert_called_once()
        mock_isfile.assert_called_once_with(Path(example_filename))
        mock_client.generate_presigned_url.assert_called_once_with(
            "get_object",
            Params={
                "Bucket": "test-bucket",
                "Key": expected_download_path
            })
예제 #5
0
    def test_s3_delete(self):
        client = get_s3_client()
        stubber = Stubber(client)
        stubber.activate()

        stubber.add_response('put_object', self._base_response,
                             dict(Bucket=ANY, Key=self._path, Body='test'))

        list_objects_response = {
            'IsTruncated':
            False,
            'Name':
            'test-bucket',
            'MaxKeys':
            1000,
            'Prefix':
            '',
            'Contents': [{
                u'LastModified':
                datetime.datetime(2016, 9, 23, 11, 17, 14),
                u'ETag':
                '"20d2cb13afb394301bbea0bcff19e12b"',
                u'StorageClass':
                'STANDARD',
                u'Key':
                self._path,
                u'Owner': {
                    u'DisplayName':
                    'test',
                    u'ID':
                    '31d89f79718dbd4435290740e6fa5e41cffafa7d9a3c323c85b525342e6341ae'
                },
                u'Size':
                77824
            }],
            'EncodingType':
            'url',
            'ResponseMetadata': {
                'RequestId': 'abc123',
                'HTTPStatusCode': 200,
                'HostId': 'abc123'
            },
            'Marker':
            ''
        }
        stubber.add_response('list_objects', list_objects_response, {
            'Bucket': ANY,
            'Prefix': self._uuid
        })
        stubber.add_response('delete_object', self._base_response, {
            'Bucket': ANY,
            'Key': self._path
        })

        with patch('eventkit_cloud.utils.s3.open',
                   mock_open(read_data='test'),
                   create=True) as mock_open_obj:
            upload_to_s3(self._uuid,
                         self._filename,
                         self._filename,
                         client=client)

        delete_from_s3(self._uuid, client=client)