2023-01-10 07:48:59 +00:00
|
|
|
from flask import Flask, request, jsonify
|
2023-01-07 22:38:47 +00:00
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
import boto3
|
|
|
|
from botocore.exceptions import ClientError, WaiterError
|
|
|
|
import tempfile
|
|
|
|
import os
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
session = boto3.Session()
|
|
|
|
storj_resource = session.resource('s3')
|
|
|
|
storj_client = boto3.client('s3',
|
|
|
|
aws_access_key_id=os.environ['STORJ_ACCESS_KEY'],
|
|
|
|
aws_secret_access_key=os.environ['STORJ_SECRET_KEY'],
|
|
|
|
endpoint_url=os.environ['STORJ_END_POINT']
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def upload_storj(filecontent, filename, bucket_name):
|
2023-01-08 00:21:41 +00:00
|
|
|
head = None
|
2023-01-10 07:48:59 +00:00
|
|
|
bucket_obj = None
|
2023-01-07 22:38:47 +00:00
|
|
|
try:
|
|
|
|
bucket = storj_resource.Bucket(bucket_name)
|
|
|
|
except ClientError:
|
|
|
|
bucket = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
# In case filename already exists, get current etag to check if the
|
|
|
|
# contents change after upload
|
|
|
|
head = storj_client.head_object(Bucket=bucket_name, Key=filename)
|
|
|
|
except ClientError:
|
|
|
|
etag = ''
|
|
|
|
else:
|
|
|
|
etag = head['ETag'].strip('"')
|
|
|
|
|
|
|
|
try:
|
2023-01-10 07:48:59 +00:00
|
|
|
bucket_obj = bucket.Object(filename)
|
2023-01-07 22:38:47 +00:00
|
|
|
except (ClientError, AttributeError):
|
2023-01-10 07:48:59 +00:00
|
|
|
bucket_obj = None
|
2023-01-07 22:38:47 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# Use the upload_fileobj method to safely upload the file
|
|
|
|
storj_client.upload_fileobj(
|
|
|
|
Fileobj=filecontent,
|
2023-01-10 07:48:59 +00:00
|
|
|
Bucket=bucket_name,
|
2023-01-07 22:38:47 +00:00
|
|
|
Key=filename
|
|
|
|
)
|
|
|
|
except (ClientError, AttributeError):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
2023-01-10 07:48:59 +00:00
|
|
|
bucket_obj.wait_until_exists(IfNoneMatch=etag)
|
2023-01-07 22:38:47 +00:00
|
|
|
except WaiterError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
head = storj_client.head_object(Bucket=bucket_name, Key=filename)
|
2023-01-08 00:21:41 +00:00
|
|
|
return head
|
2023-01-07 22:38:47 +00:00
|
|
|
|
|
|
|
|
2023-01-10 07:48:59 +00:00
|
|
|
@app.route('/api/upload', methods=['post'])
|
2023-01-07 22:38:47 +00:00
|
|
|
def upload():
|
2023-01-28 11:00:06 +00:00
|
|
|
print(request.files)
|
|
|
|
print(request.files.to_dict())
|
2023-01-27 23:07:25 +00:00
|
|
|
# check if the post request has the file part
|
|
|
|
if 'file' not in request.files:
|
2023-01-28 00:31:46 +00:00
|
|
|
return {'error': 'No file part'}, 400
|
2023-01-27 23:07:25 +00:00
|
|
|
file = request.files.get('file')
|
|
|
|
if file:
|
|
|
|
# save the file
|
2023-01-28 11:00:06 +00:00
|
|
|
filename = secure_filename(file.name or file.filename)
|
2023-01-28 00:31:46 +00:00
|
|
|
# if user does not select file, browser also
|
|
|
|
# submit a empty part without filename
|
2023-01-28 11:00:06 +00:00
|
|
|
if not filename:
|
2023-01-28 00:31:46 +00:00
|
|
|
return {'error': 'No selected file'}, 400
|
|
|
|
else:
|
2023-01-28 11:00:06 +00:00
|
|
|
# Save the file to a temporary location
|
|
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
|
|
temp_path = os.path.join(temp_dir, filename)
|
|
|
|
file.save(temp_path)
|
|
|
|
# Open the file in binary mode
|
|
|
|
with open(temp_path, 'rb') as filecontent:
|
|
|
|
result = upload_storj(filecontent, filename, 'discoursio')
|
2023-01-28 00:31:46 +00:00
|
|
|
else:
|
|
|
|
return {'error': 'No selected file'}, 400
|
|
|
|
return {'message': 'File uploaded', 'result': jsonify(result)}, 200
|