123456789101112131415161718192021222324252627 |
- def upload_file(file, chalid):
- access_key_id = utils.get_app_config('ACCESS_KEY_ID')
- secret_access_key = utils.get_app_config('SECRET_ACCESS_KEY')
- if access_key_id and secret_access_key:
- s3 = boto3.client(
- 's3',
- aws_access_key_id=access_key_id,
- aws_secret_access_key=secret_access_key
- )
- bucket = utils.get_app_config('BUCKET')
- else:
- s3 = boto3.client('s3')
- bucket = utils.get_app_config('BUCKET')
- filename = filter(clean_filename, secure_filename(file.filename).replace(' ', '_'))
- if len(filename) <= 0:
- return False
- md5hash = hashlib.md5(os.urandom(64)).hexdigest()
- key = md5hash + '/' + filename
- s3.upload_fileobj(file, bucket, key)
- db_f = Files(chalid, key)
- db.session.add(db_f)
- db.session.commit()
- return db_f.id, (md5hash + '/' + filename)
|