def generate_url_upload(self, key, mime_type): if not isinstance(key, str): raise ApplicationError('Key must be a string') if not isinstance(mime_type, str): raise ApplicationError('Mime type must be a string') return self.client.generate_presigned_url('put_object', Params={ 'Bucket': S3_BUCKET_NAME, 'Key': key, 'ContentType': mime_type, 'ACL': 'private' }, ExpiresIn=600)
def decorated(*args, **kwargs): from flask import request if not request.is_json: raise ApplicationError("Bad Request", detail="Expect json type body") data = schema_validator(request.get_json()) request.data = data return func(*args, **kwargs)
def get_file_from_s3(filename): if not storage.check_if_object_exists(filename): raise BadRequest('File not found') try: file = storage.get_object(filename).get() except Exception as e: raise ApplicationError(str(e)) return Response(file['Body'].read(), mimetype=file['ContentType'])
def command(self, args): path = wot.guessFilePath(args.file, base_dir=args.base_dir) if path is None: raise FileNotFound('file is not found: {}'.format(args.file)) if not args.dest_dir and not args.list: raise ApplicationError('required option -d or -l') unzip.extractPattern(path, extract_dir=args.dest_dir, pattern=args.regex, opt_list=args.list)
def generate_url_read(self, key): if not isinstance(key, str): raise ApplicationError('Key must be a string') return self.client.generate_presigned_url( 'get_object', Params={ 'Bucket': S3_BUCKET_NAME, 'Key': key, }, )
def update(filename): if not storage.check_if_object_exists(filename): raise BadRequest('File not found') file = request.files['file'] if 'file' in request.files else None if file is None: raise BadRequest('Form data invalid') try: s3_object = storage.get_object(filename) s3_object.upload_fileobj(Fileobj=file, ExtraArgs={'ContentType': file.mimetype}) except Exception as e: raise ApplicationError(str(e)) return Response(file['Body'].read(), mimetype=file['ContentType'])
def upload_file_obj(self, data, key, content_type='image/jpg'): if key[0] == '/': key = key[1:] try: key = self.custom_key(key) return self.client.upload_fileobj( data, S3_BUCKET_NAME, key, ExtraArgs={'ContentType': content_type}) except S3UploadFailedError as e: current_app.logger.debug(e) raise ApplicationError('Can not upload to server')
def create_file_docx_from_template(template_path, data): file_name = get_filename(template_path, prefix='doc') document = DocxTemplate(template_path) document.render(data) file = document.get_docx() stream = BytesIO() file.save(stream) stream.seek(0) try: storage.upload_file_obj(stream, file_name, DOCX_MINE_TYPE) stream.close() except Exception as e: raise ApplicationError(str(e)) return file_name
def upload_file_to_s3(): file = request.files['file'] if 'file' in request.files else None if file is None: raise BadRequest('Form data invalid') if file.filename == '': raise BadRequest('Logo no selected file') if not allowed_file(file.filename): raise BadRequest('Extension is not allow') filename = get_filename(file.filename) try: storage.upload_file_obj(file, filename, file.mimetype) except Exception as e: raise ApplicationError(e) return generate_success_response(data={'filename': filename})
def get_object(self, key): if not isinstance(key, str): raise ApplicationError('Key must be a string') key = self.custom_key(key) return self.resource.Object(S3_BUCKET_NAME, key)