Ejemplo n.º 1
0
    def test_stat(self):
        skip_if_no_boto()

        bucket = 'mybucket'
        key = 'export.csv'
        uri = f's3://{bucket}/{key}'
        checksum = '3187896a9657a28163abb31667df64c8'
        last_modified = datetime(2019, 12, 1)

        store = S3FilesStore(uri)
        from botocore.stub import Stubber
        with Stubber(store.s3_client) as stub:
            stub.add_response(
                'head_object',
                expected_params={
                    'Bucket': bucket,
                    'Key': key,
                },
                service_response={
                    'ETag': f'"{checksum}"',
                    'LastModified': last_modified,
                },
            )

            file_stats = yield store.stat_file('', info=None)
            self.assertEqual(
                file_stats,
                {
                    'checksum': checksum,
                    'last_modified': last_modified.timestamp(),
                },
            )

            stub.assert_no_pending_responses()
Ejemplo n.º 2
0
 def test_persist(self):
     assert_aws_environ()
     uri = os.environ.get('S3_TEST_FILE_URI')
     if not uri:
         raise unittest.SkipTest("No S3 URI available for testing")
     data = b"TestS3FilesStore: \xe2\x98\x83"
     buf = BytesIO(data)
     meta = {'foo': 'bar'}
     path = ''
     store = S3FilesStore(uri)
     yield store.persist_file(path,
                              buf,
                              info=None,
                              meta=meta,
                              headers={'Content-Type': 'image/png'})
     s = yield store.stat_file(path, info=None)
     self.assertIn('last_modified', s)
     self.assertIn('checksum', s)
     self.assertEqual(s['checksum'], '3187896a9657a28163abb31667df64c8')
     u = urlparse(uri)
     content, key = get_s3_content_and_delete(u.hostname,
                                              u.path[1:],
                                              with_key=True)
     self.assertEqual(content, data)
     if is_botocore():
         self.assertEqual(key['Metadata'], {'foo': 'bar'})
         self.assertEqual(key['CacheControl'],
                          S3FilesStore.HEADERS['Cache-Control'])
         self.assertEqual(key['ContentType'], 'image/png')
     else:
         self.assertEqual(key.metadata, {'foo': 'bar'})
         self.assertEqual(key.cache_control,
                          S3FilesStore.HEADERS['Cache-Control'])
         self.assertEqual(key.content_type, 'image/png')
Ejemplo n.º 3
0
    def test_persist(self):
        skip_if_no_boto()

        bucket = 'mybucket'
        key = 'export.csv'
        uri = f's3://{bucket}/{key}'
        buffer = mock.MagicMock()
        meta = {'foo': 'bar'}
        path = ''
        content_type = 'image/png'

        store = S3FilesStore(uri)
        from botocore.stub import Stubber
        with Stubber(store.s3_client) as stub:
            stub.add_response(
                'put_object',
                expected_params={
                    'ACL': S3FilesStore.POLICY,
                    'Body': buffer,
                    'Bucket': bucket,
                    'CacheControl': S3FilesStore.HEADERS['Cache-Control'],
                    'ContentType': content_type,
                    'Key': key,
                    'Metadata': meta,
                },
                service_response={},
            )

            yield store.persist_file(
                path,
                buffer,
                info=None,
                meta=meta,
                headers={'Content-Type': content_type},
            )

            stub.assert_no_pending_responses()
            self.assertEqual(
                buffer.method_calls,
                [
                    mock.call.seek(0),
                    # The call to read does not happen with Stubber
                ]
            )