def get_s3_signed_url(): """ This API is responsible for generate signed url to post data to S3 --- tags: - auth parameters: - in: body name: publisher type: string required: true description: publisher name - in: body name: package type: string required: true description: package name - in: body name: path type: string required: true description: relative path of the resources responses: 200: description: Success schema: id: get_signed_url properties: key: type: string description: signed url for post data to S3 400: description: Publisher or package can not be empty 500: description: Internal Server Error """ try: data = request.get_json() publisher = data.get('publisher', None) package = data.get('package', None) path = data.get('path', None) md5 = data.get('md5', None) if publisher is None or package is None: return handle_error('INVALID_INPUT', 'publisher or package can not be empty', 400) if md5 is None: return handle_error('INVALID_INPUT', 'md5 hash can not be empty', 400) if path == 'datapackage.json': return handle_error( 'INVALID_INPUT', 'datapackage.json should not publish with this api', 400) metadata = BitStore(publisher=publisher, package=package) url = metadata.generate_pre_signed_post_object(path, md5) return jsonify({'data': url}), 200 except Exception as e: app.logger.error(e) return handle_error('GENERIC_ERROR', e.message, 500)
def test_generate_pre_signed_put_obj_url(self): with self.app.app_context(): s3 = boto3.client('s3') bucket_name = self.app.config['S3_BUCKET_NAME'] s3.create_bucket(Bucket=bucket_name) metadata = BitStore(publisher="pub_test", package="test_package", body='hi') post = metadata.generate_pre_signed_post_object( 'datapackage.json', 123) parsed = urlparse(post['url']) self.assertEqual( parsed.netloc, 's3-{region}.amazonaws.com'.format( region=self.app.config['AWS_REGION'])) self.assertEqual('public-read', post['fields']['acl'])