Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pybossa/api/task_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ def _customize_response_dict(self, response_dict):
def _upload_files_from_json(task_run_info, upload_path, with_encryption):
if not isinstance(task_run_info, dict):
return

app.logger.info("_upload_files_from_json called")
app.logger.info(" upload_path: %s", upload_path)
app.logger.info(" with_encryption: %s", with_encryption)

for key, value in task_run_info.items():
if key.endswith('__upload_url'):
filename = value.get('filename')
Expand All @@ -211,22 +216,38 @@ def _upload_files_from_json(task_run_info, upload_path, with_encryption):
continue
bucket = app.config.get("S3_BUCKET_V2") if app.config.get("S3_CONN_TYPE_V2") else app.config.get("S3_BUCKET")
conn_name = "S3_TASKRUN_V2" if app.config.get("S3_CONN_TYPE_V2") else "S3_TASKRUN"

app.logger.info(" Uploading file from JSON: %s", filename)
app.logger.info(" bucket: %s", bucket)
app.logger.info(" conn_name: %s", conn_name)
app.logger.info(" content_length: %d bytes", len(content) if content else 0)

out_url = s3_upload_from_string(bucket,
content,
filename,
directory=upload_path, conn_name=conn_name,
with_encryption=with_encryption,
upload_root_dir=upload_root_dir)
task_run_info[key] = out_url
app.logger.info(" uploaded to: %s", out_url)


def _upload_files_from_request(task_run_info, files, upload_path, with_encryption):
app.logger.info("_upload_files_from_request called")
app.logger.info(" upload_path: %s", upload_path)
app.logger.info(" with_encryption: %s", with_encryption)
app.logger.info(" files count: %d", len(files))

for key in files:
if not key.endswith('__upload_url'):
raise BadRequest("File upload field should end in __upload_url")
file_obj = request.files[key]
bucket = app.config.get("S3_BUCKET_V2") if app.config.get("S3_CONN_TYPE_V2") else app.config.get("S3_BUCKET")
conn_name = "S3_TASKRUN_V2" if app.config.get("S3_CONN_TYPE_V2") else "S3_TASKRUN"

app.logger.info(" Uploading file from request: %s", file_obj.filename)
app.logger.info(" bucket: %s", bucket)
app.logger.info(" conn_name: %s", conn_name)
s3_url = s3_upload_file_storage(bucket,
file_obj,
directory=upload_path, conn_name=conn_name,
Expand Down
46 changes: 35 additions & 11 deletions pybossa/cloud_store_api/base_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from boto3.s3.transfer import TransferConfig
from botocore.exceptions import ClientError

logger = logging.getLogger(__name__)

from flask import current_app as app

class BaseConnection(ABC):
@abstractmethod
Expand All @@ -17,20 +16,26 @@ def __init__(
self.client = None

def get_key(self, bucket, path, **kwargs):
app.logger.info("BaseConnection.get_key called")
app.logger.info(" bucket: %s", bucket)
app.logger.info(" path: %s", path)
app.logger.info(" kwargs: %s", kwargs)

try:
fobj = self.client.get_object(
Bucket=bucket,
Key=path,
**kwargs,
)
app.logger.info(" get_object successful")
return fobj
except ClientError as e:
if "Error" in e.response:
err_resp = e.response["Error"]
http_status = e.response.get("ResponseMetadata", {}).get(
"HTTPStatusCode"
)
logger.warning(
app.app.logger.warning(
"%s: %s, key %s. http status %d",
self.__class__.__name__,
str(e),
Expand Down Expand Up @@ -76,7 +81,7 @@ def get_contents_as_string(self, bucket, path, encoding="utf-8", **kwargs):
http_status = e.response.get("ResponseMetadata", {}).get(
"HTTPStatusCode"
)
logger.warning(
app.logger.warning(
"%s: %s, key %s. http status %d",
self.__class__.__name__,
str(e),
Expand All @@ -86,6 +91,12 @@ def get_contents_as_string(self, bucket, path, encoding="utf-8", **kwargs):
raise

def set_contents(self, bucket, path, content, **kwargs):
app.logger.info("BaseConnection.set_contents called")
app.logger.info(" bucket: %s", bucket)
app.logger.info(" path: %s", path)
app.logger.info(" content_length: %d bytes", len(content) if content else 0)
app.logger.info(" content_type: %s", type(content).__name__)

if type(content) == str:
content = content.encode()
try:
Expand All @@ -94,13 +105,14 @@ def set_contents(self, bucket, path, content, **kwargs):
self.client.upload_fileobj(
source, bucket, path, Config=config, ExtraArgs=kwargs
)
app.logger.info(" upload_fileobj successful")
except ClientError as e:
if "Error" in e.response:
err_resp = e.response["Error"]
http_status = e.response.get("ResponseMetadata", {}).get(
"HTTPStatusCode"
)
logger.warning(
app.logger.warning(
"%s: %s, key %s. http status %d",
self.__class__.__name__,
str(e),
Expand All @@ -123,7 +135,7 @@ def set_contents_from_file(self, source_file, bucket, path, **kwargs):
http_status = e.response.get("ResponseMetadata", {}).get(
"HTTPStatusCode"
)
logger.warning(
app.logger.warning(
"%s: %s, key %s. http status %d",
self.__class__.__name__,
str(e),
Expand All @@ -132,19 +144,24 @@ def set_contents_from_file(self, source_file, bucket, path, **kwargs):
)
raise
def delete_key(self, bucket, path, **kwargs):
app.logger.info("BaseConnection.delete_key called")
app.logger.info(" bucket: %s", bucket)
app.logger.info(" path: %s", path)

try:
self.client.delete_object(
Bucket=bucket,
Key=path,
**kwargs,
)
app.logger.info(" delete_object successful")
except ClientError as e:
if "Error" in e.response:
err_resp = e.response["Error"]
http_status = e.response.get("ResponseMetadata", {}).get(
"HTTPStatusCode"
)
logger.warning(
app.logger.warning(
"%s: %s, key %s. http status %d",
self.__class__.__name__,
str(e),
Expand Down Expand Up @@ -172,7 +189,7 @@ def new_key(self, bucket, path):
http_status = e.response.get("ResponseMetadata", {}).get(
"HTTPStatusCode"
)
logger.warning(
app.logger.warning(
"%s: %s, key %s. http status %d",
self.__class__.__name__,
str(e),
Expand All @@ -182,20 +199,27 @@ def new_key(self, bucket, path):
raise

def copy_key(self, bucket, source_key, target_key, **kwargs):
app.logger.info("BaseConnection.copy_key called")
app.logger.info(" bucket: %s", bucket)
app.logger.info(" source_key: %s", source_key)
app.logger.info(" target_key: %s", target_key)

try:
copy_source = {"Bucket": bucket, "Key": source_key}
self.client.copy(CopySource=copy_source, Bucket=bucket, Key=target_key, ExtraArgs=kwargs)
app.logger.info(" copy successful")
except ClientError as e:
if "Error" in e.response:
err_resp = e.response["Error"]
http_status = e.response.get("ResponseMetadata", {}).get(
"HTTPStatusCode"
)
logger.warning(
"%s: %s, key %s. http status %d",
app.logger.warning(
"%s: %s, source_key %s, target_key %s. http status %d",
self.__class__.__name__,
str(e),
err_resp.get("Key", path),
source_key,
target_key,
http_status,
)
raise
Expand Down
Loading